- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharIO2.java
More file actions
Latest commit
27 lines (25 loc) · 1004 Bytes
/
Copy pathCharIO2.java
File metadata and controls
27 lines (25 loc) · 1004 Bytes
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
// File I/O using line streams (Oracle Java Tutorials)
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.BufferedReader;
importjava.io.PrintWriter;
importjava.io.IOException;
classCharIO2 {
publicstaticvoidmain(String[] args) {
// BufferedReader and PrintWriter can be used to operate on
// entire lines instead of single characters
// NOTE: above classes are used to wrap character stream objects
// below code requires xanadu.txt, to be found here:
// https://docs.oracle.com/javase/tutorial/essential/io/streams.html
try(BufferedReaderinStream = newBufferedReader(newFileReader("xanadu.txt"));
PrintWriteroutStream = newPrintWriter(newFileWriter("outfile.txt"));) {
Stringline;
// character buffer is represented as a String object
while ((line = inStream.readLine()) != null)
outStream.println(line);
} catch (IOExceptionaExc) {
System.out.println("An exception has occurred: " + aExc);
aExc.printStackTrace();
}
}
}