1 / 13

File I/O

File I/O. Topics. I/O concepts File class Reading character data using Scanner Writing character data using PrintWriter. Input/Output Concepts. data storage memory: transient, accessed directly by processor disk, tape: persistent, requires I/O operations I/O sources, destinations

cyndi
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. Topics • I/O concepts • File class • Reading character data using Scanner • Writing character data using PrintWriter

  3. Input/Output Concepts • data storage • memory: transient, accessed directly by processor • disk, tape: persistent, requires I/O operations • I/O sources, destinations • console, disk, tape, network, etc • streams • represent a sequential stream of bytes • abstract away the details of I/O devices

  4. Types of Data • character data • represented as 1-byte ASCII (256 characters) in most files in US (http://www.lookuptables.com/) • represented in 2-byte Unicode (65536 characters) within Java programs (http://www.unicode.org/) • the first 128 characters of Unicode are the ASCII character set • read with Reader subclasses (also java.util.Scannerin JDK1.5) • written with Writer subclasses • binary data • read with InputStream subclasses • written with OutputStream subclasses

  5. Representing a File • class java.io.File • represents a file or folder (directory) • constructor • public File (String fullPath) • public File (String path, String name) • interesting methods: • boolean exists() • boolean isDirectory() • boolean isFile() • boolean canRead() • boolean canWrite() • long length() • long lastModified()

  6. Representing a File (cont.) • more interesting methods: • boolean delete() • boolean renameTo(File dest) • boolean mkdir() • String[] list() • File[] listFiles() • String getName()

  7. Reading Character Data using Scanner java.util.Scanner • reads character data as Strings, or converts to primitive values • methods throw nochecked exceptions • constructors • public Scanner (File source) // reads from a file • public Scanner (InputStream source) // reads from a stream • public Scanner (String source) // scans a String • interesting methods: • boolean hasNext() • boolean hasNextInt() • boolean hasNextDouble() • … • String next() // returns Object in UWT version • String nextLine() • int nextInt() • double nextDouble()

  8. Reading Character Data using Scanner Read doubles from a file: // set up scanner File f = new File ("Doubles.txt"); Scanner sc = null; try { sc = new Scanner (f); } catch (FileNotFoundException e) { System.out.println ("File not found " + e.getMessage()); System.exit (1); } // read values from file while (sc.hasNextDouble()) { double d = sc.nextDouble(); processValue (d); }

  9. Writing Character Data using PrintWriter • java.io.PrintWriterextends Writer • converts data to strings for output • methods throw no exceptions • constructors • public PrintWriter (Writer writer) • public PrintWriter (File file) // JDK1.5 • public PrintWriter (String fileName) // JDK1.5 • interesting additional methods: • void print(boolean b) //outputs a boolean as a string • void print(int i) //outputs an int as a string • void print(double d) //outputs a double as a string • void print(String s) //outputs a String as a string • void print(Object o) //outputs an Object as a string • etc • also println(…) versions

  10. Writing Character Data using PrintWriter • interesting methods (continued) • void flush() throws IOException // forces output • void close() throws IOException // flushes • example (JDK1.5): PrintWriter pwrtr = new PrintWriter ( new File (“Grades.txt”)); String name = “Smith”; int credits = 90; double grade = 2.0; pwrtr.print (name + “ “); pwrtr.print (credits + “ “); pwrtr.println (grade); pwrtr.close();

  11. Using PrintWriter in JDK1.4 • The constructor public PrintWriter (Writer writer) requires a Writer (subclass) as a parameter • We can use a FileWriter object for this purpose • FileWriter constructors: • FileWriter (File file) • FileWriter (File file, boolean append) • FileWriter (String file) • FileWriter (String file, boolean append) • Example: PrintWriter pwrtr = new PrintWriter ( new FileWriter (“Grades.txt”));

  12. import java.io.*; /** * Reads lines from the keyboard and writes * them to a file (JDK1.5). */ public class FileIOExample { public static void main (String[] args) { String outfile = "Output.txt"; // get file name from command line if (args.length > 0) outfile = args[0]; // create keyboard reader Scanner rdr = new Scanner (System.in);

  13. // create file writer PrintWriter wrtr =null;//why declared here? //why null? try { wrtr = new PrintWriter (outfile); } catch (FileNotFoundException e) { System.out.println ( "Cannot create file " + outfile + ": " + e.getMessage()); System.exit (1); } String line; System.out.println ( "Enter lines (<Enter> to quit):"); // read strings from keyboard while((line=rdr.nextLine()).length() > 0 ){ wrtr.println (line); // write to file } wrtr.close(); // don't forget this! } }

More Related