File Handling
1.
Aim:
Write a Java program that:
Creates a file named [Link].
Checks if the file exists.
Deletes the file if it exists and prints a message indicating whether it was successfully deleted
Source Code:
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try {
File file = new File("[Link]");
if([Link]()) {
[Link]("File created.");
}
if([Link]()) {
[Link]("File exists.");
if([Link]()) {
[Link]("File deleted.");
}
else{
[Link]("Failed to delete file.");
}
}
}
catch (IOException e) {
[Link]("An error has occurred.");
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
2.
Aim:
Write a Java program that:
Reads a file named [Link] line by line using BufferedReader.
Prints each line to the console.
Source Code:
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("[Link]"))) {
String line;
while((line = [Link]()) != null) {
[Link](line);
}
}
catch (IOException e) {
[Link]("Error occurred.");
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
3.
Aim:
Write a Java program that:
Opens or creates a file named [Link].
Writes the numbers from 1 to 10, each on a new line.
Closes the file after writing.
Source Code:
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("[Link]"))) {
for(int i=1;i<=10;i++) {
[Link]([Link](i));
[Link]();
}
[Link]("File succesfully written.");
}
catch (IOException e) {
[Link]("Error occurred.");
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
4.
Aim:
Write a Java program that:
Reads content from a file named [Link].
Writes this content to another file named [Link].
The program should use FileInputStream and FileOutputStream.
Source Code:
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try (
FileInputStream source = new FileInputStream("[Link]");
FileOutputStream destination = new FileOutputStream("[Link]");
){
int data;
while((data = [Link]()) != -1) {
[Link](data);
}
[Link]("Data transferred.");
}
catch (IOException e) {
[Link]("Error occurred.");
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
5.
Aim:
Write a Java program that:
Reads a file named [Link].
Counts and displays the total number of words in the file.
Source Code:
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
int wordCount = 0;
try (BufferedReader reader = new BufferedReader(new FileReader("[Link]"))) {
String line;
while ((line = [Link]()) != null) {
String[] words = [Link]("\\s+");
wordCount += [Link];
}
[Link]("This document has " + wordCount + " word(s).");
}
catch (IOException e) {
[Link]("Error occurred.");
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
6.
Aim:
Write a Java program that:
Opens an existing file named [Link].
Appends the current date and time to the file on a new line (use [Link]()).
Closes the file after appending.
Source Code:
import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
LocalDateTime currentTime = [Link]();
try (BufferedWriter writer = new BufferedWriter(new FileWriter("[Link]"))) {
[Link]([Link]());
[Link]();
}
catch (IOException e) {
[Link]("Error occurred.");
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
7.
Aim:
Write a Java program that:
Reads a file named [Link].
Replaces every occurrence of a specific word (e.g., “Java” with another word (e.g.,
“Python”).
Saves the modified content back to the same file.
Source Code:
import [Link];
import [Link].*;
public class Main {
public static void main(String[] args) {
String old = "Slave 1";
String new_ = "Firespray";
try{
String oldContent = [Link]([Link]("[Link]"));
String newContent = [Link](old, new_);
[Link]([Link]("[Link]"), newContent);
}
catch (IOException e) {
[Link]("Error occurred.");
}
}
}
Execution:
Input/Output:
Before:
After:
------------------------------------------------------------------------------------------------------------------------------
8.
Aim:
Write a Java program that:
Reads a file named [Link].
Counts and displays the number of lines, words, and characters in the file.
Source Code:
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
try (BufferedReader reader = new BufferedReader(new FileReader("[Link]"))) {
String line;
while ((line = [Link]()) != null) {
lineCount++;
charCount+= [Link]();
String[] words = [Link]("\\s+");
wordCount += [Link];
}
[Link]("This document has: ");
[Link]("Characters: " + charCount);
[Link]("Words: " + wordCount);
[Link]("Lines: " + lineCount);
}
catch (IOException e) {
[Link]("Error occurred.");
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
9.
Aim:
Write a Java program that:
Reads two files named [Link] and [Link].
Merges their content and writes it to a new file named [Link].
Source Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try (
BufferedReader reader1 = new BufferedReader(new FileReader("[Link]"));
BufferedReader reader2 = new BufferedReader(new FileReader("[Link]"));
BufferedWriter writer = new BufferedWriter(new FileWriter("[Link]"))
){
String line;
while ((line = [Link]()) != null) {
[Link](line);
[Link]();
}
// Read and write content from file2
while ((line = [Link]()) != null) {
[Link](line);
[Link]();
}
[Link]("Files merged.");
}
catch (IOException e) {
[Link]("Error occurred.");
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
10.
Aim:
Write a Java program that:
Lists all the files and directories in a specified directory (e.g.,”C:/Documents”).
Prints the name, size, and whether each item is a file or a directory.
Source Code:
import [Link];
public class Main {
public static void main(String[] args) {
String directoryPath = "C:\\Users\\Aadhav Nagarajan\\OneDrive\\Desktop\\Java\\
Homework\\test";
File directory = new File(directoryPath);
if ([Link]()) {
File[] objects = [Link]();
if (objects != null) {
for (File object : objects) {
String type = [Link]() ? "Directory" : "File";
long size = [Link]();
[Link]("Name: " + [Link]());
[Link]("Type: " + type);
[Link]("Size: " + size + " bytes");
[Link]("----------------------------");
}
}
}
}
}
Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
Result:
The outputs of the 10 problems were obtained and verified.