1 / 8

Brief Introduction to Java I/O

Brief Introduction to Java I/O. Introductory Points. Java I/O is primarily “stream-oriented.” Data flows into the program and out of the program, like a ‘stream.’ We have ‘input’ streams and ‘output’ streams. “Standard” I/O streams: System.in – defaults to the keyboard

Download Presentation

Brief Introduction to Java I/O

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. Brief Introduction to Java I/O

  2. Introductory Points • Java I/O is primarily “stream-oriented.” • Data flows into the program and out of the program, like a ‘stream.’ • We have ‘input’ streams and ‘output’ streams. • “Standard” I/O streams: • System.in – defaults to the keyboard • System.out – defaults to the monitor • System.err – defaults to where System.out is: monitor. • These are all public and static and found in the System class of the Java API.

  3. Standard I/O • We’ve used standard I/O in creating Scanner objects to process data normally associated with standard input (keyboard) and standard output (monitor). • “I/O exceptions” are handled for us by Scanner. • Many ‘opportunity for problems in doing any kind of input output: examples include files not defined; file not found; formats not what were expected, EOF early, etc. and other exceptions. • I/O in Java can be very complicated. • We need to be able to read text data or pure binary data. These can come from external files, memory, or from strings. • Here, we will emphasize input/output as it pertains to external files.

  4. The java I/O package • The java.io package provides us classes that let us define desired input and output streams. • Because we use the classes themselves (as in Class.method) the methods we use are clearly public and static. • Recall: static methods don’t need to be part of an object to use them, as in the Math class – recall: Math.sqrt() • (Math is the class; sqrt() is method and we simply say, Math.sqrt()…) • We did not have to instantiate an object of type Math in order to access the method! • Some classes provide for buffering (manipulate data in stream itself.) • What we do is to combine a number of these classes to lock in to exactly what we want. • The general topic of java I/O is huge and we will cover only what we need here.

  5. The java.io package • To restate: so many I/O operations cancause problems and `throw’ an “I/O Exception” when we are trying to do I/O operations such as read(). • We must either • ‘catch’ the exception and process it (use a try() … catch() sequence) or recognize that • Any method from the class that may catch a problem must be listed in a ‘throws’ clause in the header of the method where the I/O is attempted. • For now, we will elect this approach. • At this time, it is simpler…

  6. Code – for Output • Must include up top: import java.io.*; // this lets us use the classes below. • Alter your method header as follows: public static void main (String[ ] args) throws IOException. // you need this! • Sample code: (from book) String file = “test.dat”; // name your output file FileWriter fw = new FileWriter (file); // creates object fw of type FileWriter. BufferedWriter bw = new BufferedWriter (fw); // creates object bw of type ... PrintWriter outFile = new PrintWriter (bw); // creates object outFile of type .. • …. • outFile.print (value + “ “); // writes this as a stream. Try it! • outFile.println (); // prints a blank line in the file • …. outFile.close(); // at end, close the file • This writes to an output file.

  7. Code – for Input • Must include up top: import java.io.*; // this lets us use the classes below. • Alter your method header as follows: public static void main (String[ ] args) throws IOException. // you need this! • Sample code: • FileInputStream fis1 = new FileInputStream("Project2StatesInputFile.txt"); • BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1)); // read file • outputString = br1.readLine(); //read a line fis the file • while (outputString != null) { // whatever you want to do with the string input. • ……. outputString.substring(0, 15).trim() //read state name • …… outputString.substring(15, 30), //read capital • …… Integer.parseInt(outputString.substring(55))); //read region # • outputString = br1.readLine(); • }//end while loop • br1.close(); //Close Stateinput file being read • Inside loop, you may use StringTokenizer too. This is merely a loop going through the file of records.

  8. More Later

More Related