天天看点

java InputStream和OutputStream

**`

InputStream

`**

InputStream抽象类中的常用方法:

int available() 返回可被读取的字节的长度

close() 关闭流通道

int read() 从文件中读取一字节内容,如果读到内容,返回该字节对应的ascii码,可以通过转成char类型打印该字节的内容,中文的话会乱码 ,如果读不到内容,返回-1,读完后指针移动到下个位置

int read(byte[] b) 每次从文件中读取一定数量的字节放入byte数组中,返回其读到的字节数目,如果没有读到内容,返回-1,读到最后可能读不满,那么最后一次读取不会完全覆盖之前的byte数组,byte数组中存放的是ascii码可使用new String(byte[] b,int offset,int length)根据指定长度将byte数组转成字符串,

int read(byte[] b, int off, int len)将指定长度的内容读到byte数组中,读不到内容返回-1

import java.io.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
    public static void main(String[] args)throws Exception{
        //File file=new File("/E:/QQ/吴晓青/test/test1.txt");
        //System.out.println(file.createNewFile());
        FileInputStream fi = null;
        try {
            //绝对路径,加转义字符防止路径不正确
            fi=new FileInputStream("/E:/QQ/吴晓青/test/test1.txt");//一定要先有这个文件
            //相对路径
           // fi=new FileInputStream("./test/test1.txt");
               /*
              System.out.println(fi.read());//fi.read()每次读取一字节,然后游标会移动到下一个
              System.out.println(fi.read());
              char c=(char)fi.read();//强转为char类型,保证读出来的不是ascii码
              System.out.println(c);
              */
            //定义byte数组每次读取三个字节提高效率
             /*  byte[] bytes=new byte[4];
               while (fi.read(bytes)!=-1){
                   System.out.println( new String(bytes));
               }*/
            byte[] bytes=new byte[4];
            int temp;
            while ((temp=fi.read(bytes))!=-1){//在读的过程中一定要保证文件里面有东西!
                //new String (byte[],offset,len)将byte数组按指定索引转换成字符串
                System.out.println(new String(bytes,0,temp) );
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fi.close();//注意这里一定要及时的关闭它
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
           

**

OutputStream

**

OutputStream抽象类中常用方法

void close()关闭输出流,释放系统资源

void flush()刷新输出流,保证缓存中内容全部写出去

void write(byte[] b) 一个bytes数组通过输出流写入到输出流

void write(byte[] b, int off, int len) 将指定起始位置和长度的byte数组写入到输出流

Writes len bytes from the specified byte array starting at offset off to this output stream.

abstract void write(int b)写一个byte到输出流,int值的高24位(如果有)被忽略,将int的低8位转换成ascii码写入

OutputStream的子类中常用的有FileOutputStream,它的构造方法有5个,常用的有

FileOutputStream(File file) 创建一个文件输出流,参数可以是文件,也可以是字符串形式的文件地址

FileOutputStream(String name)

FileOutputStream(File file, boolean append) 创建一个文件输出流,第一个参数是文件名,可以是字符串,第2个参数是否覆盖文件内容,如果为true在文件内容的后面直接写(要注意写入换行符),不覆盖原有内容,为false则覆盖

FileOutputStream继承自OutputStream,它的常用方法是重写自父类的方法

注意如果该路径文件不存在,则自动创建该路径下文件

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args){
        FileOutputStream fo=null;
        try {
            fo=new FileOutputStream("\\E:\\QQ\\吴晓青\\test\\test1.txt",true);
            fo.write(100);

            String msg="hello world";
            //调用字符串的getBytes方法,将字符串转换为byte数组
            fo.write(msg.getBytes());//write方法不会自动换行
            fo.write(msg.getBytes());//再写一遍
            fo.write("\n".getBytes());//将换行符转换成byte数组,写入到文件中
            fo.write("javase".getBytes());
            //   刷新以保证写入
            fo.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
           

借鉴https://blog.csdn.net/sinat_41132860/article/details/84326955?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161762998416780255271462%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161762998416780255271462&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-3-84326955.pc_search_result_cache&utm_term=OutputStream%E5%92%8CInputStream