The contents of a text file are a sequence of characters, including both readable/printable characters and special characters such an tabs, backspace characters and newline characters. By default, Java characters are represented by 16-bit Unicode values.
Text files are sometimes described as a sequence of lines, where each line is a sequence of characters terminated by an end-of-line character ('\n'). Text file are "human readable".
The easiest way is by using a text editor. You can use a word processor if you are careful to specify text format when you save the file.
Your Java programs can also create text files and direct program output (in text form) to them.
You could use redirection to make a text file the source for the standard input stream.
Another straightforward technique is to create an instance of the FileInputStream class, passing the name of your file as a parameter to the class constructor. This results in the creation of an input stream connected to the named file. You can then use the appropriate input tools to read from that stream. Normally, you would use an InputStreamReader object to get characters from your FileInputStream.
FileInputStream f = new FileInputStream("input.txt");
InputStreamReader fr = new InputStreamReader(f);
// now use fr.read() to read a single character from the input stream
If you want your program to write output to a text file, you can either redirect standard output to that file, or you can create an instance of the FileOutputStream class and use the appropriate output tools to send your output to the output stream it creates. Normally, you would use a PrintWriter or FileWriter object to send character output to your FileOutputStream using the same print() and println() methods you are accustomed to using with System.out, the standard output destination.
FileOutputStream f = new FileOutputStream("output.txt");
PrintWriter fw = new PrintWriter(f);
// now use fw.print(…) and/or fw.println(…)
Java provides a pair of "convenience classes" to facilitate access to text files. They are the FileReader and FileWriter classes.
Yes. When objects or primitive types are outputted to a text file, their values are converted to a sequence of characters and those characters are written to the text file.
For object types, this means invoking the object's toString() method to create a sequence of characters.
For primitive types, the "obvious" sequence of characters is outputted. Thus, a text file never contains actual numbers. It contains printed representation of those numbers, i.e. sequences of digits and decimal points that represent those numbers.
In many cases, the easiest way to read on entire line at a time from the text file. This is done by using a BufferedReader object.
FileInputStream f = new FileInputStream("input.txt");
InputStreamReader fr = new InputStreamReader(f);
BufferedReader br = new BufferedReader(fr);
// now use br.readLine() to read an entire line of text from the input stream
If you are interested in a slightly simpler solution, you can use an instance of the FileReader class to do the jobs of both the FileInputStream and the InputStreamReader objects in the above example. The FileReader class in the Java API is a "convenience class" to make I/O a bit easier in a simple situation like this.
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
This depends on which tool you are using to read from the text file.
The readLine() method of a BufferedReader will return a null value when if there is no more data to be read from the input file.
If you use the simpler InputStreamReader to read characters from a text file, its read() method will return the value -1 if end of file is encountered, otherwise it will return the next char. The type of the actual return value from read() is an int so it can represent any char (16-bit unsigned integer) as well as the number -1 without conflict.
If you use the basic InputStream object to read your text file one byte at a time, the read() method will return the value of the next unsigned byte of data from the input stream. Unsigned bytes are 8-bit signed integers whose values range from 0 to 255. The read() method returns a value of -1 if end of file is encountered. Once again, this is possible because the actual type of read's return value is int.
Instantiate an instance of the FileWriter class (another "convenience class" to create a new file for output. Then use the print() and println() methods, just as you have done when writing to the System.out output stream.
When you have completed writing to the text file, be sure to close it by invoking the FileWriter's close() method.
FileWriter fw = new FileWriter("myoutputfile.txt");
// now use fw.print(…) and/or fw.println(…)
fw.println("this is a test");
fw.close();
// execute the rest of your program
| Things to try |
|---|
© Wiggen & Associates, 2007