100 likes | 117 Views
Learn about Java input-output operations utilizing classes like InputStreamReader, BufferedReader, FileWriter, and more. Practice file handling and stream reading.
E N D
EEC 484/584Computer Networks Java Tutorial #2: Java IO Wenbing Zhao Cleveland State University wenbing@ieee.org Materials taken from: http://www.cosc.brocku.ca/mentor/javaio_tutorial
Java IO To do Java IO, need to Import java.io.* • System • InputStreamReader • BufferedReader • File • BufferedWriter • FileReader • FileWriter EEC484/584 Computer Networks
System • System.in: standard input (of type: InputStream) • System.out: standard output import java.io.*; public class TrivialApplication { public static void main ( String args[] ) throws IOException{ //reads one byte as an integer from standard in int ch = System.in.read(); System.out.println((char)ch); // print to standard out }; } EEC484/584 Computer Networks
InputStreamReader • InputStreamReader: a bridge from byte streams to character streams, i.e., it reads bytes and decodes them into characters • Constructor: InputStreamReader(InputStream in) • Methods: close(); read(); etc. import java.io.*; public class TrivialApplication { public static void main ( String args[] ) throws IOException{ InputStreamReader input = new InputStreamReader(System.in); int ch = input.read(); System.out.println((char)ch); // print to standard out }; } EEC484/584 Computer Networks
BufferedReader • BufferedReader: is used to improve IO efficiency, i.e., it may read more bytes from the underlying stream than necessary to satisfy current read operation • Constructor: BufferedReader(Reader in) • Methods: close(); read(); readLine(); etc. BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); EEC484/584 Computer Networks
File • File: used to create a file with designated filename File inputFile = new File("testFile.txt"); // relative path, in current working directory File outputFile = new File("/home/users/2p92/outFile.txt"); // absolute path, separators OS dependent EEC484/584 Computer Networks
BufferedWriter • Make write operation more efficient: allows for one writing step by providing data output storage which can then be outputted efficiently • Constructor: BufferedWriter(Writer out); • Methods: close(); flush(); newLine(); write(int c), etc. EEC484/584 Computer Networks
FileReader • Allows an application to gain access to character input from file • Constructor: FileReader(File file) • Methods: close(); read(); etc. File inputFile = new File("test.dat"); FileReader Joe = new FileReader(inputFile); EEC484/584 Computer Networks
FileWriter • Allows an application to place character output in the designated file • Constructor: FileWriter(File file); • Methods: close(); flush(); write(); etc. File inputFile = new File("test.dat"); FileReader Joe = new FileReader(inputFile); File outputFile = new File("out.dat"); FileWriter out = new FileWriter(outputFile); int c; While ((c = joe.read() !=-1){ out.write(c); } joe.close(); out.close(); EEC484/584 Computer Networks
A Useful Example public class IOTest { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader(new File("iotest.dat"))); BufferedWriter bw = new BufferedWriter(new FileWriter(new File("iotestout.dat"))); while(true) { String s = br.readLine(); if(null == s) break; bw.write("Out: "+s); bw.newLine(); } bw.flush(); br.close(); bw.close(); } } EEC484/584 Computer Networks