1 / 10

EEC 484/584 Computer Networks

EEC 484/584 Computer 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

rubytucker
Download Presentation

EEC 484/584 Computer Networks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

More Related