DOC PREVIEW
UNF COP 2551 - Very Brief Introduction to Java IO

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 10.0 Very Brief Introduction to Java I/OIntroductory PointsStandard I/OThe java I/O packageThe java.io packageVery Simplified I/O – for nowCode – for InputCode – for OutputMore LaterChapter 10.0Very Brief Introduction to Java I/OIntroductory 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 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 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 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!Recall: in die1.roll(), die1 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)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 can cause problems and `throw’ an “I/O Exception” when we are trying to do I/O operations such as read() or write() operations.We must either Use a try…catch combination:‘catch’ the exception in a ‘try’ block and process it (use a try() … catch() sequence) or Recognize that any method 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…Very Simplified I/O – for nowWe will just use: throws IOException clause for File I/O.For much more, see Chapter 10 slides.Code – for Input Must include up top: import java.io.*; // this lets us use the classes below. public static void main (String[ ] args) throws IOException. // you need the throws. You must also download (right click, Save Target As) your input file and put it into your project folder so the program can find it and read lines from it FileInputStream fis1 = new FileInputStream(“Countries.Small.txt"); // much more in Chapter 10 BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1)); // read file//// or // FileReader fr = new FileReader(“Countries.Small.txt”); // BufferedReader br = new BufferedReader (fr); String inputString; inputString = br1.readLine(); //reads one complete line (record) from the input file into inputString. // Now you need to get to the parts of the inputString. We call this ‘parsing’ the input. Consider: while (inputString != null) { inputString.substring(0, 15).trim() //read country name inputString.substring(15, 30), //read country capital Integer.parseInt(inputString.substring(55))); //read country code # // Note: these are not the precise character positions. You will need to check. // look in your book about substring method. Echo print evertying to be sure!!! // <other code – like creating an object from these attributes.> inputString = br1.readLine(); // read next input line. } //end while loop br1.close(); //Close input file being readinside loop, you may use StringTokenizer too. This is merely a loop going through the file of records.Code – for OutputMust include up top: import java.io.*; // 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!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 .. …. <other code> outFile.print (value + “ “); // writes this as a stream. Try it! outFile.println (); // prints a blank line in the file … <other code> outFile.close(); // at end, close the fileThis writes to an output file.More LaterIn using a standard IDE such as NetBeans, your input file needs to be in your project package so your program can find it.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


View Full Document

UNF COP 2551 - Very Brief Introduction to Java IO

Download Very Brief Introduction to Java IO
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Very Brief Introduction to Java IO and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Very Brief Introduction to Java IO 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?