10 May 2011

Interview questions: Java I/O Streams

Topic: Java I/O Streams


Faculty Name:Viji.V

Q What is the difference between RandomAccessFile and File?

Answer: The File class contains information the files and directories of the local file system. The RandomAccessFile class contains the methods needed to directly access data contained in any part of a file.

http://download.oracle.com/javase/tutorial/essential/io/

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://www.javapassion.com/javase/javaiostream.pdf



Q .How do I append to end of a file in Java?
Answer:You can use java.io.RandomAccessFile and something like the following:

try {
RandomAccessFile raf =
new RandomAccessFile("filename.txt", "rw");
raf.skipBytes( (int)raf.length() );
// You are now at the end of the file,
// and can start writing new data out, e.g.
raf.writeBytes(
"Log restarted at 13:00pm 3-2-2000\n");
raf.close();
} catch (IOException ex ) {
ex.printStackTrace();
}
or you can use FileWriter / FileOutputStream and open it for append:

FileWriter writer =
new FileWriter("filename.txt", true);

http://download.oracle.com/javase/tutorial/essential/io/

http://ibiblio.org/java/books/javaio/



Q. How do I check for end-of-file when reading from a stream?

Answer: Exactly how depends upon which stream you are reading from. If you are reading with the read() method of InputStream/Reader, this will return -1 on EOF. If however you are using BufferedReader.readLine(), then this will return null on end of file. And, the readXXX operations of the DataInput interface throw an EOFException. If you are unclear how to terminate your reading, check the javadoc for the stream you are using.

http://download.oracle.com/javase/tutorial/essential/io/

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://www.javapassion.com/javase/javaiostream.pdf


Q.) Under what circumstances would I use random access I/O over sequential, buffered I/O?

Answer: Whether you use random access I/O or sequential access really depends upon what you are trying to do. Random access I/O is usually used for fixed-size data records, where you want to overwrite the original record with changes, or to create something like a rolling log file. Nowadays, there are lightweight database systems that would do this type of operation for you, adding capabilities like querying and a standard JDBC access so you wouldn't have to waste your time redesigning the wheel. While not trying to tell you to never use random access I/O, the Java RandomAccessFile class lives outside the Java streams class hierarchy meaning that you can't add a facade around the random access file to buffer it or enrich its capabilities in any manner. And you would also need to program in things like simultaneous access.
Sequential access is just for that, when you need to access a file sequentially from start to finish. While you can skip() around the data, it is generally meant to be read from beginning to end, where the whole file has some meaning. For performance reasons, it is best to always buffer your I/O, at least when using Reader, Writer, InputStream, and OutputStream classes, but not RandomAccessFile.

http://download.oracle.com/javase/tutorial/essential/io/

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://www.javapassion.com/javase/javaiostream.pdf

Q. How do I copy a file?

Answer: To make a copy of a file, open the source file as a FileInputStream. Open the destination as a FileOutputStream. As you read a byte from the input, write it to the output. There is no built-in method to make a file copy for you.

http://download.oracle.com/javase/tutorial/essential/io/

http://ibiblio.org/java/books/javaio/


Q. What is a stream?

Answer: Basically, a stream is an ordered look at a sequence of bytes for input or output.
Low-level streams provide direct access to the underlying bytes, like a FileInputStream, FileOutputStream, or CharArrayWriter, where reading and writing work directly with the underlying input/output device. High-level streams, or filters, instead build upon the low-level streams to provide additional capabilities, like buffering, counting lines, compressing data, or reading/writing higher-level data members like primitives or objects. You can chain multiple filters together to get multiple higher-level operations on a single low-level stream.

http://download.oracle.com/javase/tutorial/essential/io/streams.html

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://www.javapassion.com/javase/javaiostream.pdf


Q. How can I open the same file for reading as well as writing?

Answer: The RandomAccessFile class supports simultanous reading and writing from the same file. Just open it in "rw" mode:
RandomAccessFile raf =
new RandomAccessFile("filename.txt", "rw");
Use skipBytes() to move to where you wish to read/write.

http://download.oracle.com/javase/tutorial/essential/io/

http://ibiblio.org/java/books/javaio/


Q. How do I list all drives/filesystem roots on my system?

Answer: The listRoots() method of the File class was introduced with the 1.2 release for this:
File[] roots = File.listRoots();
for(int i=0;i
System.out.println("Root["+i+"]:" + roots[i]);

http://download.oracle.com/javase/tutorial/essential/io/

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://www.javapassion.com/javase/javaiostream.pdf


Q. How to insert content into the middle of a file without overwriting the existing content?

Answer: There is no direct support for inserting content in the middle of a file/stream. What you need to do is copy the original content into another file/stream with the new content added where necessary.

http://download.oracle.com/javase/tutorial/essential/io/

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://www.javapassion.com/javase/javaiostream.pdf


Faculty Name:Saritha G Pillai

Q. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

Answer: The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.

http://download.oracle.com/javase/tutorial/essential/io/streams.html

Q.What is the purpose of the File class?

Answer. The File class is used to create objects that provide access to the files and directories of a local file system.

http://download.oracle.com/javase/tutorial/essential/io/streams.html

http://ibiblio.org/java/books/javaio/

http://www.javapassion.com/javase/javaiostream.pdf


Q. What an I/O filter?

Answer: An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.

http://download.oracle.com/javase/tutorial/essential/io/

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://www.javapassion.com/javase/javaiostream.pdf


Q.What class allows you to read objects directly from a stream?

Answer: The ObjectInputStream class supports the reading of objects from input streams.

http://download.oracle.com/javase/tutorial/essential/io/

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://www.javapassion.com/javase/javaiostream.pdf


Q. How do I write text to a file?

Answer: Writing text involves writing strings, so you would use a FileWriter.
FileWriter fw = new FileWriter(filename);
PrintWriter pw = new PrintWriter(fw);

pw.println("Hello, World");
pw.close();

http://download.oracle.com/javase/tutorial/essential/io/

http://ibiblio.org/java/books/javaio/

Q. How do I get the creation date and time of a file?

Answer:  There is no support for getting the creation date and time of a file from Java. All you can get is when the file was last modified, with the the lastModified() method of File.
http://ibiblio.org/java/books/javaio/

http://www.javapassion.com/javase/javaiostream.pdf

http://download.oracle.com/javase/tutorial/essential/io/

Q. How to make a file read only?

Answer:  For any type of file, just use the setReadOnly() method of File to mark a file as read-only. There is no argument to make it writable once it is read-only.

http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/

http://download.oracle.com/javase/tutorial/essential/io/

Q. What is Serialization and deserialization?

Answer: Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.

http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/serial.html


http://java.sun.com/developer/technicalArticles/Programming/serialization/


Q. How can I customize the seralization process? i.e. how can one have a control over the serialization process?

Answer: Yes it is possible to have control over serialization process. The class should implement Externalizable interface. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.

http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/serial.html

http://java.sun.com/developer/technicalArticles/Programming/serialization/

Q. How to make a class or a bean serializable?

Answer: By implementing either the java.io.Serializable interface, or the java.io.Externalizable interface. As long as hierarchy implements Serializable or Externalizable, that class is serializable.


http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/serial.html

http://java.sys-con.com/node/43802

http://java.sun.com/developer/technicalArticles/Programming/serialization/

For the latest IT jobs visit  www.ipsrjobs.com

No comments:

Post a Comment