My name is character stream, and I am also a ruthless machine!

Character stream

Insert picture description here

Writer (output stream)

Construction method

FileWriter​(File file) Write a FileWriter to File, use the platform's default charset
FileWriter​(FileDescriptor fd) to construct a file descriptor given by FileWriter, and use the platform's default charset.
FileWriter​(File file, boolean append) Constructs a File under the given FileWriter to be written, and uses the platform's default charset to construct a boolean value indicating whether to append the written data.
FileWriter​(File file, Charset charset) Constructs a FileWriter for File writing and charset.
FileWriter​(File file, Charset charset, boolean append) Constructs FileWriter to give File write, charset and a boolean value indicating whether to append the written data.
FileWriter​(String fileName) constructs a FileWriter to give the file name, using the platform's default charset
FileWriter​(String fileName, boolean append) uses the platform's default charset to construct a FileWriter, given a file name and a boolean value, indicating whether to write additional The data.
FileWriter​(String fileName, Charset charset) Constructs a FileWriter to give the file name and charset.
FileWriter​(String fileName, Charset charset, boolean append) Constructs a FileWriter given a file name, charset and a boolean value, indicating whether to append the written data.

Implementation

package work.february.two.daily;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 19:46
 * @Modified By:
 */
public class Demo7 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        File file =new File("d://c.txt");
        file.createNewFile();
        Writer writer=new FileWriter("d://c.txt");

        //写入数据
        writer.write(65);
        writer.write('c');
        char [] arr={
    
    'a','b','d','e'};
        writer.write(arr);
        writer.write(arr,1,2);
        writer.write("我学会了!");
        //追加
        writer.append("爱了");
        writer.close();

    }
}

Refresh the cache (if you don't refresh, it won't go to the file)

package work.february.two.daily;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 20:26
 * @Modified By:
 */
public class Demo10 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileWriter fileWriter =new FileWriter("d://c.txt");
        fileWriter.append("锄禾日当午").append("汗滴禾下土");
        //刷新 缓存
        fileWriter.flush();
        fileWriter.close();
    }
}

reader (input stream)

FileReader​(File file) uses the platform FileReader to create a new FileReader when File is read.
FileReader​(FileDescriptor fd) uses the platform default charset to create a new FileReader, given FileDescriptor to read.
FileReader​(File file, Charset charset) Create a new FileReader, give File read and charset.
FileReader​(String fileName) uses the platform default charset to create a new FileReader, given the name of the file to be read.
FileReader​(String fileName, Charset charset) Given the name of the file to be read and the FileReader, create a new FileReader.

Implementation:

package work.february.two.daily;


import java.io.FileReader;
import java.io.IOException;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 20:02
 * @Modified By:
 */
public class Demo8 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //字符输入流 一个一个读
        FileReader fileReader=new FileReader("d:\\c.txt");
        while (true){
    
    
           int c= fileReader.read();
           if (c == -1){
    
    
               break;
           } else{
    
    
               System.out.println((char) c);
           }
        }
        fileReader.close();
    }
}

package work.february.two.daily;

import java.io.FileReader;
import java.io.IOException;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-02 20:06
 * @Modified By:
 */
public class Demo9 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileReader fileReader =new FileReader("d.txt");
        char [] chars=new  char[100];
        int len =fileReader.read();
        String text = new String(chars,0,len);
        System.out.println(text);
        file
        fileReader.close();

    }
}

Insert picture description here
Nonsense time:

If the customer thinks that the food is appropriate, can you give a free like! Thank you! Slow walker officer! It is recommended to pack and collect and come again next time. Shop Xiaoer QQ: 309021573, welcome to harass!

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113572806