1. The Write
character 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 FileWriter
file character input stream
- Role: theRAMThe datacharacterThe way to writehard diskCan be avoided Chinese garbage problem
- Construction method:
FileWriter(String name) 写入数据的目的地是一个文件的路径
FileWriter(File file) 写入数据的目的地是一个文件
- The step of using the output character stream
- Create a create a
FileWriter
object constructor to fill in the destination of write data
FileWriter file = new FileWriter("G:\\Java\\测试文件夹2\\b.txt");
- Call to create an object
write
method, the data is written toMemory Buffer(Byte character conversion process)
file.write(98);
- Call to create an object
flush
method, the data memory buffer, flushed to the file (you can not write, close automatically refresh)
file.flush();
- 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();
}
}
- 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();
}
}
-
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 Reader
character input stream
- Members common method
int len = read(); 一次性读取一个字符
int len = read(char[] c); 一次性读取多个字符
close(); 释放资源
Reader
Class is abstract and can not be used directly, the need to create subclasses
2.1 FileReader
file character input stream
- Role: theHard disk fileThe datacharacterThe way to readRAMCan be avoided Chinese garbage problem
- Construction method:
FileReader file = new FileReader(String name);
FileReader file = new FileReader(File file);
- Steps for usage
- Create a create a
FileReader
object constructor to fill in the data source to read the file
FileReader file = new FileReader("G:\\Java\\测试文件夹2\\a.txt");
- Call the object's
read()
method, read the file
int len = 0;
while ((len=fr.read())!=-1){
System.out.print((char)len);
}
- 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();
}
}
- Note: Chinese garbage problem:
- 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();
}
}