Download presentation
Presentation is loading. Please wait.
Published byAndrea Flowers Modified over 8 years ago
1
1 Fall 2009ACS-1903 Writing Data To a File When writing to a file we need objects from the following classes are used to write data to files: FileWriter – This class allows basic file writing functionality. PrintWriter – This class allows the programmer to write files using the same style that is used to write to the screen (i.e. print and println).
2
2 Fall 2009ACS-1903 The FileWriter Class The FileWriter clas provides other classes with the basic file writing functionality. System.out.println(“Enter the filename.”); filename = Keyboard.readString(); FileWriter fwriter = new FileWriter(filename); This will create an object that can access the file filename. Warning: if the file above already exists, it will be erased and replaced with the new file.
3
3 Fall 2009ACS-1903 The PrintWriter Class The PrintWriter class adds to the functionality of the FileWriter class. The PrintWriter cannot directly access the file but must work through the FileWriter class. The PrintWriter needs a FileWriter object in order to work: FileWriter fwriter = new FileWriter("StudentData.txt"); PrintWriter outputFile = new PrintWriter(fwriter);
4
4 Fall 2009ACS-1903 The PrintWriter Class Once linked to the fwriter object, the outputFile object can talk to the file. outputFile.println(“Jim”); outputFile.close(); Just as with the System.out object, the println method of the PrintWriter class will place a newline character after the written data. The print method can be used to avoid writing the newline character.
5
5 Fall 2009ACS-1903 Appending Text to a File To avoid erasing a file that already exists: Create a FileWriter object using an optional boolean argument that tells the object to append data to the file. FileWriter fwriter = new FileWriter(“filename”, true); Data written to a file created in such a manner will be appended to the end of the current file.
6
6 Fall 2009ACS-1903 Specifying a File Location Windows’ Crazy Backslash Windows evolved from DOS. Since DOS was simply a hacked version of CP/M, it maintained the backslash (\) as a directory separator. Remember, if the backslash is used in a String literal, it is the escape character so there must be two of them. FileWriter fwriter = new FileWriter("A:\\PriceList.txt"); PrintWriter outputFile = new PrintWriter(fwriter);
7
7 Fall 2009ACS-1903 Specifying a File Location This is only necessary if the backslash is in a String literal. If the backslash is in a String object then it will be handled properly. Fortunately, Java allows Unix style filenames using the forward slash (/) to separate directories. FileWriter fwriter = new FileWriter("/home/rharrison/names.txt"); PrintWriter outputFile = new PrintWriter(fwriter);
8
8 Fall 2009ACS-1903 Lab 9 Question Write the SIN numbers and the provinces to a file named SinAndProvince.txt You need to declare and open the file for output: FileWriter fw = new FileWriter(“SinAndProvince.txt"); PrintWriter of = new PrintWriter(fw); You need to write each SIN and province out to the file of.println( sin+” “+province ); Before ending, your program must close the file to release it. of.close();
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.