File Handling in Java
File Handling in Java
CoursesTutorialsJavaPracticeContests
Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exceptio
Java
class GFG {
public static void main(String[] args)
{
Output
File Created!
https://www.geeksforgeeks.org/file-handling-in-java/ 1/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
Streams in Java
1. Input Stream:
The Java InputStream class is the superclass of all input streams. The input
stream is used to read data from numerous input devices like the keyboard,
network, etc. InputStream is an abstract class, and because of this, it is not
useful by itself. However, its subclasses are used to read data.
There are several subclasses of the InputStream class, which are as follows:
1. AudioInputStream
2. ByteArrayInputStream
3. FileInputStream
4. FilterInputStream
5. StringBufferInputStream
6. ObjectInputStream
https://www.geeksforgeeks.org/file-handling-in-java/ 2/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
// Creating an InputStream
InputStream obj = new FileInputStream();
Methods of InputStream
S Method Description
No.
read(byte[] array) Reads byte from the stream and stores that byte in
2
() the specified array.
2. Output Stream:
We use cookies to ensure you have the best browsing experience on our website. By using
The
our site, yououtput stream
acknowledge ishave
that you usedreadtoand
write dataour
understood toCookie
numerous output devices like the
Policy & Privacy
monitor, file, etc. OutputStream Policy is an abstract superclass that represents an
https://www.geeksforgeeks.org/file-handling-in-java/ 3/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
1. ByteArrayOutputStream
2. FileOutputStream
3. StringBufferOutputStream
4. ObjectOutputStream
5. DataOutputStream
6. PrintStream
Creating an OutputStream
// Creating an OutputStream
OutputStream obj = new FileOutputStream();
Methods of OutputStream
S. Method Description
No.
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge
Based that youtype,
on the data have read and understood
there are twoour Cookieof
types Policy & Privacy:
streams
Policy
https://www.geeksforgeeks.org/file-handling-in-java/ 4/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
1. Byte Stream:
This stream is used to read or write byte data. The byte stream is again
subdivided into two types which are as follows:
Byte Input Stream: Used to read byte data from different devices.
Byte Output Stream: Used to write byte data to different devices.
2. Character Stream:
This stream is used to read or write character data. Character stream is again
subdivided into 2 types which are as follows:
Owing to the fact that you know what a stream is, let’s polish up File
Handling in Java by further understanding the various methods that are
useful for performing operations on the files like creating, reading, and
writing files.
https://www.geeksforgeeks.org/file-handling-in-java/ 5/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
Let us now get acquainted with the various file operations in Java.
The following are the several operations that can be performed on a file in
Java :
Create a File
Read from a File
Write to a File
Delete a File
1. Create a File
In order to create a file in Java, you can use the createNewFile() method.
If the file is successfully created, it will return a Boolean value true and
false if the file already exists.
Java
We use//cookies
Import the File class
to ensure you have the best browsing experience on our website. By using
import java.io.File;
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
// Import the IOException class to handle errors
https://www.geeksforgeeks.org/file-handling-in-java/ 6/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
import java.io.IOException;
try {
File Obj = new File("myfile.txt");
if (Obj.createNewFile()) {
System.out.println("File created: "
+ Obj.getName());
}
else {
System.out.println("File already exists.");
}
}
catch (IOException e) {
System.out.println("An error has occurred.");
e.printStackTrace();
}
}
}
Output
2. Read from a File: We will use the Scanner class in order to read contents
from a file. Following is a demonstration of how to read contents from a file
in Java :
Java
https://www.geeksforgeeks.org/file-handling-in-java/ 7/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
while (Reader.hasNextLine()) {
String data = Reader.nextLine();
System.out.println(data);
}
Reader.close();
}
catch (FileNotFoundException e) {
System.out.println("An error has occurred.");
e.printStackTrace();
}
}
}
Output
3. Write to a File: We use the FileWriter class along with its write() method
in order to write some text to the file. Following is a demonstration of how
to write text to a file in Java :
Java
https://www.geeksforgeeks.org/file-handling-in-java/ 8/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
Output
Java
Output
Feeling lost in the vast world of Backend Development? It's time for a
change! Join our Java Backend Development - Live Course and embark on an
exciting journey to master backend development efficiently and on schedule.
What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/file-handling-in-java/ 9/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
78 Suggest improvement
Next
Similar Reads
File Handling in Java with CRUD Java File Handling Programs
operations
How to Execute SQL File with Java Different Ways to Copy Content From
using File and IO Streams? One File to Another File in Java
Java Program to Read Content From How to Convert a Kotlin Source File to a
One File and Write it into Another File Java Source File in Android?
How to Extract File Extension From a How to Create a File with a Specific File
File Path String in Java? Attribute in Java?
S shreyasna…
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/file-handling-in-java/ 10/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial
https://www.geeksforgeeks.org/file-handling-in-java/ 11/13
12/05/2024, 22:33 File Handling in Java - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/file-handling-in-java/ 13/13