File Handling
in Java
Dr.Rab Nawaz Jadoon
Department of Computer Science Assistant Professor
COMSATS University, Abbottabad
DCS Pakistan
COMSATS University, Islamabad
(Abbottabad Campus)
Object Oriented Programming (OOP)
File handling in Java
◼ File handling is an important part of any
application.
◼ The File class from the java.io package, allows
us to work with files.
◼ To use the File class, create an object of the
class, and specify the filename or directory
name.
◼ Example
❖ On the next slide
Department of Computer Science 2
Cont…
1. Java File class represents the files and directory pathnames
in an abstract manner.
2. This class is used for creation of files and directories, file
searching, file deletion, etc.
3. The File object represents the actual file/directory on the
disk.
Department of Computer Science 3
File Class Methods
Department of Computer Science 4
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):
Department of Computer Science 5
JAVA File Operation Method
Department of Computer Science 6
Creating a File
File created: filename.txt
Department of Computer Science 7
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
File myObj = new File("C:\\Users\\MyName\\filename.txt");
Department of Computer Science 8
Write to a File
◼ In the following example, we use the FileWriter
class together with its write() method to write
some text to the file we created in the example
above.
◼ Note that when you are done writing to the file,
you should close it with the close() method:
Department of Computer Science 9
Example Program
Department of Computer Science 10
Read a File
❖ In the following example, we use the Scanner class
to read the contents of the text file.
Department of Computer Science 11
Getting File Information
◼ To get more information about a file, use any of
the File methods:
Department of Computer Science 12
Deleting a File
Department of Computer Science 13
Listing all files in a Folder
import java.io.File;
abc.class
abc.java
AgeCalculator.class
AgeCalculator.java
public class ListFilesinFolder ascii.class
ascii.java
{ CheckEvenOdd.class
CheckEvenOdd.java
comsats.class
public static void main(String[] args) comsats.java
CountFilesinFolder.class
CountFilesinFolder.java
{ CreateFile.class
CreateFile.java
DiceRoller.class
int count=0; DiceRoller.java
Dog.class
example1.class
// creates a file object example1.java
ExampleThrows.class
File file = new File("C:\\Users\\Rab Nawaz\\Desktop\\JAVA"); ExampleThrows.java
exceptions.class
exceptions.java
Factorial.class
Factorial.java
filename.txt
// returns an array of all files GradeBook.java
GradeBookTest.java
hs_err_pid3692.log
String[] fileList = file.list(); hs_err_pid3832.log
hs_err_pid4136.log
jadoon.java
khan.java
lab.class
for(String str : fileList) lab.java
Movieshop.class
Movieshop.java
{ MyClass.class
MyClass.java
Pet.class
System.out.println(str); RefVarofTypeInterface.class
RefVarofTypeInterface.java
RollDie.class
count++; RollDie.java
s1.class
s1.java
} Student.class
StudentTest.class
System.out.println(“Total Files:” +count); StudentTest.java
WriteToFile.class
WriteToFile.java
} xyz.class
xyz.java
Total Files: 53
}
Department of Computer Science 14
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.
Department of Computer Science 15
Counting Number of Characters in a
file
Department of Computer Science 16
Cont…
Department of Computer Science 17
Cont…
Output
Department of Computer Science 18
Department of Computer Science 19