CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class...
-
Upload
rodger-page -
Category
Documents
-
view
231 -
download
2
Transcript of CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class...
![Page 1: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/1.jpg)
CS101 Lab
“File input/Output”
![Page 2: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/2.jpg)
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.*;
![Page 3: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/3.jpg)
Reading from Text file
FileReader fr = new FileReader( “input.txt” ); BufferedReader inputStream = new BufferedReader( fr );
BufferedReader inputStream = new BufferedReader( new FileReader(“input.txt”) );
File access obejct
File reading object
* Usage : inputStream.read(); inputStream.readLine();
![Page 4: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/4.jpg)
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;
}
![Page 5: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/5.jpg)
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) {
…
}
![Page 6: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/6.jpg)
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);
}
![Page 7: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/7.jpg)
Write text to file
FileOutputStream fw = new FileOutputStream( “out.txt” );PrintWriter outputStream = new PrintWriter( fw, true );
PrintWriter outputStream = new PrintWriter( new FileOutputStream(“out.txt”), true );
PrintWriter outputStream = new PrintWriter( new FileWriter(“out.txt”), true );
File access object
File reading object
* Usage : outputStream.print( … ); outputStream.println( … );
![Page 8: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/8.jpg)
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
![Page 9: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/9.jpg)
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();
}
StringTokenizer
20050001 Hong No
20040002 Kim CS
20030001 Na PH
in.txt
![Page 10: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/10.jpg)
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) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } System.exit(0); }}
Kim 30000
Choi 20000
Oh 10000
in.txt
![Page 11: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/11.jpg)
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 RequirementsRequirements
1. Download “EmployeeNode.java”, “EmployeeList.java” from cs101 homepage and use them
2. Add two method in EmployeeList classa. public EmployeeNode getHead() : return head nodeb. public int getNodeLength() : return the number of nodes
![Page 12: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/12.jpg)
3. Implement ListTest_A(class name).javaa. 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.
![Page 13: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/13.jpg)
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);}
}
![Page 14: CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649f535503460f94c77d1f/html5/thumbnails/14.jpg)
Result