流(续)
字符流
![]()
字符流的父类(抽象类)
- Reader(字符输入流)
- Writer(字符输出流)
字符流的子类
文件字符流(自带缓冲区)
- FileReader
- int read():从输入流中读取一个字符数据。返回读到的字符数据,如果达到流末尾,返回-1。
- int read(char[] c):从输入流中读取字符数组长度的字符数据存入数组中。返回实际读到的字符数,如果达到流末尾,返回-1。
- FileWriter
- void write(int n):将指定字符写入输出流,只能写入包含16位低阶字节的整型数值,16位高阶字节将会被忽略。
- void write(String str):将指定字符串写入输出流。
- void write(char[] cbuf):将指定字符数组写入输出流。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package ml.guest997;
import java.io.FileReader; import java.io.IOException;
public class FR { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("test.txt"); int data; while ((data = fr.read()) != -1) { System.out.print((char) data); } fr.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package ml.guest997;
import java.io.FileWriter; import java.io.IOException;
public class FW { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("out3.txt"); fw.write(110); fw.write("Guest997"); fw.write("你好"); fw.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package ml.guest997;
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;
public class CopyFile2 { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("test.txt"); FileWriter fw = new FileWriter("copy2.txt"); int data; while ((data = fr.read()) != -1) { fw.write(data); } fr.close(); fw.close(); } }
|
字符缓冲流
BufferedReader / BufferedWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package ml.guest997;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;
public class BR { public static void main(String[] args) throws IOException { FileReader fr1 = new FileReader("test2.txt"); BufferedReader br1 = new BufferedReader(fr1); int data; while ((data = br1.read()) != -1) { System.out.print((char) data); } br1.close();
System.out.println("\n------");
FileReader fr2 = new FileReader("test2.txt"); BufferedReader br2 = new BufferedReader(fr2); String lineData; while ((lineData = br2.readLine()) != null) { System.out.println(lineData); } br2.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package ml.guest997;
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException;
public class BW { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("out4.txt"); BufferedWriter bw = new BufferedWriter(fw); bw.write("Guest997"); bw.newLine(); bw.write("一个在互联网下的小小 Coder"); bw.close(); } }
|
打印流
PrintWriter
- 封装了 print() 和 println()。
- 支持数据原样打印。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package ml.guest997;
import java.io.FileNotFoundException; import java.io.PrintWriter;
public class PW { public static void main(String[] args) throws FileNotFoundException { PrintWriter pw = new PrintWriter("print.txt"); pw.println(110); pw.println("你好"); pw.println(3.1415926); pw.println(true); pw.close(); } }
|
转换流
InputStreamReader / OutputStreamWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package ml.guest997;
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets;
public class ISR { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("test.txt"); InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); int data; while ((data = isr.read()) != -1) { System.out.print((char) data); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package ml.guest997;
import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets;
public class OSW { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("out5.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8); osw.write(99); osw.write("guest"); osw.write("你好"); osw.close(); } }
|