JAVA single row diary -2020 / 1 / 26_IO flow - character stream

1. The Writecharacter input stream

  • Members common method
write(int c) 写入单个字符
write(char[] c) 写入字符数组
write(char[],int off,int len) 写入字符数组的一部分
write(String str) 写入字符串
write(String str,int off,int len) 写入字符串的一部分
flush() 刷新该流的缓冲
close() 关闭此流

1.1 FileWriterfile character input stream

  1. Role: theRAMThe datacharacterThe way to writehard diskCan be avoided Chinese garbage problem
  2. Construction method:
FileWriter(String name) 写入数据的目的地是一个文件的路径
FileWriter(File file)  写入数据的目的地是一个文件
  • The step of using the output character stream
  1. Create a create a FileWriterobject constructor to fill in the destination of write data
FileWriter file = new FileWriter("G:\\Java\\测试文件夹2\\b.txt");
  1. Call to create an object writemethod, the data is written toMemory Buffer(Byte character conversion process)
file.write(98);
  1. Call to create an object flushmethod, the data memory buffer, flushed to the file (you can not write, close automatically refresh)
file.flush();
  1. Release resources
file.close();
  • Other methods of writing data character output stream
  • Write a complete array of characters
  • Part of an array of characters to write
import java.io.FileWriter;
import java.io.IOException;

public class Writer {
    public static void main(String[] args) throws IOException {
        FileWriter file = new FileWriter("G:\\Java\\测试文件夹2\\b.txt");

        char[] chars = {'a', 'b', 'v', '你', '好'};
        file.write(chars);
        file.flush();

        file.write(chars, 3, 2);
        file.flush();
        file.close();
    }
}

Here Insert Picture Description

  • Write the full string
  • Write string section
import java.io.FileWriter;
import java.io.IOException;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        FileWriter fileWriter = new FileWriter("G:\\Java\\测试文件夹2\\c.txt");
        fileWriter.write("你好,再见!");
        fileWriter.flush();
        
        fileWriter.write("123asd123",3,3);
        fileWriter.flush();
        fileWriter.close();
    }
}

Here Insert Picture Description

  • Writing and line feed

  • Writing
    the construction method:

FileWriter(String name,boolean append) 写入数据的目的地是一个文件的路径,append为true则不会覆盖原文件,继续在原文件中续写,append为false则覆盖原文件
FileWiter(File file,boolean append)  写入数据的目的地是一个文件,append为true则不会覆盖原文件,继续在原文件中续写,append为false则覆盖原文件
  • Wrap: Write a newline

windows:\r\n
Linux:/n
Mac:/r

2. The Readercharacter input stream

  • Members common method
int len = read();   一次性读取一个字符
int len = read(char[] c);  一次性读取多个字符
close();  释放资源

ReaderClass is abstract and can not be used directly, the need to create subclasses

2.1 FileReaderfile character input stream

  1. Role: theHard disk fileThe datacharacterThe way to readRAMCan be avoided Chinese garbage problem
  2. Construction method:
FileReader file = new FileReader(String name);
FileReader file = new FileReader(File file);
  • Steps for usage
  1. Create a create a FileReaderobject constructor to fill in the data source to read the file
FileReader file = new FileReader("G:\\Java\\测试文件夹2\\a.txt");
  1. Call the object's read()method, read the file
int len = 0;
while ((len=fr.read())!=-1){
    System.out.print((char)len);
}
  1. Release memory
fr.close();
package Reader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Demo01 {
    public static void main(String[] args) throws IOException {
        FileReader fr= new FileReader("G:\\Java\\测试文件夹2\\qqq.txt");
        int len = 0;
        while ((len=fr.read())!=-1){
            System.out.print((char)len);
        }
        fr.close();
    }
}

Here Insert Picture Description

  • Note: Chinese garbage problem:
    Here Insert Picture Description
  • Method to read multiple characters
int len = 0;
char[] chars = new char[1024];
while ((len=fr.read(chars))!=-1){
    System.out.print(new String(chars,0,len));
}
package Reader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Demo01 {
    public static void main(String[] args) throws IOException {
        FileReader fr= new FileReader("G:\\Java\\测试文件夹2\\qqq.txt");
        int len = 0;
        char[] chars = new char[1024];
        while ((len=fr.read(chars))!=-1){
            System.out.print(new String(chars,0,len));
        }
        fr.close();
    }
}

Here Insert Picture Description

Published 103 original articles · won praise 1 · views 2648

Guess you like

Origin blog.csdn.net/wangzilong1995/article/details/104088942