File I/O Flashcards
What are Character Streams used for?
Reading/writing files that contain ASCII/Unicode data
- Text files
- HTML files
- Program source files
What classes do Byte Streams inherit from?
InputStream and OutputStream
What classes do Character Streams inherit from?
Reader and Writer
What are Byte Streams used for?
To read/write low level data
- Image pixels
- Audio data
What is a file in terms of I/O streams?
Can be anything from a disk file to a physical device:
- Screen
- Keyboard
- Mouse
What is a stream in terms of I/O streams?
An abstraction/interface between the programmer and file
How does file I/O work in java?
- Opening the data source: Create a stream and associate it with the data source
- Accessing the data stream: Read and write from the file
- Closing the data stream: Close the stream to free up memory and clear any buffered data
What are the two major types of byte streams in Java?
- FileInputStream & FileOutputStream
- DataInputStream & DataOutputStream
What are the two major types of character streams in Java?
- FileReader & FileWriter
- BufferedReader & PrintWriter
How do you create a FileInputStream in Java?
FileInputStream fileInputStream = new FileInputStream( filename );
How do you create a DataOutputStream in Java?
DataOutputStream dataOutputStream =new DataOutputStream(new FileOutputStream( filename ));
What are ObjectOutputStream and ObjectInputStream used for? Give examples.
- Used to read and write objects from file
- Can only read and write serializable objects (e.g. Java classes)
What are Checked Exceptions?
- Problems that occur outside of a program’s control (e.g. Corrupt files)
-These must be dealt with
What are Unchecked exceptions?
- Problems caused by the programmer/user (e.g. ArrayIndexOutOfBounds exception)
- These can be ignored