Math codes
Math codes
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;
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;
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;
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;
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;
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;
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.*;
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;
charCount+= line.length();
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;
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;
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.