1 import java.io.BufferedInputStream; 2 import java.io.BufferedOutputStream; 3 import java.io.DataInputStream; 4 import java.io.DataOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.PrintWriter; 9 10 public class TextCode {11 public static void main(String[] args) throws IOException {12 13 DataOutputStream output = new DataOutputStream(14 new BufferedOutputStream(new FileOutputStream("data.dat")));15 output.writeChar('a');16 output.writeInt(5);//int型32位17 output.writeInt(16);/*十六进制显示:00 61 00 00 00 05 00 00 00 10*/18 output.flush();//当缓冲区填满或当流被清空时,数据就被写出。19 DataInputStream input = new DataInputStream(20 new BufferedInputStream(new FileInputStream("data.dat")));21 System.out.println(input.readChar());22 System.out.println(input.readInt());23 System.out.println(input.readInt());24 output.close();25 input.close();26 PrintWriter out = new PrintWriter("text.txt");27 out.println("你好");28 out.println("Hello");29 out.print(10);/*十六进制显示:30 C4 E3 BA C3 0D OA 48 65 6C 6C 6F 0D 0A 31 30*/31 out.close();//如果没有调用该方法,数据就不能正确地保存在文件中32 }33 }
FileInputStream和FileOutputStream类:是为了从/向文件读取/写入字节。
FilterInputStream类和FilterOutputStream类:过滤器数据流。
DataInputStream类(从数据流读取字节,并且把它们转换为正确的基本类型或字符串)和DataOutputStream类(将基本类型的值或字符串转换成字节,并且将字节输出到数据流)
- +readByte():byte +writeByte(v: int):void
- +readDouble():double +writeDouble(v: double):void
- +writeChar(c: char): void 向输出流写入一个字符(由两个字节组成)
- +writeChars(s: String): void
BufferedInputStream类和BufferedOutputStream类为存储字节在流中添加一个缓冲区,以提高处理效率。
- BufferedInputStream(int: InputStream)
- BufferedOutputStream(filename:String, bufferSize: int)
ObjectInputStream类和ObjectOutputStream类:对象的输入输出。
- ObjectInputStream(in: InputStream)
- ObjectOutputStream(out: OutputStream)
1 import java.io.BufferedInputStream; 2 import java.io.BufferedReader; 3 import java.io.DataInputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileReader; 8 import java.io.IOException; 9 import java.io.InputStreamReader;10 import java.util.Scanner;11 12 13 public class Test {14 public static void main(String[] args) throws IOException {15 BufferedReader in;16 try {17 //FileReader会抛出异常18 in = new BufferedReader(new FileReader("text.txt"));19 System.out.println(in.readLine());20 in.close();21 } catch (IOException e) {22 }23 try {24 Scanner input = new Scanner(new File("text.txt"));25 System.out.println(input.nextLine());26 input.close();27 } catch (FileNotFoundException e) {28 }29 /*BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。*/30 /*InputStreamReader类将包含字节(用某种字符编码方式表示的字符)的输入流转换为可以产生Unicode字符的读入器*/31 BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));//字符的读取32 Scanner input1 = new Scanner(System.in);//从键盘输入33 try{34 FileInputStream fileInput = new FileInputStream("text.txt");//只能读入字节和字节数组35 /*默认不使用缓冲区*/36 DataInputStream din = new DataInputStream(fileInput);//可以读取数据类型Byte,Int,Char,Double...37 /*嵌套过滤器来添加多重功能*/38 DataInputStream dInput = new DataInputStream(39 new BufferedInputStream(new FileInputStream("text1.txt")));40 System.out.println(dInput.readChar());41 42 }43 catch(FileNotFoundException e){44 }45 try {46 FileReader fileReader = new FileReader("tetx.txt");47 } catch (Exception e) {48 } 49 }50 51 }