130 likes | 243 Views
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
E N D
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 • console, disk, tape, network, etc • streams • represent a sequential stream of bytes • abstract away the details of I/O devices
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
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()
Representing a File (cont.) • more interesting methods: • boolean delete() • boolean renameTo(File dest) • boolean mkdir() • String[] list() • File[] listFiles() • String getName()
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()
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); }
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
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();
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”));
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);
// 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! } }