IO Flashcards
File class
used to read information about existing les and directories, list the contents of a directory, and create/delete les and directories.
Two ways to retrieve the file separator character for the current file system
System.getProperty(“file.separator”)
java.io.File.separator
What does the following code do?
File file = new File("/home/smith/data/zoo.txt"); System.out.println(file.exists());
Checks if the file exists within the file system.
How to create a new File object
File file = new File(String path);
Difference between the “Stream” named classes and the “Reader/Writer” classes
The stream classes are used for inputting and outputting all types of binary or byte data, whereas the reader/writer classes can only work with characters and strings
What does it mean when a class has the word “Buffered” in it?
The class reads and writes data in groups rather than a single byte or character at a time.
Unless you are doing something very specialized in your application, you should always wrap a file stream with a Buffered class in practice.
You create these instances by wrapping the corresponding stream type, which means, for example, that if you had a BufferedWriter, you would create it with: new BufferedWriter(new FileWriter(filename))
Where should you open your file streams?
In the opening of a try-with-resources statement so that it will close at the end. Otherwise you must call .close() on it when you are done
flush()
When called on a stream, requests that all accumulated data be written immediately to disk.
Useful for if the application terminates unexpectedly
What should you do before calling the mark(int) method on a stream?
Call markSupported() on the stream to make sure it supports mark(int). Otherwise an exception will be thrown if it doesn’t support it.
mark(int num)
Sets a marker at the current point in the stream and allows you to read up to num amount of bytes with the ability to call reset() if you want to return to the original marked point
skip(long num)
Skips over the next num bytes, or if there are less than num bytes left then skips to the end.
Returns the number of bytes skipped.
How do you know when a FileInputStream object has reached the end of the file?
It returns -1
What exception is thrown if the file you try to read from doesn’t exist?
FileNotFoundException
copy(File file1, File file2)
Copies the contents of file1 to file2, overwriting any content that file2 may already have
write(byte[],int,int)
Used with BufferedOutputStream, stores the bytes in the byte array until filled with the third arguments value of bytes. The second argument is an offset to start writing which is usually set to 0
serialization
The process of converting an in-memory object to a stored data format
Serializable interface
The interface that classes must implement if you are going to serialize objects of that type
Any class can implement the Serializable interface since there are no required methods to implement.
NotSerializableException
Thrown when attempting to serialize an object when its class or an object contained in its class is not marked as Serializable
Which members of a class will be ignored when serializing?
Those marked static or transient
How to create an ObjectInputStream
ObjectInputStream in = new ObjectInputStream (new BufferedInputStream( new FileInputStream(filename)));
Method to deserialize an object given
ObjectInputStream in = new ObjectInputStream (new BufferedInputStream( new FileInputStream(filename)));
Object object = in.readObject();
Note: you need to do an explicit cast if you want it to be something other than Object
Method to serialize an object given
ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(filename)))
out.writeObject(objectName);
How do we know when an ObjectInputStream has reached the end of the input file?
An EOFException is thrown.
What do ObjectInputStream and ObjectOutputStream do with null values?
Accept them!
What is ignored during the deserialization (writing) process?
The constructor and initialization blocks
PrintStream
Operates on OutputStream objects and writes data as bytes
PrintWriter
Operates on Writer objects and writes data as bytes
What type of object are System.out and System.err?
PrintStream
PrintWriter methods
print()
println()
format()
printf()
prints the argument by calling String.valueOf on it
Like print() but with a new line at the end
Takes a string and prints it based on the formatting arguments you give it
Same thing as format(), just there to make it easier on C programmers
How to obtain the console object
Console console = System.console()
Note: will return null if the environment doesn’t support text interactions.
Console methods reader() and writer()
Provide access to an instance of Reader and PrintWriter, respectively
Console method format()
Lets you just pass it a string to print so that you don’t need to use the reader() or writer() method then print()
Console method readLine(prompt)
Displays the String prompt to the user and accepts input terminated by the Enter key
Console method readPassword()
functions the same as the readLine method but does not show the text being typed.
Also returns an array of chars instead of a String to avoid having it be stored in the String pool