Presentation is loading. Please wait.

Presentation is loading. Please wait.

EEC 484/584 Computer Networks

Similar presentations


Presentation on theme: "EEC 484/584 Computer Networks"— Presentation transcript:

1 EEC 484/584 Computer Networks
Java Tutorial #2: Java IO Wenbing Zhao Cleveland State University Materials taken from:

2 EEC484/584 Computer Networks
Java IO To do Java IO, need to Import java.io.* System InputStreamReader BufferedReader File BufferedWriter FileReader FileWriter 6/4/2019 EEC484/584 Computer Networks

3 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 }; } 6/4/2019 EEC484/584 Computer Networks

4 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 }; } 6/4/2019 EEC484/584 Computer Networks

5 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)); 6/4/2019 EEC484/584 Computer Networks

6 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 6/4/2019 EEC484/584 Computer Networks

7 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. 6/4/2019 EEC484/584 Computer Networks

8 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); 6/4/2019 EEC484/584 Computer Networks

9 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(); 6/4/2019 EEC484/584 Computer Networks

10 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();   } } 6/4/2019 EEC484/584 Computer Networks


Download ppt "EEC 484/584 Computer Networks"

Similar presentations


Ads by Google