Io stream (character stream) usage and the previous combination of understanding is very simple

Huh? What happened just now

Well, let’s take a look at the character stream

By the way, let’s talk about the character stream before doing a small case to get the previous knowledge

Exercise: Copy of files

public class Fdemo {
    
    
    //复制文件
    public static void main(String[] args) {
    
    
        //字节输入
        FileInputStream fi = null;
        //字节输出
        FileOutputStream fo =null;
        String path="D:\\jj\\a2.txt";
        try {
    
    
            fi = new FileInputStream(path);
            fo =new FileOutputStream("D:\\jj\\FuZhi.txt");
            //遍历
            int b;
            while ((b=fi.read())!=-1){
    
    
                fo.write(b);
              }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

        //释放资源
        try {
    
    
            fi.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        try {
    
    
            fo.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

1.1 FileWriter: File character output stream:

Role : writing the character data in memory to a file

Construction method:

FileWriter(File file) Construct a FileWriter object for a File object.

FileWriter(String fileName) Construct a FileWriter object with a given file name.

Use steps of file character output stream (emphasis):

​ 1. Create a FileWriter object, bind the destination of the data to be written in the constructor

​ 2. Use the method write in FileWriter to write data to the memory buffer (the process of converting characters to bytes)

​ 3. The method in FileWriter will be flushed, and the data in the memory buffer will be flushed to the file

​ 4.Release resources

/*
* 字符输出流【Writer】
*   void write(int c)          写入单个字符。
    void write(char[] cbuf)    写入字符数组。
    abstract  void write(char[] cbuf, int off, int len)   写入字符数组的某一部分,off数组的开始索引,
*    len写的字符个数。
    void write(String str)     写入字符串。
    void write(String str, int off, int len)   写入字符串的某一部分,off字符串的开始索引,
*    len写的字符个数。
    void flush()   刷新该流的缓冲。
    void close()   关闭此流,但要先刷新它。
* */
public class Fdemo07 {
    
    
    //写出字符
    public static void main(String[] args) throws IOException {
    
    
        FileWriter fileWriter = null;
        try {
    
    
            fileWriter=new FileWriter("D:\\jj\\b.txt");
            fileWriter.write(93);
            fileWriter.write("我喜欢");
            fileWriter.write("雷姆");
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        /*
        【注意】关闭资源时,与FileOutputStream不同。
        如果不关闭,数据只是保存到缓冲区,并未保存到文件。
        */
        fileWriter.close();
    }
}

Other methods of writing data in character output stream

public class Fdemo08 {
    
    
    //写出字符数组
    public static void main(String[] args) {
    
    
        FileWriter fileWriter = null;
        try {
    
    
            fileWriter=new FileWriter("D:\\jj\\b.txt");
            char[] chars = "都拉都拉都拉都拉都拉都拉".toCharArray();
            fileWriter.write(chars);          //都拉都拉都拉都拉都拉都拉
            fileWriter.write("奥里给");        //直接写字符串
            fileWriter.write(chars,0,2);      //写字符数组的一部分
            fileWriter.write("奥里给",0,2);    //写字符数组的一部分
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        try {
    
    
            fileWriter.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

1.2 File character input stream

Character input stream Reader: is the top-level parent class of character input stream, defines some common member methods, and is an abstract class

Member method:

int read()Read a single character and returns

int read(char[] cbuf)Once read multiple characters, reading characters into an array

void close()Closes the stream and releases all associated resources

FileReader: File character input stream

Function: read the data in the hard disk file into the memory in the form of characters

Construction method:

FileReader(File file)Create a new FileReader, given a File read.

FileReader(String fileName)Creates a new FileReader, name of the file to be read given.

​ Parameters: the data source to read the file

​ String fileName: file path

​ File file: file

Steps to use character input stream:

1. Create a FileReader object and bind the data source

2. Use the method read in the FileReader object to read the file

3. Release resources

Let's have a king fried

int read() read a single file and return

public class Fdemo09 {
    
    
    //写出字符数组
    public static void main(String[] args) {
    
    
        FileReader fileReader = null;
        try {
    
    
            fileReader=new FileReader("D:\\jj\\b.txt");
         
           int b=0;
            while ((b=fileReader.read())!=-1){
    
    
                System.out.println((char)b);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

        try {
    
    
            fileReader.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

}

int read (char[] cbuf) Read multiple files at once, read characters into an array

public class Fdemo09 {
    
    
    //写出字符数组
    public static void main(String[] args) {
    
    
        FileReader fileReader = null;
        try {
    
    
            fileReader=new FileReader("D:\\jj\\b.txt");
            char[] chars = new char[1024];
            int b=0;  //每次读取的有效个数
            while ((b=fileReader.read(chars))!=-1){
    
    
                /*
                    String类的构造方法
                     String(char[] value)把字符数组转换为字符串
                     String(char[] value, int offset , int count)把字符数组的一部分转换为字符串, offset数组的开始索引  count转换的个数
                    
                */
                System.out.println(new String(chars,0,b));
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

        try {
    
    
            fileReader.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

}

Guess you like

Origin blog.csdn.net/agood_man/article/details/108299241