-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathJavaIOExample.java
143 lines (126 loc) · 3.95 KB
/
JavaIOExample.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package IO_example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaIOExample {
private static final String INPUT = "/Users/SteveSun/Downloads/word_dict.txt";
// "/tmp/input.txt";
private static final String OUTPUT = "/tmp/output.txt";
public static void main(String[] args) throws IOException {
byteStreamsIO();
// characterStreamsIO();
// standardStreamsIO();
}
/**
* Java byte streams are used to perform input and output of 8-bit bytes.
* Though there are many classes related to byte streams but the most
* frequently used classes are: FileInputStream and FileOutputStream.
* Following is an example which makes use of these two classes to copy an
* input file into an output file:
*
* @throws IOException
*/
static void byteStreamsIO() throws IOException {// This type is
// package-level access,
// without public, protected
// and private keywords,
// this is package-level
// method
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(INPUT);
fos = new FileOutputStream(OUTPUT);
int c;
while ((c = fis.read()) != -1) {
fos.write(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
/**
* Java Byte streams are used to perform input and output of 8-bit bytes,
* where as Java Character streams are used to perform input and output for
* 16-bit unicode. Though there are many classes related to character
* streams but the most frequently used classes are: FileReader and
* FileWriter Though internally FileReader uses FileInputStream and
* FileWriter uses FileOutputStream but here major difference is that
* FileReader reads two bytes at a time and FileWriter writes two bytes at a
* time.
*
* @throws FileNotFoundException
* @throws IOException
*/
static void characterStreamsIO() throws FileNotFoundException, IOException {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader(INPUT);
out = new FileWriter(OUTPUT);
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
/**
* All the programming languages provide support for standard I/O where
* user's program can take input from a keyboard and then produce output on
* the computer screen. If you are aware if C or C++ programming languages,
* then you must be aware of three standard devices STDIN, STDOUT and
* STDERR. Similar way Java provides following three standard streams
*
* Standard Input: This is used to feed the data to user's program and
* usually a keyboard is used as standard input stream and represented as
* System.in.
*
* Standard Output: This is used to output the data produced by the user's
* program and usually a computer screen is used to standard output stream
* and represented as System.out.
*
* Standard Error: This is used to output the error data produced by the
* user's program and usually a computer screen is used to standard error
* stream and represented as System.err.
*
* Following is a simple program which creates InputStreamReader to read
* standard input stream until the user types a "q":
*
* @throws IOException
*/
static void standardStreamsIO() throws IOException {
InputStreamReader isr = null;
try {
isr = new InputStreamReader(System.in);
System.out.println("Enter characters, 'q' to quit.");
char c;
do {
c = (char) isr.read();
System.out.print(c);
} while (c != 'q');
} finally {
if (isr != null) {
isr.close();
}
System.out.println("\nProgram stopped.");
}
}
}