java-File-Handling
java-File-Handling
Lesson 12
1. File handling is an important part of any application.
2. The File class from the java.io package, allows us to
work with files.
3. To use the File class, create an object of the class,
and specify the filename or directory name.
Files
• Files are stored on disks
• Each files consist of multiple lines composed of
characters
• Each line ends with an end of line character
• The file itself may have an end of file character
• Programmers often need to read or write files stored
on disks
Streams
• Stream: an object that either delivers data to its destination (screen, file,
etc.) or that takes data from a source (keyboard, file, etc.)
– it acts as a buffer between the data source and destination
• Input stream: a stream that provides input to a program
– System.in is an input stream
• Output stream: a stream that accepts output from a program
– System.out is an output stream
• A stream connects a program to an I/O object
– System.out connects a program to the screen
– System.in connects a program to the keyboard
Text File I/O
• Important classes for text file output (to the file)
– PrintWriter
– FileOutputStream [or FileWriter]
• Important classes for text file input (from the file):
– BufferedReader
– FileReader
• FileOutputStream and FileReader take file names as arguments.
• PrintWriter and BufferedReader provide useful methods for easier writing
and reading.
• Usually need a combination of two classes
• To use these classes your program needs a line like the following:
• import java.io.*;
• Java File class represents the files and directory
pathnames in an abstract manner.
• This class is used for creation of files and
directories, file searching, file deletion, etc.
• The File object represents the actual file/directory
on the disk.
File Class Methods
Java Create and Write to Files
• To create a file in Java, you can use the createNewFile()
method.
• This method returns a boolean value: true if the file was
successfully created, and false if the file already exists.
• Note that the method is enclosed in a try...catch block.
• This is necessary because it throws an IOException if an
error occurs (if the file cannot be created for some
reason):
Java File Operation Method
Creating a File
Writing File to a Specific Directory
• To create a file in a specific directory (requires permission), specify
the path of the file and use double backslashes to escape the "\"
character (for Windows).
• On Mac and Linux you can just write the path, like:
/Users/name/filename.txt
• For example:
• read() - reads a single character from the internal buffer of the reader
• read(char[] array) - reads the characters from the reader and stores in
the specified array
• read(char[] array, int start, int length) - reads the number of
characters equal to length from the reader and stores in the specified
array starting from the position start
Methods of BufferedReader
• read: read a char at a time
• readLine: read a line into a String
• close: close BufferedReader stream
Example
Example Cont…
• In this example, we have created
a buffered reader named input.
The buffered reader is linked
with the input.txt file.
{ CheckEvenOdd
CheckEvenOdd
comsats.class
.class
.java
Factorial.class
Factorial.java
filename.txt
// returns an array of all files String[] GradeBook.java
GradeBookTes t.java
hs_err_pid3692 .log
fileList = file.list(); hs_err_pid3832
hs_err_pid4136
.log
.log
jadoon.java
khan.java
lab.class
{ MyClass.class
MyClass.java
Pet.class
System.out.println(str); count++; RefVarofTypeI
RefVarofTypeI
terface.class
terface.java
RollDie.class
RollDie.java
s1.class
} s1.java
Student.class
StudentTest.clas s
} xyz.class xyz.java
Total Files: 53
}
FileInputStream
• A FileInputStream obtains input bytes from a file in a file
system.
• What files are available depends on the host environment.
• FileInputStream is meant for reading streams of raw bytes
such as image data.
• For reading streams of characters, consider using FileReader.
Counting Number of Characters in a File
Counting Number of Characters in a File(Cont…)
Counting Number of Characters in a File(Cont…)
Output