Professional Documents
Culture Documents
les
troduction
File processing is performed in Java using various classes. The primary class used to handle files
s called File. The File class is part of thejava.io package. To use it, you can start by importing it
n your file. Here is an example:
port java.io.File;
throws Exception {
he File class
is
based
on
(implements)
the FileOutputStream class.
he FileOutputStreamclass is based on the OutputStream class. That is how it gets most of its
unctionality.
o use a file, declare a File variable using one of its constructors. One of the constructors takes
ne argument as a string, the name of the file or its complete path. Of course, if the file has an
xtension, you must include it. Here is an example:
port java.io.File;
he only real thing the File class does is to indicate that you are planning to use a file. It does not
ndicate what you will do with the file. You must specify whether you will create a new file or open
n existing one.
eating a File
f you want to create a new file, you must use a class that is equipped to write values to a file. To
o this, you can use the PrintWriter class. The PrintWriter class is defined in
he java.iopackage. Therefore, if you want to use it, you can import it in your document. This
would be done as follows:
port java.io.PrintWriter;
throws Exception {
he PrintWriter class is based on (implements) the Writer class. The class is equipped with the
ecessary means of writing values to a file.
efore using the class, declare a variable for it. This class is equipped with many constructors. One
f the constructors takes as argument an OutputStream object. We saw that the File class is
ased on OutputStream. This means that you can pass a File object to a PrintWriterconstructor.
his would be done as follows:
port java.io.File;
port java.io.PrintWriter;
riting to a File
fter creating a PrintWriter object, you can write values to the file. To support this,
hePrintWriter class is equipped with the print() and println() methods that is overloaded with
arious versions for each type of values (boolean, char, char[], int, float, double, String,
rObject). Therefore, to write a value to a file, call the appropriate version of
hePrintWriter.print() method and pass the desired value. Here are examples:
port java.io.File;
port java.io.PrintWriter;
fter using a PrintWriter object, you should free the resources it was using. To assist you with
his, the PrintWriter class is equipped with the Close() method. Here is an example of calling:
port java.io.File;
port java.io.PrintWriter;
return kid;
it
memory
wrtStudent.close();
// For convenience, let the user know that the file has
been created
pening a File
esides creating a file, the second most common operation performed on a file consists of opening one. You can open
he File class. As done previously, first declare a File variable and pass the name of the file to its constructor. Here is an
port java.io.File;
o support the ability to read a value from a file, you can use the Scanner class. To support this operation, the Scan
quipped with a constructor that takes a File object as argument. Therefore, you can pass it a File variable yo
reviously declared. Here is an example of declaring and initializing a variable for it:
port java.io.File;
port java.util.Scanner;
he values of a file are stored in or more lines. To continuously read the lines from the file, one at a time, yo
while loop. In the while loop, continuously use the Scanner object that can read a line of value(s). In the while st
heck whether the Scanner object has not gotten to the last line, you can check the status of its hasNext() method. As
method returns true, the Scanner reader has not gotten to the end of the file. Once the Scanner object has arrived to th
method would return false. Here is an example of implementing this scenario:
port java.io.File;
port java.util.Scanner;
fter using the Scanner object, to free the resources it was using, call its close() method. Here is an example:
port java.io.File;
port java.util.Scanner;
return kid;
}
}
return majoring;
answer = scnr.next();
if( (answer.equals("y")) || (answer.equals("Y")) ) {
show(std);
save(std);
}
}
else if( (mainAnswer.equals("v")) || (mainAnswer.equals("V"))
) {
System.out.println("Good Bye!!!");
}
2. Execute the application
3. On the menu, indicate that you want to view a student record
4. Enter one of the student file names and press Enter
le Management
esides writing to a file or reading from it, there are many other actions you may to perform on a file.
f you try opening a file that does not exist, you would receive an error:
herefore, before opening a file, you may want to check first whether it exists. To assist you with this, the File class
with the exists() method. Here is an example of calling it:
port java.io.File;
port java.util.Scanner;
eleting a File
o delete a file, you can call the delete() method of the File class.