Programlama ve Nesneler

Download Report

Transcript Programlama ve Nesneler

Object Oriented Programming
Lecture 09: File Input/output
Mustafa Emre İlal
[email protected]
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")
– ByteArrayInput Stream
• String
– StringBufferInput Stream
• File
– FileInputStream
• "Pipe"
– PipedInputStream
• Other Streams (one or more)
– SequenceInputStream
Filtering Data
• FilterInputStream – FilterOutputStream
– Similar to lens filters, streams often need filters that
extract data in specific formats that applications can
recognize.
• Examples:
– DataInputStream
• When you want to read primitive types such as int, double, etc
– BufferedInputStream
• 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:
– InputStream - Reader
– FileInputStream - FileReader
– PipedOutputStream - PipedWriter
• 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
• InputStream (abstract)
• FileInputStream
• BufferedInputStream
• DataInputStream
– Output
• OutputStream (abstract)
• FileOutputStream
• BufferedOutputStream
• DataOutputStream
Text
– Input
• Reader (abstract)
• FileReader
• BufferedReader
• StringReader
• PrintReader
– Output
• Writer (abstract)
• FileWriter
• BufferedWriter
• StringWriter
• PrintWriter
Example - Keyboard
import java.io.*;
public class readFromKeyboard {
public static void main(String[] args) {
BufferedReader br =
new BufferedReader(
new InputStreamReader(System.in)));
......
String line = br.readLine() ;
.....
}
Example - File
import java.io.*;
public class WriteToFile {
public static void main(String[] args) {
PrintWriter outFile =
new PrintWriter (
new BufferedWriter(
new FileWriter(args[0])));
......
outFile.println(“A line of text");
.....
}
Assignment 09 – Part I
•
•
•
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 “exit”.
Write an application that displays the contents of
a given text file.
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:
–
–
–
–
–
–
File name: xxxxxx
1) 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