Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.

Similar presentations


Presentation on theme: "CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader."— Presentation transcript:

1 CS101 Lab “File input/Output”

2 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.*;

3 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();

4 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; }

5 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) { … }

6 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); }

7 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( … );

8 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

9 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

10 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

11 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 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

12 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.

13 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); }

14 Result


Download ppt "CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader."

Similar presentations


Ads by Google