Introduction to Java Files Text Files A sequential

  • Slides: 13
Download presentation
Introduction to Java Files

Introduction to Java Files

Text Files • A sequential collection of data stored on a permanent storage device

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 …

File Operations • Open – Locate the file on computer’s file system – Position

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 … Current position John 9 87 book 77. 9 44 course …

File Operations • Open for input (reading) … John 9 87 book 77. 9

File Operations • Open for input (reading) … John 9 87 book 77. 9 44 course … Current position • Open for output (writing) – If the file already exists, erase the file content Current position

File Operations • Read – At the current position … John 9 87 book

File Operations • Read – At the current position … John 9 87 book 77. 9 44 course Current position – Read next string • Returns book – Read next integer • Error (next piece of data is not integer) …

File Operations -- rules • Open for input – The file must exist on

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. next. Int, next. Double, …) • 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

File Operations -- Java • Open for input – The file must exist on

File Operations -- Java • Open for input – The file must exist on the computer’s file system • Error otherwise String file. Name= "my. File. txt"; File inp. File = new File(file. Name); Scanner in = new Scanner(inp. File); • Error if "my. File. txt“ does not exist in the project folder

File Operations -- rules • Open for output – If the file already exists,

File Operations -- rules • Open for output – If the file already exists, its content is erased String file. Name= "my. File. txt"; Print. Writer out. File = new Print. Writer(file. Name); – "my. File. txt“ will be erased, if it already exists.

File Operations -- rules • Read – The type of the data item at

File Operations -- rules • Read – The type of the data item at current position should match the type of read instruction (i. e. next. Int, next. Double, …) • Error otherwise – inp. File. next(); » returns the next piece of data in the file as a string – inp. File. next. Int(); – returns the next piece of data in the file as an Integer. The next piece of data in the file must be an integer. – inp. File. next. Double » returns the next piece of data in the file as a Double. The next piece of data in the file must be a Double.

File Operations -- rules • Read – Must ensure that we do not attempt

File Operations -- rules • Read – Must ensure that we do not attempt to read beyond the last piece of data in the file – inp. File. has. Next();

File Operations -- rules • Write – Must close the file at the end

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 – out. File. print(); – out. File. println(); – out. File. printf(); » See section 4. 3. 2 of the text book » See http: //web. cerritos. edu/jwilson/Site. Pages/java_language _resources/Java_printf_method_quick_reference. pdf

File Operations -- rules • Close – out. File. close();

File Operations -- rules • Close – out. File. close();

File Operations -- rules • Method that use file operations must have throws IOException

File Operations -- rules • Method that use file operations must have throws IOException in their headers