|
|
-
Creating your own files is a little bit trickier than reading from them. There are two basic ways to write data to files: raw bytes and character streams. Raw byte writing is useful for items such as pictures. Character streams are used for writing plain text. This curriculum will focus only on character streams.
- This curriculum uses the java.io.FileWriter class. It has two basic constructors:
FileWriter file = new FileWriter(“test.txt”);
FileWriter file2 = new FileWriter(“test2.txt”, true);
The first constructor opens a basic FileWriter object that points at the file “test.txt” in the same directory where the Java files are being run. When this file is first opened and written to, all of the data that was previously stored in the file will be erased. The second constructor indicates that the new data being sent to the file will be appended to the end of the file. In either case, if the file does not exist, then Java will attempt to create a new file with the indicated name and at the indicated location.
- Writing data to the file is done by using the FileWriter.write(
String, int, int ) method. The String is the data that will be written to the file. The first int is where to start writing the data in the String . The second int indicates how many characters of the String to actually write. For example:
String one = “#Hello!!!”;
FileWriter out = new FileWriter(“test.txt”);
out.write(one, 1, 5);
This will write only “Hello” to the file “test.txt.”
- Merely opening a file and writing to it is not enough to store your data in most cases. You know from personal experience that if you don’t save your work in a word processor, your work will not be there the next time you start up your computer. Data must be saved. This is done with FileWriter by calling the close() method when you are done writing data. This “closes” the output stream to the file and saves your data.
Out.write(one, 1, 5);
Out.close();
- What if there is some error in opening the file? That’s right - an exception is thrown and it must be dealt with just like in the Scanner class.
String one = “Hello World!!!”;
FileWriter out;
try{
out = new FileWriter(“test.txt”);
out.write(one, 0, one.length());
out.close();
}catch(IOException i){
System.out.println(“Error: “ + i.getMessage());
}
-
There is no equivalent to println() with the FileWriter class, so any newlines that you wish to create must be done with the ‘\n ’ character.
- FileWriter only deals with writing Strings to the text files, which creates a little bit of a problem with writing numeric data. However, we can use the shortcut learned earlier in Lesson A10, Strings to change our other data types to Strings.
String temp;
int a = 5;
temp = “” + a + “\n”;
out.write(temp, 0, temp.length());
double p = 3.14;
temp = “” + p + “\n”;
out.write(temp, 0, temp.length());
boolean test = true;
temp = “” + test + “\n”;
out.write(temp, 0, temp.length());
- Because
FileWriter requires you to specify how many characters of the given String to print out, you must be careful with the values that you give it. If the int value that you send is bigger than the String itself, you will get a StringIndexOutOfBoundsException when the FileWriter object tries to access characters in the String which do not exist. An easy way to prevent this from ever occurring is to always create a String object before the write method is called with the data you wish to output, place that String in the call to write, and use that String’s length() method for how many characters to print.
String one = “Hello World!!!\n”;
Out.write(one, 0, one.length());
|