-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOStreamDemo.java
More file actions
77 lines (63 loc) · 1.6 KB
/
Copy pathIOStreamDemo.java
File metadata and controls
77 lines (63 loc) · 1.6 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.chapter11;
import java.io.*;
public class IOStreamDemo
{
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new FileReader("IOStreamDemo.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
{
s2 += s + "\n";
}
in.close();
//input from standard.
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("input a line:");
System.out.println(stdin.readLine());
//input from memory
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.print((char)c);
//format memory input
//×Ö½ÚÁ÷, byte
try
{
DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(s2.getBytes()));
while(true)
{
System.out.println((char)in3.readByte());
}
}
catch (EOFException e)
{
// TODO: handle exception
System.err.println("end of stream");
}
//file output
try
{
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("demo.out")));
PrintWriter out2 = new PrintWriter(new File("demo1.out"));
PrintWriter out3 = new PrintWriter(new FileWriter("demo3.out"));
int lineNumber = 1;
while ((s = in4.readLine()) != null)
{
out1.println(lineNumber++ + ":" + s);
}
out1.close();
}
catch (EOFException e)
{
// TODO: handle exception
System.err.println("end of stream");
}
//storing and recovering data
}
}