Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.

Similar presentations

Presentation on theme: "COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12."— Presentation transcript:

1 COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12

2 COMP201 Topic 6 / Slide 2 Objectives and Outline l Objectives: n Overall view i/o in java n Reading and writing local files l Outline n Introduction and overview n Reading and writing local files –Connecting to files –Reading and writing characters –Reading and writing objects n File management

3 COMP201 Topic 6 / Slide 3 Introduction and Overview l When talking about IO, we need to consider files n At different locations: –blocks of main memory – local file system – over the net n In different formats: –text or binary – zipped or not zipped n With different access modes: –plain sequential, –buffered, –pushback, –Random

4 COMP201 Topic 6 / Slide 4 l Java streams n Input streams: –Objects from where we read input sequences n Output stream: –Objects where we write output sequences l Java has stream classes allow us to deal with all possible combinations of location, format, and access mode. l In particular, Java streams provide an abstraction of files at different locations n Local files and files from internet can be handled the same way. n We discuss only local files in this lecture. l In the next few slides, we give some of the stream classes Introduction and Overview

5 COMP201 Topic 6 / Slide 5 Introduction and Overview All classes for reading from character streams inherit from abstact class Reader Reader has one abstract method read, which returns the next unicode character or –1 (EOF) Reader LineNumber Reader Pushback Reader InputStream Reader CharArray Reader Filter Reader Buffered Reader String Reader Piped Reader File Reader

6 COMP201 Topic 6 / Slide 6 Introduction and Overview All classes for writing character streams inherit from abstact class Writer Writer has one abstract method write(int b), which writes a unicode charater to an output Writer Print Writer File Writer Buffered Writer OutputStream Writer Piped Writer Filter Writer CharArray Writer String Writer

7 COMP201 Topic 6 / Slide 7 Introduction and Overview All classes for reading from byte streams inherit from abstact class InputStream InputStream has one abstract method read, which returns the next byte character or –1 (EOF) InputStream Pushback InputStream LineNumber InputStream Data InputStream Buffered InputStream File InputStream ByteArray InputStream Filter InputStream Sequence InputStream StringBuffer InputStream Object InputStream Piped InputStream..

8 COMP201 Topic 6 / Slide 8 Introduction and Overview All classes for writing to byte streams inherit from abstact class OutputStream OutputStream has one abstract method write(int b), which writes one byte to an output OutputStream Checked OutputStream PrintStream Data OutputStream Buffered OutputStream ByteArray OutputStream Filter OutputStream Object OutputStream Piped OutputStream.. File OutputStream

9 COMP201 Topic 6 / Slide 9 Reading and Writing local files l Plan –Connecting to files: open files –Reading and writing characters l Parsing –Reading and writing objects

10 COMP201 Topic 6 / Slide 10 Connecting to Files l Open a file for writing bytes n FileOutputStream out = new FileOutputStream(“employee.dat”); FileOutputStream has method for writing bytes: out.write(int b); n Seldom write individual bytes write method used by higher level streams l Open a file for reading bytes FileInputStream in = new FileInputStream(“employee.dat”); FileInputStream has method for reading bytes; n Seldom read individual bytes read method used by higher level streams

11 COMP201 Topic 6 / Slide 11 Writing Characters An OutputStreamWriter is a bridge from character streams to byte streams Has method for writing character: void write(int c) Nesting OutputStreamWriter with FileOutputStream allows us to write individual characters to files n OutputStreamWriter out = new OutputStreamWriter (new FileOutputStream(“employee.dat”) ); If out.write(‘a’), out.flush() then ‘a’ goes to the file (“employee.dat”). –Two classes in action here l One converts ‘a’ into bytes l One write bytes to file

12 COMP201 Topic 6 / Slide 12 Writing Characters The combination of OutputStreamWriter and FileOutputStream is commonly used. A convenience class FileWriter is hence introduced FileWriter f = new FileWriter(“employee.dat”); is equivalent to OutputStreamWriter f = new OutputStreamWriter( new FileOutputStream(“employee.dat”));

13 COMP201 Topic 6 / Slide 13 Writing Charaters l Seldom write characters one by one n Usually write strings. How to write Strings? PrintWriter prints formatted representations of objects to a text-output stream Has methods print and println. l Example n PrintWriter out = new PrintWriter( new FileWriter(“employee.dat”)); If out.println(“this is a test”), – then the string “this is a test” goes to file “employee.dat” n Three classes in action here –PrintWriter breaks the string into characters – OutputStreamWriter converts characters into bytes –FileOutputStream writes bytes to file

14 COMP201 Topic 6 / Slide 14 Reading Characters l For writing we have n FileOutputStream, OutputStreamWriter, FileWriter, PrintWriter For reading we have n FileInputStream, InputStreamReader, FileReader, BufferedReader BufferedReader has method readLine for reading one line

15 COMP201 Topic 6 / Slide 15 Read and Write Standard IO System.out predefined PrintStream, stands for screen. l Print to screen: l System.out.print(); System.out.println(); predefined InputStream, stands for keyboard. Nest with BufferedReader Use method readLine l Example:

16 COMP201 Topic 6 / Slide 16 Parsing l Reading a text file / Parsing input s=readLine() gives you a long string. l Need to break the string into individual strings and convert them into proper type. To do this, use the StringTokenizer class in java.util. Create a StringTokenizer object for the delimiter “|” String Tokenizer t = new StringTokenizer(s, “|”);

17 COMP201 Topic 6 / Slide 17 Parsing Reading a text file / Parsing input Use the nextToken method to extract the next piece from string. t.nextToken () --- name. t.nextToken() --- salary as string. Need Double.parseDouble t.nextToken() --- year as string. Need Integer.parseInt t.nextToken() --- month as string. Need Integer.parseInt t.nextToken() --- day as string. Need Integer.parseInt l Here we know that there are 5 tokens on each line. In general, call t.hasMoreTokens before each t.nextToken Use StreamTokenizer to parse a file

18 COMP201 Topic 6 / Slide 18 Reading and Writing Objects l When useful n Need to save some information and retrieve it later. n Saved information does not have to be human readable. l Why bother (since we can write and read text files) n Much easier than writing and reading text files

19 COMP201 Topic 6 / Slide 19 Writing Objects To save an object, open an ObjectOutputStream ObjectOutputStream out = new ObjectOutputStream ( new FileOutputStream(“employee.dat”)); Simply use writeObject method to save an object Employee harry = new Employee("Harry Hacker", 35000, new Day(1989,10,1)); Manager carl = new Manager("Carl Cracker", 75000, new Day(1987,12,15)); out.writeObject(harry); out.writeObject(carl);

20 COMP201 Topic 6 / Slide 20 Reading Objects To get back an object, open an ObjectInputStream ObjectInputStream in = new ObjectInputStream ( new FileInputStream(“employee.dat”)); Use readObject method to retrieve objects in the same order in which they were written Employee e1 = (Employee) in.readObject(); Employee e2 = (Employee) in.readObject(); Cast necessary because readObject returns an object of class Object.

21 COMP201 Topic 6 / Slide 21 Serializable Interface l For object writing/reading to work, the class must implement the serializable interface n Class Employee implements Serializable(){…} l Since Arrays, one can write/read an array of any objects in one sentence n Employee[] staff; … n out.writeObject( staff ); n (Employee[])in.readObject();

22 COMP201 Topic 6 / Slide 22 File Management Use l Creating a new directory: Create a File object File tempDir = new File( “temp”); n Create directory tempDir.mkdir(); l Creating a new file n Create a File object: File foo = new File(“dirName”+File.separator + “data.txt”); File foo = new File(“dirName”, “data.txt”); n Create file foo.createNewFile();

23 COMP201 Topic 6 / Slide 23 File Management l Inspecting contents of a directory someDir.list() returns an array of file names under the directory l Deleting files and directories someDir.delete(); someFile.delete();

Download ppt "COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12."

Similar presentations

Ads by Google