Intermediate Java

Week 2 Review Questions

1. How does Java read and write to files?
Java opens a stream on an information source ( file, memory or socket) and reads the information sequentially.
2. What is stream?
A pipeline for moving databetween objects.
3. What package do you need to use to be able to perform reading and writing to files?
java.io
4. What is the difference between Character and Byte Streams?
5. What main classes implement byte streams?
Character classes and byte classes
6. What is the main stream class for data input (reading data)?
The byte stream "Input Stream"
7. What is the main stream class for data output (writing data)?
The byte stream "Output Stream"
8. What is the purpose of flush() method?
Forces the output stream to write out any (buffered) bytes, even if the buffer is not full.
9. Which method do you use to close the stream?
close()
10. What happens if the stream cannot be closed?
The method throws an IOException.
11. What is the purpose of FileInputStream and FileOutputStream classes?
The read or write bytes from a file.
12. Which is the preferred method of file name addressing: relative of absolute?
relative
13. How would you append data to the end of a file? Show the constructor for the class you would use and explain your answer.
use FileOutputStream with 2 arguments - the relative path of the file to add to, and the boolean "true".
Constructor: FileOutputStream appendMyFile = new FileOutputStream("filename.html", true);
14. What is up with all those big-endians and little-endians :o))?
Big end-ian and Little end-ian has to do with arbitrary choices made by microprocessor manufacturers about which bytes of a multi-byte number are stored into memory first. Two systems are used, one which stores the most significant digit first, and onw which stores the least significant digit first. An analogy would be writing dates in documents in the form 2/8 : Is this the 8th day of the 2nd month or is it 2nd day of the 8th month ? There are 2 systems - one used in Europe and one in the U.S. (As always, we Canadians are confused about which system to use.) You need to know which system is being used, especially if there is a chance you might lose some of the information - are you losing the day or the month?
15. Why do we need stream filter classes?
These take data from a stream and modify it - for example to clean up the big and little endian confusion. Or for security purposes. Or due to limits of memory size. Or for any number of reasons that mean that it is better to handle the data a few bytes at a time.
16. What class would you use to read a few pieces of data that are at known positions near the end of a large file?
DataInputStream class, reading data writtten by a DataOutputStream.
17. What class in the java.util.zip package gives you access to the entries in a ZIP archive and allows you to read those entries through a stream?
RandomAccessFile