Just the same as C++, you can read and write from text files in Java.
In Java, file input occurs through whats called a BufferedReader.
The BufferedReader is contained in the java.io library.
There are numerous types of input streams in Java such as Character Streams, Byte Streams and
Object Streams just to name some. The commonality of these mentioned streams is that they use
the operating system directly to handle the stream.
A BufferedReader will greatly reduce the operating systems use when reading or writing from files.
A simple BufferedReader is defined below:
where in the above definition, varName is a useful name of the BufferedReader input stream;
and filename.txt is an input text file.
The rule about the input stream is that it MUST be contained in a try catch block. Here is how that
will look in a short code snippet:
Example 1:
File input example
Download source code here (Right click - Save Tagret As...)
Download input file here (Right click - Save Tagret As...)
Here is a short example that will read a file of integers and compute the average.
It will use a StringTokenizer to split the integers apart which will be separated by a white space.
The output from above is: "Avg: 26.5"
The program also uses some methods from the StringTokenizer class. If you need a refresher,
please see tutorial 6.
Here is an explanation of the two methods used above in the BufferedReader class.
String readLine()
This will extract an entire line of text as denoted by the carriage return character '\r' or a new
line character '\n'.
In the above program, the line was placed in a String variable called line.
void close()
This will simply flush the input stream and close the file.
int read()
There is also another method in the class called read(). This will simply read an individual
character. It will return a -1 if the end of the stream has been reached.
File Output
In Java, file output occurs through what's called a BufferedWriter.
The BufferedWriter is contained again in the java.io library.
Here is the simple definition of a BufferedWriter object:
For the BufferedWriter class, if the output file name is not currently in the directory, it will
create the file for you. If the file is already there, the output will overwrite the information
in the output file.
Let's see another program that will write output to a file.
Example 2:
File output example
Download source code here (Right click - Save Tagret As...)
Here is a short example that will write a String to a text file named output.txt. It requires
a command line argument which will be a String argument.
There is no output to the console in this program. It is only in the output file.
For the purpose of the example, the String "hello there" will be used. This will be the output
in the file:
Let's observe some methods that are used from the BufferedWriter class:
void write(String s)
This will write a String argument to the file.
void newLine()
This will write a line separator to the file. This is not quite the new line character but it can
be thought of that way.
void close()
This will simply close the file output stream.