0% found this document useful (0 votes)
17 views

Math codes

Uploaded by

ahileshroy007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Math codes

Uploaded by

ahileshroy007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

File Handling

1.
Aim:
Write a Java program that:
Creates a file named testFile.txt.
Checks if the file exists.
Deletes the file if it exists and prints a message indicating whether it was successfully deleted

Source Code:
import java.io.File;
import java.io.IOException;

public class Main {


public static void main(String[] args) {
try {
File file = new File("testFile.txt");
if(file.createNewFile()) {
System.out.println("File created.");
}
if(file.exists()) {
System.out.println("File exists.");
if(file.delete()) {
System.out.println("File deleted.");
}
else{
System.out.println("Failed to delete file.");
}
}
}
catch (IOException e) {
System.err.println("An error has occurred.");
}
}
}

Execution:

Input/Output:

------------------------------------------------------------------------------------------------------------------------------
2.
Aim:
Write a Java program that:
Reads a file named input.txt line by line using BufferedReader.
Prints each line to the console.

Source Code:
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class Main {


public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
}
catch (IOException e) {
System.err.println("Error occurred.");
}
}
}

Execution:
Input/Output:

------------------------------------------------------------------------------------------------------------------------------
3.
Aim:
Write a Java program that:
Opens or creates a file named output.txt.
Writes the numbers from 1 to 10, each on a new line.
Closes the file after writing.

Source Code:
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;

public class Main {


public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
for(int i=1;i<=10;i++) {
writer.write(Integer.toString(i));
writer.newLine();
}
System.out.println("File succesfully written.");
}
catch (IOException e) {
System.err.println("Error occurred.");
}
}
}

Execution:
Input/Output:

------------------------------------------------------------------------------------------------------------------------------

4.
Aim:
Write a Java program that:
Reads content from a file named source.txt.
Writes this content to another file named destination.txt.
The program should use FileInputStream and FileOutputStream.

Source Code:
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Main {


public static void main(String[] args) {
try (
FileInputStream source = new FileInputStream("source.txt");
FileOutputStream destination = new FileOutputStream("destination.txt");
){
int data;
while((data = source.read()) != -1) {
destination.write(data);
}
System.out.println("Data transferred.");
}
catch (IOException e) {
System.err.println("Error occurred.");
}
}
}

Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------
5.
Aim:
Write a Java program that:
Reads a file named document.txt.
Counts and displays the total number of words in the file.

Source Code:
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class Main {


public static void main(String[] args) {
int wordCount = 0;
try (BufferedReader reader = new BufferedReader(new FileReader("document.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] words = line.split("\\s+");
wordCount += words.length;
}
System.out.println("This document has " + wordCount + " word(s).");
}
catch (IOException e) {
System.err.println("Error occurred.");
}
}
}
Execution:

Input/Output:

------------------------------------------------------------------------------------------------------------------------------

6.
Aim:
Write a Java program that:
Opens an existing file named log.txt.
Appends the current date and time to the file on a new line (use LocalDateTime.now()).
Closes the file after appending.

Source Code:
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.time.LocalDateTime;

public class Main {


public static void main(String[] args) {
LocalDateTime currentTime = LocalDateTime.now();
try (BufferedWriter writer = new BufferedWriter(new FileWriter("log.txt"))) {
writer.write(currentTime.toString());
writer.newLine();
}
catch (IOException e) {
System.err.println("Error occurred.");
}
}
}
Execution:

Input/Output:

------------------------------------------------------------------------------------------------------------------------------

7.
Aim:
Write a Java program that:
Reads a file named notes.txt.
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 java.io.IOException;
import java.nio.file.*;

public class Main {


public static void main(String[] args) {
String old = "Slave 1";
String new_ = "Firespray";
try{
String oldContent = Files.readString(Path.of("notes.txt"));
String newContent = oldContent.replace(old, new_);
Files.writeString(Path.of("notes.txt"), newContent);
}
catch (IOException e) {
System.err.println("Error occurred.");
}
}
}

Execution:

Input/Output:
Before:
After:

------------------------------------------------------------------------------------------------------------------------------

8.
Aim:
Write a Java program that:
Reads a file named sample.txt.
Counts and displays the number of lines, words, and characters in the file.

Source Code:
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

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("sample.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
lineCount++;

charCount+= line.length();

String[] words = line.split("\\s+");


wordCount += words.length;
}
System.out.println("This document has: ");
System.out.println("Characters: " + charCount);
System.out.println("Words: " + wordCount);
System.out.println("Lines: " + lineCount);
}
catch (IOException e) {
System.err.println("Error occurred.");
}
}
}
Execution:

Input/Output:
------------------------------------------------------------------------------------------------------------------------------

9.
Aim:
Write a Java program that:
Reads two files named file1.txt and file2.txt.
Merges their content and writes it to a new file named merged.txt.

Source Code:
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.BufferedWriter;

public class Main {


public static void main(String[] args) {
try (
BufferedReader reader1 = new BufferedReader(new FileReader("file1.txt"));
BufferedReader reader2 = new BufferedReader(new FileReader("file2.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("merged.txt"))
){
String line;
while ((line = reader1.readLine()) != null) {
writer.write(line);
writer.newLine();
}

// Read and write content from file2


while ((line = reader2.readLine()) != null) {
writer.write(line);
writer.newLine();
}

System.out.println("Files merged.");
}
catch (IOException e) {
System.err.println("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 java.io.File;

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 (directory.isDirectory()) {
File[] objects = directory.listFiles();

if (objects != null) {
for (File object : objects) {
String type = object.isDirectory() ? "Directory" : "File";
long size = object.length();
System.out.println("Name: " + object.getName());
System.out.println("Type: " + type);
System.out.println("Size: " + size + " bytes");
System.out.println("----------------------------");
}
}
}
}
}

Execution:
Input/Output:
------------------------------------------------------------------------------------------------------------------------------

Result:
The outputs of the 10 problems were obtained and verified.

You might also like