1 / 11

File I/O

File I/O. Streams. Executing program. Executing program. input stream. output stream. file (on disk). file (on disk). The File class can be used to create a file or directory, check for the existence of a file/directory, examine properties of files/directories.

alma-savage
Download Presentation

File 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. File I/O

  2. Streams Executing program Executing program input stream output stream file (on disk) file (on disk) The File class can be used to create a file or directory, check for the existence of a file/directory, examine properties of files/directories. However, the File class isn’t useful for reading or writing file content. Instead, streams are needed. Input Output

  3. Writing to a Stream Basic Algorithm 1) Open the file for output by instantiating the appropriate streams. 2) Call write methods to transfer data 3) Flush the stream 4) Close the stream Example try { FileOutputStream fStream; DataOutputStream writingStream; fStream = new FileOutputStream( "oneFile.bin" ); writingStream = new DataOutputStream( fStream ); writingStream.writeDouble( 3.14159 ); writingStream.writeInt( 7 ); writingStream.writeChar("Z"); writingStream.flush(); writingStream.close(); } catch (IOException e) { System.out.println( "I/O exception" ); }

  4. Reading from a Stream Basic Algorithm 1) Open the file for input by instantiating the appropriate streams. 2) Call read methods(non-void) to transfer data 3) Close the stream Example try { double val1; intval2; char val3; FileInputStreamfStream; DataInputStreaminStream; fStream = new FileInputStream( "oneFile.bin” ); inStream = new DataInputStream( fStream ); val1 = inStream.readDouble(); val2 = inStream.readInt(); val3 = inStream.readChar(); inStream.close(); } catch (IOException e) { System.out.println( "I/O exception" ); } Warning: Data should be input in the same order as it was output. (In this case first a double, then an int, then a char.

  5. Binary & Text Files Some files encode all data in textual format (i.e., a sequence of characters). Such files are called text files. Other files store data in its native form (raw bytes of data). Such files are called binary files. DataInputStream / DataOutputStreamprocess binary files. BufferedReader / PrintWriterprocess text files.

  6. Writing Text The following code creates a text file called “threeLines.txt” and writes three lines into it. try { FileOutputStream fStream; PrintWriter myStream; fStream = new FileOutputStream( "threeLines.txt" ); myStream = new PrintWriter( fStream ); myStream.println( "This is line #1." ); myStream.println( "This is line #2." ); myStream.print( "This is the start," ); myStream.println( " and this is the end of line #3." ); myStream.flush(); myStream.close(); } catch (IOException e) { System.out.println( "I/O exception" ); }

  7. Reading Lines The following code reads all lines from a file called “threeLines.txt”. try { FileReader fReader; BufferedReader reader; String line; fReader = new FileReader( "threeLines.txt” ); reader = new BufferedReader( fReader ); line = reader.readLine(); while ( line != null ) { System.out.println( line ); //echoes input to standard out line = reader.readLine(); } reader.close(); } catch (IOException e) { System.out.println( "I/O exception" ); } (Note that FileReader, not FileInputStream, is required by BufferedReader.)

  8. Reading Characters The following code reads each character from a file called “threeLines.txt”. try { FileReader fReader; BufferedReader reader; int charAsInt; fReader = new FileReader( "threeLines.txt” ); reader = new BufferedReader( fReader ); charAsInt = reader.read(); while ( charAsInt != -1 ) { System.out.println( (char)charAsInt ); charAsInt = reader.read(); } reader.close(); } catch (IOException e) { System.out.println( "I/O exception" ); } (Note that each char is read as an int and must be cast.)

  9. java.util.Scanner «constructor» + Scanner( String ) + Scanner( InputStream ) «query» + boolean nextBoolean( ) + byte nextByte() + double nextDouble() + float nextFloat() + int nextInt() + String nextLine() + long nextLong() + short nextShort() + String next() «predicate» + boolean hasNext( ) + boolean hasNextBoolean( ) + boolean hasNextByte() + boolean hasNextDouble() + boolean hasNextFloat() + boolean hasNextInt() + boolean hasNextLong() + boolean hasNextShort() . . . Data Translation What is the difference between text files and binary files? How can you programmatically translate binary data into text? How can you programmatically translate textual data into binary?

  10. }

More Related