📁 A repository showcasing various file handling operations in Java.
This repository provides examples and code snippets for performing file handling operations in Java. File handling is an essential aspect of many Java applications, allowing you to read, write, copy, and manipulate files on the local file system.
File handling is a critical aspect of any programming language.
File handling refers to manipulating files and directories on the file system. This article will provide an overview of file handling in Java.
java.io.File Class
The File class in Java is the primary way to handle files and directories. It is part of the java.io package, which is used for input and output operations.
The File class has two constructors:
1️⃣ One that takes a String representing the file path
2️⃣ Another File object representing the file path
import java.io.*;
public class ReadFileExample
{
public static void main(String args[])
{
try{
File file = new File("C:\\Users\\masai\\Desktop\\test.txt");
FileReader fr=new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s=null;
while ((s = br.readLine()) != null)
System.out.println(s);
}
catch(Exception ex)
{
System.out.println(“Exception=”+ex);
}
}
}
import java.io.*;
class FileWriterDemo
{
public static void main(String[] args) throws IOException
{
try{
File file = new File("C:\\Users\\sharp\\Desktop\\out.txt"); //file being created if it does not exist.
FileWriter fw=new FileWriter(file);
fw.write("Content to be written");
fw.close();
System.out.println("File written");
}
catch(Exception ex)
{
System.out.println("Exception is"+ex);
}
}
```
}
The repository includes the following file handling concepts:
- 📥 BufferedInputStream: Demonstrates how to efficiently read data from a file using a buffered input stream.
- 📤 BufferedWriter: Shows how to write data to a file using a buffered writer.
- 📁 FileOperations: Provides various file operations, such as checking file existence, creating a new file, deleting a file, etc.
- 📄 FileContentCopy: Illustrates how to copy the contents of one file to another.
- 📄 FileMethods: Demonstrates different methods available for working with files, such as getting file size, file last modified timestamp, etc.
- 📖 FileReading: Shows how to read the contents of a file using various techniques, including FileReader, FileInputStream, etc.
- 🔄 FileRenaming: Provides examples of renaming files in Java.
- ✍️ FileWriting: Illustrates how to write content to a file using different methods, such as FileWriter, FileOutputStream, etc.
- 📝 ProgramForCreatingFile: Contains a sample program that creates a new file with predefined content.
To explore the examples and run the code in this repository, follow these steps:
-
Clone the repository to your local machine using the following command:
git clone https://github.com/Shubh2-0/File-Handling-In-Java.git
-
Open the cloned directory in your preferred Java IDE.
-
Navigate to the specific file or example you want to explore.
-
Compile and run the Java code to see the file handling operations in action.
🌟 Star this repository if you find it helpful!
If you want to contact me, you can reach me through below handles.