Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reading and Writing Text Files

Similar presentations


Presentation on theme: "Reading and Writing Text Files"— Presentation transcript:

1 Reading and Writing Text Files

2 Writing Text Data to a File
Before you can write to a text file, you need to create a character output stream and you need to connect that stream to a file. Basic process: Open the file Write to the file Close the file

3 Writing Text Data to a File
Create a FileWriter object Opens the file Connects the output stream to a file Create a BufferedWriter object Creates a buffer for the output stream Create a PrintWriter object Writes data to the stream Important methods: print println close

4 Before you can write to a text file, you need to create a character output stream and you need to connect that stream to a file. This example shows how to include a BuffedWriter object in the output stream. The buffer increases efficiency.

5 import javax.swing.JOptionPane;
import java.io.*; public class FileOut { public static void main(String[] args) throws IOException String line = ""; String file = "myFile.txt"; FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); PrintWriter outFile = new PrintWriter(bw); for(int i = 0; i < 4; i++) line = JOptionPane.showInputDialog(null, "Enter word #" + (i+1)); outFile.println(line); } outFile.close(); System.exit(0);

6 Reading Text Data from a File
Before you can read from a text file, you need to create an input stream and you need to connect that stream to a file. Basic process: Open the file Read from the file Close the file

7 Reading Text Data from a File
Create a FileReader object Opens the file Connects the input stream to a file Create a BufferedReader object Used to create a character input stream that uses a buffer. Important methods: readLine close

8 Before you can read characters from a text file, you must connect the character input stream to a file. The BufferedReader class creates a buffer and provides methods that read data so you’ll almost always want to use this class. Then, you'll need to use the FileReader class to connect the character input stream to a file.

9 public static void main(String[] args) throws IOException
import java.io.*; public class FileIn { public static void main(String[] args) throws IOException String line = ""; FileReader fr = new FileReader("demoFile.txt"); BufferedReader inFile = new BufferedReader(fr); line = inFile.readLine(); while(line != null) System.out.println("Just read: " + line); } inFile.close(); Before you can read characters from a text file, you must connect the character input stream to a file. The BufferedReader class creates a buffer and provides methods that read data so you’ll almost always want to use this class. Then, you'll need to use the FileReader class to connect the character input stream to a file. Since the BufferedReader constructor accepts an argument of type Reader, it can accept a FileReader object that connects the stream to a file, or it can also accept an InputStreamReader object, which can be used to connect the character input stream to the keyboard or to a network connection rather than to a file.

10 import java.io.*; import java.util.HashSet; public class FileIn { public static void main(String[] args) String line = ""; HashSet<String> words = new HashSet<String>(); try BufferedReader inFile = new BufferedReader(new FileReader("demoFile.txt")); line = inFile.readLine(); while(line != null) String[] wordArray = line.split(" "); for(String word: wordArray) words.add(word); } inFile.close(); catch(Exception e) System.out.println("Problem opening or reading file."); for(String word: words) System.out.println(word); Code demonstrating using exception handling when reading from a text file.


Download ppt "Reading and Writing Text Files"

Similar presentations


Ads by Google