Download presentation
Presentation is loading. Please wait.
Published byMargaretMargaret Gilbert Modified over 9 years ago
2
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes two different programs need to share the same data, so the data must be persistent. Files Persistent data is stored in a file. Files are stored by devices: The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
3
A directory is a mechanism for grouping files or other directories. (synonym for directory: ___________) A file system is a hierarchy of directories. A complete file ___________ includes all directories from the root. /System/Applications/Mail.app The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
4
Most operating systems/shells maintain a current directory. This permits the use of a relative path name to identify files. The relative path name for the circled file is... Documentation/read.me, when the current directory is ____________________. read.me, when the current directory is/System/Developer/Documentation. _________________________________, when the current directory is /System.
5
The standard Java classes for manipulating files are found in java.io.* Terminology The primary class for manipulating directory information is java.io.File. File - String fileName «constructor» + File( String )... «query» + boolean canRead() + boolean canWrite() + boolean isFile() + boolean isDirectory() + boolean exists() + String getName() + int length()... «update» + boolean createNewFile() + boolean delete() + boolean mkdir()... _______________________ refer to transfering information from the file to the program. _______________________ refer to transfering information from the program to the file. ________ is short for input and/or output The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
6
Example (create a new directory) File - String fileName «constructor» + File( String )... «query» + boolean canRead() + boolean canWrite() + boolean isFile() + boolean isDirectory() + boolean exists() + String getName() + int length()... «update» + boolean createNewFile() + boolean delete() + boolean mkdir()... try { File dir; boolean itWorked; dir = new File( "thisDirectory" ); if (dir.exists()) { System.out.println(“File exists!”); } else { itWorked = dir.mkdir(); if (itWorked) { System.out.println(“Directory ” + “created.”); } } catch (IOException e) { System.out.println( “I/O exception” ); } Notes IOExceptions must be handled for I/O. A complete path name begins with “/”. This is a relative name. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
7
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. Input However, the File class isn’t useful for reading or writing file content. Instead, streams are needed. Executing program input stream file (on disk) Executing program output stream file (on disk) Output The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
8
1) Open the file for output by instantiating the appropriate streams. 2) 3) 4) Basic Algorithm 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" ); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
9
1) Open the file for input by instantiating the appropriate streams. 2) 3) Basic Algorithm Example try { double val1; int val2; char val3; FileInputStream fStream; DataInputStream inStream; 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. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
10
Some files encode all data in textual format (i.e., a sequence of characters). Such files are called ________ files. DataInputStream / DataOutputStream process binary files. Other files store data in its native form (raw bytes of data). Such files are called _________ files. BufferedReader / PrintWriter process text files. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
11
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" ); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
12
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.) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
13
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.) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.