22 Question Related To File or Java
22 Question Related To File or Java
io package
1. What is File?
Ans:
File is a container which store text or binary information for a permanent period.
Ans:
The following are the operations operated with file.
1. Create a new file.
2. Create a directory.
3. Delete a file.
4. Delete a directory.
5. Check permission.
6. Read data from file.
7. Write into file.
3. How to read data from the file in byte format with constructor
Ans:
FileInputStream class is used to read data from file in byte format.
4. How to read data from the file in String format with constructor?
Ans:
By the help of readLine() method.
Ans:
FileOutputStream class is used to write data into file in byte format by the help of
write().
Ans:
By the help of Write() method.
Ans:
By the help of public File(String pathname) ,public File(String pathname, String
filename) method.
Ans:
By the help of public boolean delete() method.
Ans:
A stream is a sequence of data .In java a stream is composed of bytes.
11. Which package contains the classes for input & output operation?
Ans:
java.io package contains all the classes for I/O operation.
Ans:
There are 3 Stream present in java. They are
1. System.out which known as Standard output stream.
2. System.in which is known as Standard input stream.
3. System.err-Standard error.
Ans:
Output stream used to write data to a destination ,which may be a file, an
array, device or socket.
14. State the diff. b/w public void Write(int) throws IOException& public void
Write(byte[]) throws IOException?
Ans:
Write (int) throws IOException is used to write a byte to the current output
stream. Where Write (byte[]) throws IOException is used to write an array of
byte to the current output stream.
15. State the diff. b/w public voidflush() throws IOException& public void close()
throws IOException?
Ans:
flush() method flushes the current output stream Where Close() method is used
to close the current output stream.
Ans:
i) Public abstract int read() throws IOException.
ii) public int available () throws IOException.
iii) Public void close() throws IOException.
Ans:
Public void close () throws IOException.
Ans:
FileInputStream and FileOutputStream classes are used for file handling in java.
Ans:
FileInputStream class: FileInputStream is used to read data from file in a
character format with the help of read method. If file not present FileInputStream
terminate the program abnormally by throwing FilenotFound Exception.
Ans:
In this stream the data written into a byte array. buffer automatically grows as
data is written to it.
Ans:
ByteArrayOutputStream() creates a new byte array output stream with the initial
capacity of 32 bytes, throw its size increases if necessary.
Where ByteArrayOutputStream(int size) creates a new byte array output stream
,with a buffer capacity of the specified size ,in bytes.
22. State the commonly used methods of ByteArrayOutputStream class & Describe
it?
Ans:
Public synchronized void writeTo(outputstream out) throws IOException writes
the complete contents of this byteArrayOutputStream to the specified output
stream.
Ans:
SequenceInputStream class is used to read data from multiple streams.
Ans:
SequenceInputSteam(InputSteam s1,InputStream s2) creates a new input
stream by reading the data of two input stream in order,first s1 and then s2.
Where SequenceInputSteam(Enumeration e) creates a new input stream by
reading the data of an enumeration whose type is input stream.
Ans:
It used an internal buffer which add more efficiency than to write data directly
into a stream, which makes the performance fast.
Ans:
FileWriter class is used to write character-oriented data to the file.
Ans:
If you have to read and write the textual information then don’t use
FileInputStream & FileOutputStream class.
Ans:
FileReader class is used to read data from the file.
Ans:
The java.io.OutputStream.flush() method flushes this output stream and
forces any buffered output bytes to be written out. In otherwords The
java.io.BufferedWriter.flush() method flushes the characters from a write buffer
to the character or byte stream as an intended destination.
Ans:
CharArrayWriter class used to write data to multiple files.
Ans:
CharArrayWriter class implements Appendable interface
Ans:
There are many ways to read the data from keyboard. They are
i) InputStreamReader
ii) Console
iii) Scanner
iv) DataInputStream.etc..
Ans:
It connects to input stream of keyboard.and converts the byte oriented Stream
into character oriented stream.
Ans:
BufferedReader class used to read data line by line by readLine() method.
Ans:
Console class use to get input from the keyboard.
Ans:
System class provides a static method named console() that returns the unique
instance of console class.
37. State the diff. b/w String readLine() and String readLine(String
fmt,Object…args)?
Ans:
String readLine() is used to read a single line of text from the console .
Where String readLine(String fmt,Object…args) provides a formatted prompt
then reads the single line of text from the console.
38. State the diff. b/w char[] readPassword() and char[] readPassword(String
fmt,obj…arg)?
Ans:
char[] readPassword() is used to read password that is not being displayed on
the console. Where char[] readPassword(String fmt,obj…arg) provides a
formatted prompt then reads the password that is not being displayed in the
console.
39. State the use of java.util.Scanner class?
Ans:
java.util.Scanner class use to read input from the keyboard.
40. State the diff. b/w String next() and String nextLine() method?
Ans:
String next() returns the next token from the scanner
Where String nextLine() moves the scanner position to the next line and returns
the value as a string.
Ans:
PrintStream class provides methods to write data to another stream.
Ans:
Println() method terminates the line only.
Ans:
It prints the specified String value and terminates the line.
Ans:
It writes the formatted string to the current string.
Ans:
It prints the specified String value.
Ans:
It prints the specified object value.
Ans:
It is use to compress the data in the default compression format .it provide
facility to the other compression filters such as GZI Output stream.
Ans:
It is used to un compress the file in the deflate compression format. It provide
facility to the other un compression filter such as GZI Input Stream.
Ans:
It is a mechanism of writing the state of an object into a file. It is mainly used in
Hibernate ,JPA and EJB.it also use to travel objects state on the network.
Ans:
Extracting the state of object from file is known as Deserialization.
Ans:
The String class and all the Wrapper class by default implements serializable
interface.
Ans:
It is a Marker interface.it is just used to ‘mark’ java classes which support a
certain capability.
Ans:
It is used to write primitive data types and java objects to an OutputStream.
Ans:
Public ObjectOutputStream (OutputStream out) throws IOException use in
ObjectOutputStream class and its use is it create an ObjectOutputStream that
writes to the specified ObjectOutputStream .
Ans:
Public ObjectInputStream (InputStream in) throws IOException
Ans:
Public ObjectInputStream (InputStream in) throws IOException.
Ans:
Public final Object readObject() throwsIOException,ClassNotFoundException.
Ans:
Transient keyword use in Serialization.
Ans:
It will return the default value for transient variable.
Ans:
If file not present FileInputStream terminate the program abnormally by throwing
FileNotFoundException but if file not present FileOutputStream create a new file.
Ans:
readLine() is a predefined method present in BufferedReader class which use to
read data from byte in a string format.
Ans:
To create a Serializable object the class should be the child class of Serializable
interface.
Ans:
public Boolean isHidden () method tests whether the file named by this abstract
pathname is a hidden file.
Ans:
It automatically create a new empty file named by this abstract pathname if and
only if a file with this name does not yet exit.
Ans:
It deletes the file or directory denoted by this abstract pathname.
Ans:
It test whether the file or directory denoted by this abstract pathname exists.
Ans:
It test whether the file denoted by this abstract pathname is a normal file.
Ans:
It creates a new file instance by converting the given path name String into a
abstract pathname.
Ans:
It returns the name of the file or directory denoted by this abstract pathname.
Ans:
it creates the directory named by this abstract pathname.
74. What class allows you to read objects directly from a stream?
Ans:
FileInputStream class.
Ans:
Externalizable is an interface that enables you to define custom rules and your
own mechanism for serialization.