Object Oriented Programming Lecture 09 File InputOutput Mustafa
Object Oriented Programming Lecture 09: File Input/Output Mustafa Emre İlal emreilal@iyte. edu. tr
Recap • Assignment 07 • Interfaces and Exceptions
Today • Assignment 08 • File Access • Thinking in Java – Chapter 12
Streams • All “data flow” (input and output) for applications happen through “streams”. • “Piped stream" – streams connect to each other and data flows from the source to the sink. • All pipes flow in only one direction. • All keystrokes on the keyboard send a signal through streams to the operating system. . .
java. io. * • Java classes that allow control of streams and the data flow can be found in java. io package • Depending on the type of data and whether the purpose is reading input or sending output, you will need to use different classes.
Direction • Streams run one-way. Either input or output. • Every type of stream has one input version and one output version. • Input classes provide methods to “read” data • Output classes provide methods to “write” data
Data type of Source • Byte Array (as it is in memory– "binary") – Byte. Array. Input Stream • String – String. Buffer. Input Stream • File – File. Input. Stream • "Pipe" – Piped. Input. Stream • Other Streams (one or more) – Sequence. Input. Stream
Filtering Data • Filter. Input. Stream – Filter. Output. Stream – Similar to lens filters, streams often need filters that extract data in specific formats that applications can recognize. • Examples: – Data. Input. Stream • When you want to read primitive types such as int, double, etc – Buffered. Input. Stream • To read data in groups (a buffer) rather than one by one. e. g. : read line by line not keystoke by keystroke
Byte vs Text • Streams are designed to carry data in memory as bytes (0’s and 1’s) from one place to another • However, most of the time, information needs to be communicated to users as UNICODE data. In files or on the screen, bytes need to be converted into text. • "Reader“ and "Writer" classes carry out this conversion.
Reader - Writer • Almost all stream classes have a corresponding Reader and Writer classes. • Examples: – Input. Stream - Reader – File. Input. Stream - File. Reader – Piped. Output. Stream - Piped. Writer • You should mostly use Reader and Writer classes. Only exception is when you need to access data directly as bytes and such situations will be obvious.
java. io. * package Binary – Input • Input. Stream (abstract) • File. Input. Stream • Buffered. Input. Stream • Data. Input. Stream – Output • Output. Stream (abstract) • File. Output. Stream • Buffered. Output. Stream • Data. Output. Stream Text – Input • Reader (abstract) • File. Reader • Buffered. Reader • String. Reader • Print. Reader – Output • Writer (abstract) • File. Writer • Buffered. Writer • String. Writer • Print. Writer
Example - Keyboard import java. io. *; public class read. From. Keyboard { public static void main(String[] args) { Buffered. Reader br = new Buffered. Reader( new Input. Stream. Reader(System. in))); . . . String line = br. read. Line() ; . . . }
Example - File import java. io. *; public class Write. To. File { public static void main(String[] args) { Print. Writer out. File = new Print. Writer ( new Buffered. Writer( new File. Writer(args[0]))); . . . out. File. println(“A line of text"); . . . }
Keyboard Alternatives 1. Buffered. Reader + Input. Stream – Java 1. 1 – Conventional – Needs Try/catch 2. java. util. Scanner – Java 5 – Preferred by many – No need for Try/catch 3. System. console() – – Java 6 Password entry support No need for try/catch Does not work in IDE environments such as net. Beans
try-with-resources • Try / catch that automatically closes streams and files. try (Buffered. Reader br = new Buffered. Reader(new File. Reader(path))) { return br. read. Line(); } • May handle multiple resources in one statement • Errors in closing files are “suppressed”
Assignment 09 – Part I • • • Write an application that displays the contents of a given text file. Write an application that writes to a file, the lines of text that the user enters using the keyboard. The application will end only when the user enters the line “end”. Write an application that adds line numbers to a given text file: – Format: "001: First line" "002: Second line"
Assignment 09 – Part II • • A line number program: Should display a menu: – – – Selected File: xxxxxx 1) Select/Open new file 2) Display the file 3) Add line numbers 4) Remove line numbers 5) Exit
Next week • Data Structures – Containers - Collections • Preperation: Thinking in Java Chapter 11
- Slides: 18