📄 iodemo.out
字号:
1: //: c11:IOStreamDemo.java
2: // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
3: // www.BruceEckel.com. See copyright notice in CopyRight.txt.
4: // Typical I/O stream configurations.
5: import java.io.*;
6:
7: public class IOStreamDemo {
8: // Throw exceptions to console:
9: public static void main(String[] args)
10: throws IOException {
11: // 1. Reading input by lines:
12: BufferedReader in =
13: new BufferedReader(
14: new FileReader("IOStreamDemo.java"));
15: String s, s2 = new String();
16: while((s = in.readLine())!= null)
17: s2 += s + "\n";
18: in.close();
19:
20: // 1b. Reading standard input:
21: BufferedReader stdin =
22: new BufferedReader(
23: new InputStreamReader(System.in));
24: System.out.print("Enter a line:");
25: System.out.println(stdin.readLine());
26:
27: // 2. Input from memory
28: StringReader in2 = new StringReader(s2);
29: int c;
30: while((c = in2.read()) != -1)
31: System.out.print((char)c);
32:
33: // 3. Formatted memory input
34: try {
35: DataInputStream in3 =
36: new DataInputStream(
37: new ByteArrayInputStream(s2.getBytes()));
38: while(true)
39: System.out.print((char)in3.readByte());
40: } catch(EOFException e) {
41: System.err.println("End of stream");
42: }
43:
44: // 4. File output
45: try {
46: BufferedReader in4 =
47: new BufferedReader(
48: new StringReader(s2));
49: PrintWriter out1 =
50: new PrintWriter(
51: new BufferedWriter(
52: new FileWriter("IODemo.out")));
53: int lineCount = 1;
54: while((s = in4.readLine()) != null )
55: out1.println(lineCount++ + ": " + s);
56: out1.close();
57: } catch(EOFException e) {
58: System.err.println("End of stream");
59: }
60:
61: // 5. Storing & recovering data
62: try {
63: DataOutputStream out2 =
64: new DataOutputStream(
65: new BufferedOutputStream(
66: new FileOutputStream("Data.txt")));
67: out2.writeDouble(3.14159);
68: out2.writeChars("That was pi\n");
69: out2.writeBytes("That was pi\n");
70: out2.close();
71: DataInputStream in5 =
72: new DataInputStream(
73: new BufferedInputStream(
74: new FileInputStream("Data.txt")));
75: BufferedReader in5br =
76: new BufferedReader(
77: new InputStreamReader(in5));
78: // Must use DataInputStream for data:
79: System.out.println(in5.readDouble());
80: // Can now use the "proper" readLine():
81: System.out.println(in5br.readLine());
82: // But the line comes out funny.
83: // The one created with writeBytes is OK:
84: System.out.println(in5br.readLine());
85: } catch(EOFException e) {
86: System.err.println("End of stream");
87: }
88:
89: // 6. Reading/writing random access files
90: RandomAccessFile rf =
91: new RandomAccessFile("rtest.dat", "rw");
92: for(int i = 0; i < 10; i++)
93: rf.writeDouble(i*1.414);
94: rf.close();
95:
96: rf =
97: new RandomAccessFile("rtest.dat", "rw");
98: rf.seek(5*8);
99: rf.writeDouble(47.0001);
100: rf.close();
101:
102: rf =
103: new RandomAccessFile("rtest.dat", "r");
104: for(int i = 0; i < 10; i++)
105: System.out.println(
106: "Value " + i + ": " +
107: rf.readDouble());
108: rf.close();
109: }
110: } ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -