e!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> CISC 1115 Files

CISC 1115
Introduction to Programming Using Java
Files


Reading from the Text

  • The text does not discuss files.
  • Files

    Files vs Streams

    Until now, we've only dealt with System.out and System.in which are called stream. Files on the other hand are more permanent data sources that reside on disk; they can be opened repeatedly.

    Files and Scanner

    Notes

    FileNotFoundException at Compilation and Execution

    P05.1 Rounder Write a program that illustrates the Math.round method by reading in a header value (i.e., number of data items to read in) from the file data.text followed by that number of floating point numbers (doubles). For each number read in, print out the number and it's rounded value using Math.round.
    Code
    

    import java.io.*; import java.util.*; public class Rounder { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("data.text")); int howMany = scanner.nextInt(); for (int i = 1; i <= howMany; i = i + 1) { double d = scanner.nextDouble(); System.out.println(d + " rounds to " + Math.round(d)); } } }

    data.text
    

    6 3.2 5.7 4.5 9.0 12 16.4

    Stdout
    

    3.2 rounds to 3 5.7 rounds to 6 4.5 rounds to 5 9.0 rounds to 9 12.0 rounds to 12 16.4 rounds to 16

    The next and nextLine Methods

  • Thus, if one has a data item containing blanks — for example a street address:
    2900 Bedford Avenue
    		
    or a full (i.e., first and last) name:
    Gerald Weiss
    		
    the individual elements (e.g. the house number (2900), street name (Bedford), street type, (Avenue, first name (Gerald), and last name (Weiss) could be Note:

    Prompting for the Name of the File — Multiple Scanners

    P5.2 Finder Write a program that prompts the user for a file name and an integer, and if the integer appears as one of the numbers in the file, the program outputs *** Found it!;
    Code
    

    import java.io.*; import java.util.*; public class Finder { public static void main(String [] args) throws Exception { Scanner keyboard = new Scanner(System.in); System.out.print("Which file would you like to search? "); String filename = keyboard.next(); System.out.print("Which number would you like to find? "); int theNumber = keyboard.nextInt(); Scanner dataFile = new Scanner(new File(filename)); int n = dataFile.nextInt(); // get number of numbers in file for (int i = 1; i <= n; i = i + 1) { int num = dataFile.nextInt(); if (theNumber == num) System.out.println("*** Found it!"); } } }

    Stdin
    

    data.text 4

    data.text
    

    3 2 4 6

    Stdout
    

    Which file would you like to search? Which number would you like to find? *** Found it!

    Interactive Session

    Here is a sample execution of the program.
    User input is in bold.
    Which file would you like to search? data.text
    Which number would you like to find? 4
    *** Found it!

    Writing Output to a File — the PrintStream Class

    P05.3 Rounder

    Rewrite P05.1 so that the output is written to the file data.output (rather than the screen)

    Code
    

    import java.io.*; import java.util.*; public class Rounder { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("data.text")); PrintStream ps = new PrintStream("data.output"); int howMany = scanner.nextInt(); for (int i = 1; i <= howMany; i = i + 1) { double d = scanner.nextDouble(); ps.println(d + " rounds to " + Math.round(d)); } } }

    data.text
    

    6 3.2 5.7 4.5 9.0 12 16.4

    data.output
    

    3.2 rounds to 3 5.7 rounds to 6 4.5 rounds to 5 9.0 rounds to 9 12.0 rounds to 12 16.4 rounds to 16

    P05.4 FileCopier

    Write a program that prompts the user for the name of an existing file and the name of a second file to which the contents of the first file should be copied. The existing file contains a header file (an integer) followed by that number of doubles, all of which should be copied to the destination file..

    Code

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    import java.util.Scanner;
    
    public class FileCopier {
    	public static void main(String [] args) throws FileNotFoundException {
    		Scanner keyboard = new Scanner(System.in);
    
    		System.out.println("File to copy from: ");
    		String infilename = keyboard.next();
    
    		System.out.println("File to copy to: ");
    		String outfilename = keyboard.next();
    
    		Scanner infile = new Scanner(new File(infilename));
    		PrintStream outfile= new PrintStream(outfilename);
    
    		int numValues = infile.nextInt();
    		outfile.println(numValues);			// have to copy the header as well
    		outfile.println();						// blank line to separate the header from the actual data
    
    		for (int i = 1; i <= numValues; i = i + 1) {
    			double num = infile.nextDouble();
    			outfile.println(num);
    		}
    
    		infile.close();
    		outfile.close();
    
    		System.out.println(numberOfValues + " values copied from " + infilename + " to " + outfilename);
    	}
    }
    

    Notes

    oldfile.data
    5
    12.1   6.7   3.5   9   45.2
    

    Notes

    stdin (keyboard)
    oldfile.data
    newfile.text
    

    Notes

    newfile.text
    5
    
    12.1
    6.7
    3.5
    9.0
    45.2
    

    Notes

    stdout
    File to copy from: 
    File to copy to: 
    5 values copied from oldfile.data to newfile.text
    

    The PrintWriter Class

    For technical reasons beyond the scope of this class (or at least for now), there is a second class — PrintWriter — that provides print and println methods that operate identically (as far as we are concerned) to those of the PrintStream class. And for equally obscure reasons, CodeLab exercises on files request the use of PrintWriter rather than PrintStream.

    Files used in Lecture 5

    Lab for Lecture 5