Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CS101 Lab
“File input/Output”
File input, output
• File : binary file, text file
• READ/WRITE class of “text file”
- File Reading class
: FileReader, BufferedReader
- File Writing class
: FileWriter, FileOutputStream, PrintWriter
- import java.io.*;
Reading from Text file
File access obejct
FileReader fr = new FileReader( “input.txt” );
BufferedReader inputStream = new BufferedReader( fr );
File reading object
BufferedReader inputStream =
new BufferedReader( new FileReader(“input.txt”) );
* Usage : inputStream.read(); inputStream.readLine();
readLine() method
Read a line of the text file
Return null if the end of the stream has been reached
BufferedReader inputStream =
new BufferedReader( new FileReader(“input.txt”) );
String s;
While((s = inputStream.readLine()) != null) {
do something with s;
}
Exception Handling in File IO
- FileNotFoundException : if the named file does not exist
- IOException : if an I/O error occurs
- public String readLine() throws IOException
- public FileReader(String fileName) throws FileNotFoundException
try{
BufferedReader reader = new BufferedReader(new FileReader(“input.txt”));
String line = reader.readLine();
…
} catch (FileNotFoundException e) {
…
} catch (IOException e) {
…
}
QUIZ
Print all contents in “input.txt”
BufferedReader inputStream = new
BufferedReader( new FileReader(“input.txt”) );
String s;
While((s = inputStream.readLine()) != null) {
System.out.println(s);
}
Write text to file
File access object
FileOutputStream fw = new FileOutputStream( “out.txt” );
PrintWriter outputStream = new PrintWriter( fw, true );
File reading object
PrintWriter outputStream =
new PrintWriter( new FileOutputStream(“out.txt”), true );
PrintWriter outputStream =
new PrintWriter( new FileWriter(“out.txt”), true );
* Usage : outputStream.print( … );
outputStream.println( … );
StringTokenizer
- Break a string into tokens by delimiters (the characters that separate
tokens)
ex) “I_am_a_students.”
~> delimiter : “_” 4 tokens
delimiter : “ ” 1 tokens
public StringTokenizer(String str, String delim)
import java.util.*;
StringTokenizer tokenizer = new StringTokenizer(str, “\t”);
String token = tokenizer.nextToken(); // return next tokens
int count = tokenizer.countTokens(); // return the number of left tokens
StringTokenizer
BufferedReader input = new BufferedReader( new
FileReader("in.txt") );
String str;
While((str = input.readLine()) != null) {
StringTokenizer token = new StringTokenizer(str, “ ”);
int ID = Integer.parseInt(token.nextToken());
String name = token.nextToken();
String depart = token.nextToken();
20050001 Hong No
}
20040002 Kim CS
20030001 Na PH
in.txt
import java.io.*;
import java.util.*;
public class FileReadWriteTest {
public static void main(String[] args) {
try {
BufferedReader in = new BufferedReader( new FileReader("in.txt") );
PrintWriter out = new PrintWriter( new FileOutputStream("out.txt") , true);
String s;
while ((s = in.readLine()) != null) {
StringTokenizer token = new StringTokenizer(s," ");
String name = token.nextToken();
int borrowing = Integer.parseInt(token.nextToken());
System.out.println(name + " " + borrowing);
out.println(name + " " + borrowing);
}
} catch (FileNotFoundException e) {
Kim 30000
System.out.println(e.getMessage());
Choi 20000
} catch (IOException e) {
System.out.println(e.getMessage());
Oh 10000
}
in.txt
System.exit(0);
}
}
Practice
Implement a progrm that executes correctly the following
procedures
Read the “data.txt” file
Get the information of employee from “data.txt” file
Make linked list using the information
Save the information of employees into the “output_A(Class
name).txt” file
Requirements
1. Download “EmployeeNode.java”, “EmployeeList.java” from
cs101 homepage and use them
2. Add two method in EmployeeList class
a. public EmployeeNode getHead() : return head node
b. public int getNodeLength() : return the number of nodes
3. Implement ListTest_A(class name).java
a. Implement two method
- public static void GetData(String filename, EmployeeList list)
method
: Read Data from file, and save the data into the linked list
- public static void SaveData(String filename, EmployeeList
list) method
: Save Data in the linked list into file
b. Add Exception Handling in GetData, SaveData method
: using try { } catch (Exception) { }
c. main method
•
Download “data.txt” from the cs101 homepage.
•
•
•
•
In each line, one employee’s data is saved in the file.
Each data separate by tab(“\t”).
The name of output file is “output_A(class name).txt”.
You can determine path of the directory.
public class ListTest_A {
……
public static void main(String[] args) {
EmployeeList list = new EmployeeList();
GetData("c:/data.txt", list); // you may modify the path of the text file
String output = "Linked list size : " + list.getNodeLength() + "\n" + list;
list.DeleteNode("003");
list.InsertNode(“Kang,Dong-il", 2004, "010");
list.DeleteNode("005");
output += "\nLinked list size : " + list.getNodeLength() + "\n" + list;
JTextArea area = new JTextArea();
area.setText(output);
JOptionPane.showMessageDialog(null,area);
SaveData("c:/output_class name.txt", list);
// you may modify the path of the text file
System.exit(0);
}
}
Result