1. Create a new class called FileTest. Remove the "import javabook.*" statement from the top.
2. Enter in the following Java program:
import java.io.*;
class FileTest
{
public void fileWrite()
{
File dstFile = new File("K:\\myOutput\\outputfile.txt");
PrintWriter out = new
PrintWriter
(new BufferedWriter(new FileWriter(dstFile)));
out.print("Hello ");
out.println("world");
out.close();
}
}
Likewise, create a new class called FileTestMain and enter in the following:
import java.io.*;
class FileTestMain
{
public static void main(String[] args)
{
FileTest fileTest =
new FileTest();
fileTest.fileWrite();
}
}
3. Try to compile the class FileTest. What goes wrong? This is because opening up a file could throw an IOException, which is a checked exception. This means you have to tell Java how to deal with it, or the program won't compile.
4. One method to fix it is to add a throws clause at the end of the method header, like this:
public void fileWrite() throws IOException
Try adding "throws IOException" after your main header (as above), and compile again.
Now try to compile FileTestMain. Why does the same problem happen again? fileWrite is allowed to throw an exception to main, but main does nothing with it. Add a "throws IOException" following the header to main in a similar fashion. Now try compiling.
5. Try running your program. What happens? Your program should throw an exception indicating that the system cannot find a place to put the file outputfile.txt. This is because the folder myOutput does not exist. Fix this problem by changing the path in "K:\\myOutput\\outputfile.txt" to actually correspond to the location on your K drive where your project is.
6. Run your program again. If all went successfully, open up "My Computer", and find your FilePractice folder on your K drive. You should be able to find the file "outputfile.txt". Double click on it, and take a look. What do you see?
7. Modify your program to write to the file five lines, each of which contains your name or a friend's name, followed by a space and then an age, then another space and a gpa. For example:
Arlene 19 3.8
Bill 22 3.5
Marilyn 15 3.9
Bryan 35 1.1
Buzz 6 4.0
0. Add the following method to your FileTest class:
public void consoleRead() throws IOException
{
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("What
is your first name? ");
String first = in.readLine();
System.out.print("What
is your last name? ");
String last = in.readLine();
System.out.println("Your
name is " + last + ", " + first + ".");
}
1. Compile it. Add "throws" statements as necessary.
2. Modify your main to run the consoleRead method, and recompile. Run your program. What does it do?
public void fileRead() throws IOException
{
File srcFile = new File("K:\\FilePractice\\outputfile.txt");
BufferedReader in =
new BufferedReader(new FileReader(srcFile));
String text = in.readLine();
System.out.println(text);
in.close();
}
1. Run the method. What do you see? Modify this method to print out the names, ages, and gpas of the five people you stored back in Lab 1.
2. Add code to your program to try reading and printing three more people after you've read the five that are there. (Yes, this is a bad idea!) How does Java react?
3. A better way to read and print from a file is to use a while loop. Modify your code that prints the five people using code something like the following:
while (in.ready()) {
text = in.readLine();
System.out.println(text);
}
4. In reality, you would want to be able to separate each item on each line into different variables, rather than keeping all the information on name, age, and gpa in one string. To break it up, use a StringTokenizer. Here are two examples of code that use StringTokenizers. Play around with them, and see if you can adapt the same idea to have your program print out all five of your people, and then an average age and gpa.
String text = "Beggars in Spain*Nancy Kress*1992*1.5";
StringTokenizer tokenizer = new StringTokenizer(text,"*");
String title = tokenizer.nextToken();
String author = tokenizer.nextToken();
String yearStr = tokenizer.nextToken();
String millionsSoldStr = tokenizer.nextToken();
int year = Integer.parseInt(yearStr);
double millionsSold = Double.parseDouble(millionsSoldStr);