1 / 11

Files and Input/Output

Files and Input/Output. Fall 2007 IST 311 / MIS 307. Files. File – collection of related records Student file Customer file Several types of file organization: Sequential Random Database. Streams. Streams provide communication of data at the byte level Used for reading or writing

wannie
Télécharger la présentation

Files and Input/Output

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. Files and Input/Output Fall 2007 IST 311 / MIS 307

  2. Files • File – collection of related records • Student file • Customer file • Several types of file organization: • Sequential • Random • Database

  3. Streams • Streams provide communication of data at the byte level • Used for reading or writing • Streams for reading inherit from a common superclass – java.io.InputStream class • Streams for writing inherit from the common superclass – java.io.OutputStream

  4. Files • There are two types of files in Java: • Binary files – processed as a sequence of bytes • Data is not very portable due to compatibility issues between different computer operating systems • Java binary files are platform independent; binary files created by a Java program can be interpreted by Java programs on any platform • Text files – processed as a sequence of characters • Portable files using ASCII code • Can be read and written by most text-processing programs

  5. Streams • Java has a wide variety of streams for performing I/O • Defined in the java.io package which must be imported to perform any I/O • import java.io.*; • Generally, binary files are processed by subclasses of InputStream and OutputStream • Text files are processed by subclasses of FileReader and PrintWriter

  6. Reading from a File Use a 3 step algorithm • Connect an input stream to a file • Causes the file to open BufferedReaderinFile; inFile = newBufferedReader (new FileReader("books.txt")); • Read data using a loop while (value != null) • Close the input stream (file) when finished inFile.close( );

  7. Reading from a File • If your file is not in the same folder as the Java program, specify the file directory: …..(new FileReader ( “a:\\data.txt”)); • If the input file does not exist, statements associated with it fail • throws a FileNotFoundException

  8. Reading from a File • Remember that BufferedReader • throws an IOException

  9. Writing to a File Use a 3 step algorithm • Connect an output stream to a file • Causes the file to open • If the file already exists, opening the file will destroy any data it previously held • If the file doesn’t exist, it will be created PrintWriter outFile; outFile = new PrintWriter(new FileWriter("report.txt"));

  10. Writing to a File • Write data to the output stream, which passes it on to the file outFile.println(title); • Close the output stream when finished outFile.close();

  11. Lab • Simple program to read from a file • Simple program to write to a file

More Related