Download presentation
Presentation is loading. Please wait.
Published byRaymond Pearson Modified over 9 years ago
1
Introduction to Java Files
2
Text Files A sequential collection of data stored on a permanent storage device Hard drive USB memory CD/DVD Has a name and location on computer’s file system … John 9 87 book 77.9 44 course … Current position
3
File Operations Open – Locate the file on computer’s file system – Position the read/write head at the beginning of the file Current position = beginning of the file … John 9 87 book 77.9 44 course … Current position
4
File Operations Open for input (reading) Open for output (writing) – If the file already exists, erase the file content … John 9 87 book 77.9 44 course … Current position
5
File Operations Read – At the current position – Read next string Returns book – Read next integer Error (next piece of data is not integer) … John 9 87 book 77.9 44 course … Current position
6
File Operations -- rules Open for input – The file must exist on the computer’s file system Error otherwise Open for output – If the file already exists, its content is erased Read – The type of the data item at current position should match the type of read instruction (i.e. nextInt, nextDouble, …) Error otherwise – Trying to read past the last piece of data (beyond end of file)will result in an error Write – Must close the file at the end so that it will be written back to the operating system’s file system
7
File Operations -- Java Open for input – The file must exist on the computer’s file system Error otherwise String fileName= "myFile.txt"; File inpFile = new File(fileName); Scanner in = new Scanner(inpFile); Error if "myFile.txt“ does not exist in the project folder
8
File Operations -- rules Open for output – If the file already exists, its content is erased String fileName= "myFile.txt"; PrintWriter outFile = new PrintWriter(fileName); – "myFile.txt“ will be erased, if it already exists.
9
File Operations -- rules Read – The type of the data item at current position should match the type of read instruction (i.e. nextInt, nextDouble, …) Error otherwise – inpFile.next(); » returns the next piece of data in the file as a string – inpFile.nextInt(); – returns the next piece of data in the file as an Integer. The next piece of data in the file must be an integer. – inpFile.nextDouble » returns the next piece of data in the file as a Double. The next piece of data in the file must be a Double.
10
File Operations -- rules Read – Must ensure that we do not attempt to read beyond the last piece of data in the file – inpFile.hasNext();
11
File Operations -- rules Write – Must close the file at the end so that it will be written back to the operating system’s file system Similar to a scanner – outFile.print(); – outFile.println(); – outFile.printf(); » See section 4.3.2 of the text book » See http://web.cerritos.edu/jwilson/SitePages/java_language _resources/Java_printf_method_quick_reference.pdf
12
File Operations -- rules Close – outFile.close();
13
File Operations -- rules Method that use file operations must have throws IOException in their headers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.