Very Brief Introduction to Java IO with Buffered

Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer

Introductory Points n 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. n “Standard” I/O streams: ¨ System. in – defaults to the keyboard ¨ System. out – defaults to the monitor ¨ System. err – defaults to where System. out n is: monitor. These are all public and static and found in the System class of the Java API.

Standard I/O n We’ve used standard I/O in creating Scanner objects to process data normally associated with standard input (keyboard) and standard output (monitor). n “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. ¨ n We oftentimes 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 pertains to external files.

The java I/O package n 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 public and static. n n n 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! (note capitalized class name followed by member name) Recall: in die 1. roll(), die 1 was an object; hence object. method() call…. Some classes provide for buffering (manipulate data in stream itself. ) ¨ Like, we can change format and more (ahead) n What we do is to combine a number of these classes to lock in to exactly what we want to do our input/output. n The general topic of java I/O is huge and we will cover only what we need here.

The java. io package n To restate: so many I/O operations can cause problems and `throw’ an “I/O Exception” when we are trying to do I/O operations such as read() or write() operations. n We must either Use a try…catch combination: n ¨ ‘catch’ the exception in a ‘try’ block n (use a try() … catch() sequence) or n and process it Recognize that any method that may catch a problem must have a ‘throws’ clause in the header of the method where the I/O is attempted. n n For now, we will elect this approach. At this time, it is simpler…

Very Simplified I/O – for now We will just use: throws IOException clause for File I/O. n For much more, see Chapter 10 slides. n

Code – for Input import java. io. *; // You need this to access the classes cited below. public static void main (String[ ] args) throws IOException. //you need the “throws. ” Why? ? You must also // 1. download (right click, Save Target As) your input file (from my web page), and // 2. put it (drag it) into your project folder so the program can find it and read lines from it. File. Input. Stream fis 1 = new File. Input. Stream(“Countries. txt"); // much more in Chapter 10 Buffered. Reader br 1 = new Buffered. Reader(new Input. Stream. Reader(fis 1)); // for an input file to be read// // or File. Reader fr = new File. Reader(“Countries. txt”); Buffered. Reader br = new Buffered. Reader (fr); String input. String; String country. Name String country. Whatever…. // additional attributes here… input. String = br 1. read. Line(); //reads one complete line (record) from the input file into input. String. // Now you need to get to the parts of the input. String. We call this ‘parsing’ the input. Consider: while (input. String != null) // looks at entire string you just read into input. String { country. Name = input. String. substring(0, 15). trim() //read country name country. Whatever = input. String. substring(15, 30), //read country capital country. Populatiion = Integer. parse. Int(input. String. substring(55, 60)); //read country code # // Note: these are not the precise character positions in ‘your’ file. // You will need to check appropriate attribute field lengths. // Look in your book about substring method. Echo print everything to verify!!! // <other code – like creating an object from these attributes. > input. String = br 1. read. Line(); // read next input line. } //end while loop

With a few more comments: File. Reader fr = new File. Reader(“Countries. Small. txt”); Buffered. Reader br = new Buffered. Reader (fr); // Sample names made up to show the parsing works: String input. String; String some. Name, some. Capital; // but declare one attribute per line. Int some. Pop; // Note: this has nothing to do with creating objects or the array. // Only ensures you are able to access the individual attributes from each input line. // Once you verify all this, you can comment out the print lines. input. String = br 1. read. Line(); //reads one complete line (record) from the input file into input. String. // Now you need to get to the parts of the input. String. We call this ‘parsing’ the input. Consider: while (input. String != null) // looks at entire string you just read into input. String { some. Name = input. String. substring(0, 15). trim() //read country name // echo print to verify you ‘read in and parse what you think you did. System. out. println (“ some Name read in was: “ + some. Name); some. Capital = input. String. substring(15, 30), //read country capital System. out. println (“some Capital read in was: “ + some. Capital); some. Pop = Integer. parse. Int(input. String. substring(55, 62)); //read country code # // Statements like these are used to extract specific positions from the input record. // Note: these are not the precise character positions in ‘your’ file. // Look in your book about substring method. Echo print everything to verify!!! …. Other code (later: create object and move into array…. Later!) // <other code – like creating an object from these attributes. > input. String = br 1. read. Line(); // read next input line. } //end while loop

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

More Later In using a standard IDE such as Net. Beans, your input file needs to be in your project package so your program can find it. n Be certain to move it (drag it) into your package and test accessing the file from your package before you zip up your project and send it to me. n
- Slides: 10