You are on page 1of 12

Lesson: 11 File Handling

Understanding Computer Files:


=> Storage Devices of a computer system can be broadly classified into two categories: volatile
storage and non-volatile Storage.
=> Volatile storage is temporary; values stored in variables are lost when a computer is
shutdown.
=> A java program that stores a value in a variable uses Random Access Memory (RAM).
=> Non-volatile storage is permanent storage; data is not lost when a computer is shutdown.
=> When java program is saved on a disk, we are using permanent storage.
=> A computer file is a collection of data stored on a non-volatile device.
=> File can be further classified broadly into two categories: text files and binary files.
=> Text files contain data that can be read in a text editor because the data has been encoded
using a scheme such as ASCII or Unicode. Files created through editors like gedit, vi, pico
are example of text file. They may have extensions like txt, java or c.
=> Binary files contain data that has not been encoded as text. Their contents are in binary
format, which means the data is accessed in terms of bytes. Some example extensions of
binary files are jpeg, mp3 and class.
=> Java language supports various operations that can be performed on file or on directories.
Like below:
1. Determining the path of a file of directory
2. Opening a file
3. Writing to a file
4. Reading from a file
5. Closing a file
6. Deleting a file
7. Querying the attributes of a file
=> Java provides built-in classes that contain methods to help us with these tasks. These
classes are present in java.io package.
File class in Java:
=> The java.io.File class encapsulates information about the properties of a file or a directory.
=> File class can be used to access attributes of files and directories.
=> We can create/rename/delete a file or directory.
=> There are nearly 30 methods of File Class that can be used to perform various operations on
a file or a directory.
1
Constructors of File Class:
=> File class provide following constructor to refer a file or a directory.
1. File(String path)
2. File(String directory_path, String file_name)
3. File(File directory, String file_name)
=> Java file object can be created using three ways as mentioned below:
1. By Specifying the path as File fileobj=new File(“/etc/passwd”);
2. By specifying directory and filename as two separate arguments
File fileobj = new File (“/etc”,”passwd”);
3. By using the reference to directory encapsulated in dirobj object
File dirobj = new File(“/etc);
File fileobj=new File(dirobj.”passwd);

Method of File class:

Method Description
boolean exists() Returns true if the file or directory exists, otherwise return false
boolean isFile() Returns true if the file exists, otherwise returns false
boolean isDirectory() Returns true if the directory exists, otherwise returns false
boolean isHidden() Returns true if the file or directory is hidden
String getAbsolutePath() Returns the absolute path of the file or directory
String getName() Returns the name of the file or directory referred by the object
String getPath() Returns the path to the file or directory
long length() Returns the number of bytes in that file
String[] list() Returns the name of files and directories in directory
File[] listFiles() Returns an array of abstract pathnames denoting the files in the
directory.

Introduction to Streams:
=> Till now, we haven‟t seen how to modify a file or display the content of a file. To perform
such operations we need to understand the concept of streams. Java uses stream classes
to carry out read and write operations on files.
=> For instance, keyboard is an input device while monitor is an output device.

2
=> Hard disk can be classified as both input and output devices as we can store and read data
from the files.
=> A stream is an abstract representation of an input or output device that is used as a source
or destination for data.
=> We can visualize a stream as a sequence of bytes that flows into the program or that flows
out of our program.
=> We can write data or read data using streams.
=> When we write data to stream, the stream is called an output stream.
=> The output stream can transfer data from the program to a file on a hard disk or a monitor or
to some other computer over the network
=> An input stream is used to read data from an external device to the program; it can transfer
data from keyboard or from the file on a hard disk to the program.
=> The main reason for using streams for input or output operations is to make our program
independent of the devices involved. Two advantages of streams are:
1. Programmer does not need to worry about the technical details of the device.
2. The program can work for a variety of input/output devices without any changes to the
source code.

Stream Classes in Java:


=> To understand byte streams and character streams, let us first try to differentiate between
character and byte representation.
=> A character is generally stored using ASCII or Unicode format but when it is used for
calculation purpose; its binary value is meaningful.
=> Java supports two types of streams, byte stream and character stream.
=> Streams that transfer data in the form of bytes to the file or devices are known as byte
stream or binary stream.
=> The files that are created using byte stream are known as binary files.
=> Suppose if we wish to store variables like integer, double or Boolean into a file, then we
must use binary files.
=> Binary files can also be used to store arrays or objects.
=> Java provides two set of classes, character stream class and binary stream class.
=> Character Stream classes present in java.io.package deal with the character/text data while
byte stream classes present in java.io package deal with binary data.

3
Stream Classes

Byte Stream Character Stream

Input Stream Reader Classes

Byte Stream Writer Classes

=> Java streams can be classified into two basic types, namely input stream and output stream.
=> An input stream reads data from the source (file, keyboard) while the output stream writes
data to the destination (file, output device).

Character Stream Classes:


=> Character stream classes are a group of classes available in java.io package. They can be
used to read and write 16-bit Unicode characters.
=> Character Stream classes can be further classified into Reader and Writer classes.

Object

Reader Writer

InputStreamRead OutputStreamWriter

BufferReader PrintWriter

FileReader FileWriter

4
Writer Classes:
=> Writer class is base class for writing a character stream. The abstract writer class defines
the functionality that is available for all character output streams.
=> The methods of Writer class can throw IOException.
Method Description
Void close() Closes the stream
Void write(int c) Write the lower 16 bits of „c‟ to the stream
Void write(String s) Writes string „s‟ to the stream

=> The OutputStreamWriter class extends Writer class. It converts stream of characters to a
stream of bytes.
=> A FileWriter class extends OutputStreamWriter and outputs characters to a file. Some of its
constructor are:
1. FileWriter(String filepath) throws IOException
2. FileWriter(File fileobj) throws IOException
3. FileWriter(String filepath,Boolean append) throws IOException
=> We can create an object of FileWriter as shown below:
FileWriter fwobject=new FileWriter(“/java/files/Charfile1.txt”);
Reader Classes:
=> Reader class is the base class for reading a character stream
Method Description
Void close() Closes the stream
Int read() Read next available character from the stream, it returns “-1” to indicate the
end of stream

=> The InputStreamReader class extends Reader class. It converts a stream of bytes to a
stream of characters.
1. FileReader(String filepath) throws FileNotFoundException
2. FileReader(File fileobj) throws FileNotFoundException
=> We can create an object of FileReader as shown below:
FileReader frobject=new FileReader(“/java/files/Charfile1.txt”);
Byte Stream Classes:
=> The FileInputStream and FileOutputStream classes in the java.io package give us the ability
to read and write bytes from and into any files in the disk. These classes are the sub-classes
of InputStream and OutputStream classes.

5
FileOutputStream:
=> The FileOutputStream is a subclass of OutputStream and is used to write bytes to the file or
some output stream.
Method Description
Void close() Closes this file output stream and releases any system resources
associated with the stream.
Void write(int b) Write the specified byte to this file output stream.
Void write(byte[] b) Write b.length bytes from the specified byte array to this file output
stream.

=> The constructor of FileOutputStream can accept either a string containing the path to the file
location or an object of the file class. Let us few constructor that are normally used:
1. FileOutputStream(String name) throws FileNotFoundException
2. FileOutputStream fosobject=new FileOutputStream(“/home/Akash/myfile.txt);
3. File obj=new File(“/home/Akash/myfile.txt”);
FileOutputStream fosobject=new FileOutputStream(fobj);
FileInputStream:
=> FileInputStream is a subclass of InputStream and is generally used to read byte data from
the files.
Method Description
Void close() Closes this file output stream and releases any system resources
associated with the stream.
Int read() Reads a byte of data from this input stream.
Int read(byte[] b) Reads up to b.length bytes of data from this input stream into an array
of bytes.

Processing Input from Keyboard:


=> There are many classes that facilitate the input from keyboard, here we will discuss two of
the most widely used technique, using Scanner class of java.util package, and using the
Console class of java.io package.
Scanner Class:
=> Scanner class belongs to the java.util package; it provides various methods to read input
from the Keyboard or from the file.
=> A special feature of this class is that it breaks the input string into tokens(words) using a
delimiter
6
1. Scanner(String str)
2. Scanner(InputStream isobject)
3. Scanner(File fobject) throws FileNotFoundException
=> A scanner class object can be created from a string, file object or InputStream object.

Method Description
Void close() Closes the Scanner
String next() Returns the next token
Boolean hasNext() Returns true if there is a token in input
Int nextInt() Scans the next token of the input as Int
Float nextFloat() Scans the next token of the input as Float
String nextLine() Scans the next token of the input as Line

Console Class:
=> Apart from the scanner class, there is another class java.io.Console which can be used to
get the input from the keyboard. We use this class especially when the input is to be typed in
hidden form (character must not be echoed on screen).

Method Description
String readLine() This method reads a single line of text from the
console.
Char[] readPassword() This method reads a password or passphrase from
the console with echoing disabled.
Console printf(String format, Object This method is used to write a formatted string to this
args() console‟s output stream using the specified format
string and arguments

************

7
Chapter – 11
1. Storage devices of a computer system is classified into how many categories?
(A) 2 (B) 3 (C) 4 (D) 5
A
2. Storage device of a computer system is classified into which categories?
(A) Primary storage, Secondary storage (B) Master storage, Basic storage
(C) Text storage, Binary storage (D) Volatile storage, Non-volatile storage
D
3. Which of the following is a temporary storage?
(A) Volatile storage (B) Non-volatile storage
(C) Both (A) and (B) (D) None of these
A
4. Which type of storage values store in variables are lost when computer is shutdown?
(A) Volatile storage (B) Non-volatile storage
(C) Both (A) and (B) (D) None of these
A
5. Random Access Memory (RAM) is example of which type of storage?
(A) Volatile storage (B) Non-volatile storage
(C) Permanent storage (D) All of these
A
6. Which of the following storage is permanent storage, data is not lost when a computer loses
power?
(A) Volatile storage (B) Non-volatile storage
(C) Both (A) and (B) (D) None of these
B
7. Which of the following refers to a collection of data stored on a non-volatile device in a
computer system?
(A) File (B) Folder (C) Function (D) Program
A
8. File can exists on which of the following permanent storage device?
(A) Hard disk (B) Optical disk (C) USB drive (D) All of these
D
9. Files can be classified broadly into how many categories?
(A) 2 (B) 3 (C) 4 (D) 5
A
10. Files can be classified broadly into which categories?
8
(A) Text file, Image file (B) Audio file, Video file
(C) Text file, Binary file (D) Class file, Java file
C
11. Which of the following is an example of text file?
(A) .txt, .java, .jpeg (B) .txt, .java, .mp3 (C) .txt, .java, .class (D) .txt, .java, .c
D
12. In Text file, data has been encoded using which scheme?
(A) Binary, Octal (B) Unicode, EBCDIC (C) ASCII, Unicode (D) ASCII, POSIX
C
13. Which of the following is example of text editor?
(A) edit, vi, pico (B) gedit, vi, pico (C) gedit, vi, text (D) gedit, vi, editor
B
14. Which of the following is an example of binary file?
(A) .jpeg, .mp3, .class (B) .jpeg, .mp3, .txt
(C) .jpeg, .mp3, .c (D) .jpeg, .mp3, .java
A
15. The data hierarchy occurs in which of the following order from the smallest to largest?
(A) file : character : field : record (B) file : character : record : field
(C) character : field : file : record (D) character : field : record : file
D
16. Which class encapsulated information about the properties of a file or a directory?
(A) java.File (B) java.in.File (C) java.File.io (D) java.io.File
D
17. Which method of File class returns true if file exists, otherwise returns false?
(A) boolean File() (B) boolean isFile()
(C) Boolean listFile() (D) Boolean getFile()
B
18. File class method boolean isFile() returns which type of value of File class?
(A) True or False (B) Yes or No (C) 0 or 1 (D) Y or N
A
19. Which method of File class returns true if the directory exists, otherwise returns false?
(A ) boolean Directory () (B) Boolean getDirectory()
(C) Boolean isDirectory() (D) Boolean listDirectory()
C
20. Which method of File class returns true, if the file or directory is hidden?
(A) boolean Hidden() (B) boolean isHidden()
9
(C) boolean Hide() (D) boolean isHide()
B
21. Which method of File class returns the path to the file or directory?
(A) String getName() (B) String getFile()
(C) String getDirectory() (D) String getPath()
D
22. Which method of File class returns the number of bytes in that file?
(A) long length() (B) length long() (C) file length() (D) file bytes()
A
23. Which method of File class returns the name of files and directories in a directory?
(A) String[] list() (B) String[] file() (C) String[] name() (D) String[] path()
A
24. Java uses which classes to carry out read and write operations on files?
(A) Text class (B) Write class (C) Stream class (D) Read class
C
25. Which of the following is an abstract representation of an Input and Output device that is
used as a source or destination for data?
(A) System (B) Stream (C) Section (D) Source
B
26. In Java, when we write data to stream, what is that stream called?
(A) Output steam (B) Input stream (C) Data stream (D) Line stream
27. Java supports which type of stream classes?
(A) Byte stream, String stream (B) Byte stream, Character stream
(C) Input stream, Output stream (D) Reader stream, Writer stream
B
28. Which stream is used to make text files and program codes in Java?
(A) Byte stream (B) Binary stream (C) Character stream (D) Boolean stream
C
29. Java streams can be classified into which types?
(A) Input stream, Output stream (B) System stream, Process stream
(C) Primary stream, Secondary stream (D) Internal stream, External stream
A
30. Which stream reads data from the source like file, keyboard?
(A) Input stream (B) Output stream (C) Byte stream (D) Character stream
A
31. Which stream writes data to the destination like file, output device?
10
(A) Input stream (B) Output stream (C) Byte stream (D) Character stream
B
32. Which package contains a collection of stream classes that support reading and writing in a
file?
(A) java.input (B) java.output (C) java.file (D) java.io
D
33. Character stream is classified into how many classes?
(A) 2 (B) 3 (C) 4 (D) 5
A
34. Character stream is classified into which classes?
(A) Reader class, Writer class (B) Byte class, Output class
(C) Input class, Output class (D) Text class, Binary class
A
35. Which is the base class for reading and writing a character stream respectively?
(A) Reader class, Writer class (B) Byte class, Output class
(C) Input class, Output class (D) Text class, Binary class
B
36. Which of the following are subclass of Reader class?
(A) InputStreamReader, OutputStreamReader
(B) ReadStream, WriteStream
(C) InputStreamReader, BufferedReader
(D) InputReader, OutputReader
C
37. Which abstract class defines the functionality that is available for all character input
streams?
(A) Input class (B) Output class (C) Reader class (D) Writer class
38. Which of the following exception occurs when there is a failed I/O operation?
(A) Arithmetic Exception (B) Failed Exception
(C) Close Exception (D) IO Exception
D
39. Which method of FileWriter class is used to closes the stream?
(A) void close() (B) void exit() (C) void stop() (D) void quit()
A
40. In FileReader class, int read() method returns what to identify the end of data in the stream?
(A) -1 (B) 0 (C) 1 (D) Null
A
11
41. Which FileInputStream class method reads a byte of data from input steam?
(A) void read() (B) read void() (C) void close() (D) int read()
D
42. Which FileOutStream class method writes b.length bytes from the specified byte array to file
output stream?
(A) void write (int b) (B) void write (byte [] b)
(C) void write (int length) (D) void write (byte length)
B
43. Scanner class belongs to which of the following package?
(A) java.scan (B) java.pack (C) java.ioFile (D) java.util
D
44. Which of the following is used as a separator between fields of a record?
(A) Space (B) Variable (C) Delimiter (D) Path
C
45. Which of the following is the default delimiter of Scanner class?
(A) Data space (B) Escape space (C) White space (D) Full space
C
46. Which Scanner class method is used to returns the next token?
(A) String hasNext() (B) String token()
(C) String next() (D) String isToken()
C
47. Which Scanner class method is sued to scans the next token of the input as Int?
(A) int nextInt() (B) next Int() (C) String nextInt() (D) boolean nextInt()
A
48. Which Scanner class method is used to scans the next token of the input as Line?
(A) String Line() (B) String next() (C) String nextLine() (D) String lineInput()
C
49. Which of the following class provides a method for reading password?
(A) Write class (B) Scanner class (C) Console class (D) Password class
C
50. Which Console class method reads a single line of text form the console?
(A) String textLine() (B) String readsLine()
(C) String writeLine() (D) String readLine()

=============

12

You might also like