WRITING DATA TO A TEXT FILE: FileWriter

advertisement
WRITING DATA TO A TEXT FILE:
The FileWriter and BufferedWriter Classes
The FileWriter and BufferedWriter classes are part of the java.io package and are used together to
write data to a file. The FileWriter class is used to create an output file stream, while the
BufferedWriter class is used to send data to the stream.
The following is a list of some of the constructors and methods included in the FileWriter and
BufferedWriter classes:
CONSTRUCTOR
DESCRIPTION
FileWriter(File file)
Creates an output file stream from the specified File object.
Throws an IOException exception if an error occurs.
FileWriter(File fileName, boolean
append)
Creates an output file stream for the File object. Throws an
IOException if an error occurs. If the second parameter is
true, data is added to the end of the file if the file already
exists; if false, the file will be overwritten with the new data.
BufferedWriter(Writer stream)
Creates a buffered stream writer from the FileWriter object.
METHODS
DESCRIPTION
void newLine()
Writes a newline character to the output stream. Throws an
IOException if data cannot be written to the stream.
void write(String str)
Writes the String str to the output stream. Throws an
IOException if data cannot be written to the stream.
void write(char c)
Writes the character c to the output stream. Throws an
IOException if data cannot be written to the stream.
void close()
Closes the output file stream. Throws an IOException if the
stream cannot be closed.
The following program prompts the user for three (3) names and three (3) student numbers and then
writes them to a file called studentNumbers.txt:
import java.io.*;
public class FileWriterExample {
public static void main(String[] args) {
// Declare and initialize File object
Writing Data to a Text File
Page 1 of 3
File dataFile = new File("H:\\My
Documents\\studentNumbers.txt");
// Declare data variables
String name, number;
try
{
// Initialize BufferedWriter and FileWriter objects
BufferedWriter out = new BufferedWriter(new
FileWriter(dataFile));
for (int i = 1; i <= 3; i++)
{
// Prompt user for names and student numbers
name = JOptionPane.showInputDialog(“Please enter
student’s name:”);
number = JOptionPane.showInputDialog(“Please enter ” +
name + “’s student number:”);
// Write data to file
out.write(name);
out.newLine();
out.write(number);
out.newLine();
}
// Close BufferedWriter stream
out.close();
// Confirm that data has been written
JOptionPane.showMessageDialog(null, "Data has been
written to the file!", “Finished!”,
JOptionPane.INFORMATION_MESSAGE);
}
// Handle exception if there’s a problem writing to file
catch (IOException e)
{
JOptionPane.showMessageDialog(null, e.getMessage() + “!”,
“Error”, JOptionPane.ERROR_MESSAGE);
}
}
}
The above program prompts the user for three names and three student numbers using a JOptionPane
object:
Writing Data to a Text File
Page 2 of 3
Once the three names and student numbers have been entered and the data written to the file, the
following message will appear confirming that the data has been written:
The following data file will be saved in the directory specified above:
Writing Data to a Text File
Page 3 of 3
Download