1.流的概念
定义:数据流是指所有的数据通信通道
分类:
2.标准输入输出流(键盘,屏幕)
1)重要概念
标准输入输出都是System类中定义的类成员变量:
System.in:InputStream类型的,代表标准输入流,这个流是已经打开了的,默认状态对应于键盘输入。
System.out:PrintStream类型的,代表标准输出流,默认状态对应于屏幕输出
System.err:PrintStream类型的,代表标准错误信息输出流,默认状态对应于屏幕输出
也可通过重导向方法进行重新指定:
setIn(InputStream): 指定新的标准输入流
setOut(PrintStream):指定新的标准输出流
setErr(PrintStream):指定新的标准错误输出流
2)键盘输入,屏幕输出
1 | import java.io.*; |
2 | public class Echo |
3 | { |
4 | public static void main(String[] args) throws IOException |
5 | { |
6 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
7 | String s; |
8 | while((s = in.readLine()).length() != 0) |
9 | System.out.println(s); |
10 | } |
11 | } |
说明:
①以System.in作为参数,进一步创建了一个InputStreamReader流对象,它相当于字节流和字符流之间的一座桥梁,读取字节并将其转换为字符。
②BufferedReader用于对InputStreamReader处理后的信息进行缓冲,提高效率,它们都属于处理流。
3.文件操作
3.1文本文件(FileWriter,BufferedWriter,FileReader,BufferedReader)
写:
1 | import java.io.*; |
2 | class Ex6_4 |
3 | { |
4 | public static void main ( String[] args ) throws IOException |
5 | { |
6 | String fileName = "C://newHello.txt" ; |
7 |
|
8 | out.write( "Hello!" ); |
9 | out.newLine() ; |
10 | out.write( "This is another text file using BufferedWriter," ); |
11 | out.newLine(); ; |
12 | out.write( "So I can use a common way to start a newline" ); |
13 | out.close(); |
14 | } |
15 | } |
读 :
1 | import java.io.*; |
2 | class Ex6_5 |
3 | { |
4 | public static void main ( String[] args ) |
5 | { |
6 | String fileName = "C:/Hello.txt" , line; |
7 | try |
8 | { |
9 | BufferedReader in = new BufferedReader(new FileReader(fileName)); |
10 | line = in.readLine(); //读取一行内容 |
11 | while ( line != null ) |
12 | { |
13 | System.out.println( line ); |
14 | line = in.readLine(); |
15 | } |
16 | in.close(); |
17 | } |
18 | catch ( IOException iox ) |
19 | { |
20 | System.out.println("Problem reading " + fileName ); |
21 | } |
22 | } |
23 | } |
3.2二进制文件(非字符)
写:
基类:OutputStream
继承类:①FileOutputStream
②DataOutputStream:具有写各种基本数据类型的方法(属于过滤流)
③BufferedOutputStream:对于大量数据的写入,可以提高效率(属于过滤流)
1 | import java.io.*; |
2 | class Ex6_8 |
3 | { |
4 | public static void main (String[] args) throws IOException |
5 | { |
6 | String fileName = "mixedTypes.dat" ; |
7 | DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream( |
8 | new FileOutputStream(fileName))); |
9 | dataOut.writeInt(0); |
10 | System.out.println(dataOut.size() + " bytes have been written."); |
11 | dataOut.writeDouble(31.2); |
12 | System.out.println(dataOut.size() + " bytes have been written."); |
13 | dataOut.writeBytes("JAVA"); |
14 | System.out.println(dataOut.size() + " bytes have been written."); |
15 | dataOut.close(); |
16 | } |
17 | } |
读:
基类:InputStream
继承类:①FileInputStream
②DataInputStream:具有写各种基本数据类型的方法(属于过滤流)
③BufferedInputStream:对于大量数据的写入,可以提高效率(属于过滤流)
1 | import java.io.*; |
2 | public class Ex6_12 |
3 | { |
4 | public static void main(String[] args) throws IOException |
5 | { |
6 | FileInputStream s=new |
7 | FileInputStream("c://Hello.txt"); |
8 | int c; |
9 | while ((c = s.read()) != -1) |
10 | System.out.write(c); |
11 | s.close(); |
12 | } |
13 | } |
读写综合题:
读写下面的发票数据:
price unit description
19.99 12 Java T-shirt
9.99 8 Java Mug
STEP1:使DataOutputStream 和另一个输出流建立连接
1 | DataOutputStream out = new DataOutputStream(new FileOutputStream("invoice1.txt")); |
STEP2:用DataOutputStream专门的write方法将上述发票数据写到invoice1.txt
1 | for (int i = 0; i < prices.length; i ++) |
2 | { |
3 | out.writeDouble(prices[i]); //单价 |
4 | out.writeChar('/t'); |
5 | out.writeInt(units[i]); //数量 |
6 | out.writeChar('/t'); |
7 | out.writeChars(descs[i]); //描述 |
8 | out.writeChar('/n'); |
9 | } |
10 | out.close(); |
STEP3:基于刚生成的文件建立一个DataInputStream
1 | DataInputStream in = new DataInputStream(new FileInputStream("invoice1.txt")); |
STEP4:通过DataInputStream专门的read方法把数据读取回来
1 | try |
2 | { |
3 | while (true) |
4 | { |
5 | price = in.readDouble(); |
6 | in.readChar(); //throws out the tab |
7 | unit = in.readInt(); |
8 | in.readChar(); //throws out the tab |
9 | char chr; |
10 | desc = new StringBuffer(20); |
11 | while ((chr = in.readChar() != '/n') |
12 | { desc.append(chr); } |
13 | System.out.println("You've ordered " + unit + " units of " |
14 | + desc + " at $" + price); |
15 | total = total + unit * price; |
16 | } |
17 | } |
18 | catch (EOFException e) { } |
19 | System.out.println("For a TOTAL of: $" + total); |
20 | in.close(); |