Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views10 pages

Lecture 11 - File IO in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Lecture 11 - File IO in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

File IO Operations

Course Code: Course Title: Object Oriented Programming -


1(JAVA)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: Week No: 12 Semester: Summer 21-22


Lecturer: Mazid-Ul-Haque, [email protected]
Lecture Outline

1. File Class
2. Methods of File Class
3. File Write Operation
4. File Read Operation
File IO
• 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:

import java.io.File; // Import the File class


File myObj = new File("filename.txt"); // Specify the
filename
File IO
Methods of File Class

Method Type Description


canRead() Boolean Tests whether the file is readable or not
canWrite() Boolean Tests whether the file is writable or not
createNewFile() Boolean Creates an empty file
delete() Boolean Deletes a file
exists() Boolean Tests whether the file exists
getName() String Returns the name of the file
getAbsolutePath() String Returns the absolute pathname of the file
length() Long Returns the size of the file in bytes
list() String[] Returns an array of the files in the directory
mkdir() Boolean Creates a directory
Creating file

• 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.
• 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)
Creating file

• To create a file in a specific directory (requires


permission), specify the path of the file and use double
backslashes to escape the "\" character.

• File myObj = newFile("C:\\Users\\MyName\\filename.txt");


Writing in File

• We use the FileWriter class together with its write()


method to write some text to the file we created.
• Note that when you are done writing to the file, you
should close it with the close() method
Reading from File

• We create object of FileReader class to read some


text from a file we created.
• We create BufferedReader object using the
FileReader class object to read the file content.
• Note that when you are done reading to the file, you
should close it with the close() method
Books

1. Java Complete Reference, 7th Edition, By Herbert Schildt.

2. A Programmer's Guide to Java SE 8 Oracle Certified Associate, Khalid A. MughalRolf W.


Rasmussen

3. Java How to Program Java, 9th Edition, By Deitel and Deitel.

4. The Java Language Specification, By J. Gosling, B. Joy, G. Steele, G.Bracha and A.


Buckley

5. Introduction to Programming Using Java, 6th Edition, By David j. Eck

6. Head First Java, By Kathy Sierra and Bert Bates


References

1. 1. Java Complete Reference, 7th Edition, By Herbert Schildt.

2. A Programmer's Guide to Java SE 8 Oracle Certified Associate, Khalid A.


MughalRolf W. Rasmussen

2. The Java Tutorials. http://docs.oracle.com/javase/tutorial/

You might also like