80 likes | 176 Views
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
E N D
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.
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.
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.
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…
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.
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.