File Handling - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

File Handling

Description:

We must close the file when we are done (forgetting to close a file could corrupt the file) ... infile.close( ); outfile.close( ); Notice this while loop once ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 7
Provided by: foxr
Category:
Tags: close | file | handling

less

Transcript and Presenter's Notes

Title: File Handling


1
File Handling
  • As with most aspects of Java, there are classes
    available to help you perform all kinds of I/O
  • These include inputting other than using
    JOptionPane
  • They also include inputting and outputting to
    disk files
  • The classes are all part of the java.io package
  • One difference with these classes is that they
    all require checked exceptions
  • An exception is an event that arises during
    execution of your program
  • If the exception is not handled in some way, your
    program terminates
  • These io classes require that you handle the
    exception in some way
  • We will handle them in the simplest way possible
    but not necessarily a good way

2
Inputting From Disk File
  • The object we need is a BufferedReader but to
    create one, we use a FileReader
  • BufferedReader infile new BufferedReader(new
    FileReader(filename))
  • filename is the String equal to the name of the
    file
  • It may be a String variable or a literal
  • If it is a literal, then we must make sure that
    any \ actually appear as \\ an example will
    illustrate this
  • We pass our variable a readLine( ) message which
    returns the next entry in the file as a String
    and store that in a String variable (at least
    temporarily)
  • To determine when the file is empty, we test the
    String variable to see if it is null or not
  • null is a special value for an object meaning
    that it does not equal anything

3
Example Input an Array from File
this handles the checked exception without
this, the program does not compile
import java.io. public class
InputArrayFromFile public static void
main(String args) throws IOException
BufferedReader key new
BufferedReader(new InputStreamReader(System.in))
String filename JOptionPane.showInp
utDialog("Enter the filename to input from
") BufferedReader infile new
BufferedReader(new FileReader(filename))
int number Integer.parseInt(infile.readLine
( )) int list new
intnumber for(int j 0 j lt
number j) listj
Integer.parseInt(infile.readLine( ))
infile.close( ) for(int j 0 j lt
number j)
System.out.println(listj)
Notice that we are inputting a file of int
values so each input must first be
converted from String to int via Integer.parseInt
4
Some Comments
  • The term throws IOException will handle the
    BufferedReaders checked exceptions
  • If we were to use this in a method called from
    main, then main would also require throws
    IOException
  • If this exception arises, your program terminates
  • The IOException will arise if the disk cannot be
    accessed
  • Other exceptions that could occur are
  • FileNotFound, ArrayOutOfBoundsException,
    NullPointerException, NumberFormatException (if
    the file contains any non-int values)
  • This code assumes that the file stores as its
    first entry the number of int values stored in
    the file
  • this is not necessarily a good assumption
  • an example follows that tests against null
    instead
  • We must close the file when we are done
    (forgetting to close a file could corrupt the
    file)

5
To Output to a Disk File
  • Much like input, we create an object to handle
    output
  • In this case, it is a PrintWriter
  • The PrintWriter is created using a FileWriter
  • PrintWriter outfile new PrintWriter(new
    FileWriter(filename))
  • The method must include throws IOException
  • We use the print or println message to output the
    next item to the file
  • Unlike input, we can output any type (int,
    double, etc)
  • An example follows which inputs items from one
    file and outputs them to a second file, thus
    copying the file

6
Example Copying an Input File to Output
import java.io. public class FileCopier
public static void main(String args) throws
IOException String file1
JOptionPane.showInputDialog(Enter input file
name) String file2
JOptionPane.showInputDialog(Enter output file
name) String inputLine
BufferedReader infile new BufferedReader(new
FileReader(file1)) PrintWriter
outfile new PrintWriter(new FileWriter(file2))
inputLine infile.readLine( )
while(inputLine ! null)
outfile.writeline(inputLine)
inputLine infile.readLine()
infile.close( )
outfile.close( )
Assume the input file contains Strings only
Notice this while loop once we reach the end of
the input file, our input is null so we keep
repeating while the latest item input is not null
that means, there was something else in the
file to copy to the output file
Write a Comment
User Comments (0)
About PowerShow.com