

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Professor: Thornton; Class: Fund Software Dsgn; Subject: Computing and Information Sciences; University: Kansas State University; Term: Spring 2010;
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!
This section will describe how to read from a text file and how to write to a text file. In both cases, you will need to include the line:
import java.io.;*
You will also need to add:
throws IOException
to the end of the main method. Thus, the main method will look like this:
public static void main(String[] args) throws IOException {
}
This means that interacting with files can cause some unexpected problems (like if the file isn’t there), but that we’re not going to handle them. Later in the course we will discuss how to handle these problems.
Reading from a File First, we need to open a connection to the file. We do this by creating a Scanner (like we do to get user input). This time, however, we’ll connect the Scanner to a file:
Scanner s = new Scanner(new File(filename));
Here, filename is a string that is the name of the input file. This file should be stored in the same directory as the class that’s using it. Alternatively, you can specify the absolute path of the file (e.g., “C:\Documents and Settings...”).
To read a single line in the file, do this:
String line = s.nextLine();
The first line of the file will be stored in the line variable. If we call nextLine() again, it will read the second line, and so on.
To see if you’ve reached the end of the input file, use the hasNext() command. This will return false if we’re at the end of the file, and true otherwise.
When you’re done reading from a file, you need to close it:
s .close();
Here’s an example that opens the file “in.txt”, and prints every line from the file to the screen:
Scanner s = new Scanner(new File(“in.txt”)); while (s.hasNext()) { String line = s.nextLine(); System.out.println(line); } s.close();
If in.txt looked like this: Hi Testing example
Then this would print to the command line: Hi Testing example
You will often read files that have a bunch of information on each line – say, a bunch of names separated by spaces. To process each name, read each line (as shown above), and then use a StringTokenizer to break apart each line.
Writing to a File First, we need to open a connection to the file. Here, filename is the name of the file we want to write to. If this file doesn’t exist, it will be created in the same directory as your code. If it does exist, everything in the file will be overwritten.
PrintWriter sw = new PrintWriter(new FileWriter(filename));
Now we can write to the file. Here’s how to write a single line of text (and advance to the next line):
pw.println(stuff);
Here, stuff is the String, int, char, or double that you want to write to the file. This function works exactly like System.out.println. To print a line without advancing to the next line, do:
pw.print(stuff);
When you’re done writing to the file, close it: