File Handling
File Handling
AIM:
1. Java program to creating a program that reads the content of a text file named
input.txt, converts all the text to uppercase, and writes it into a new file named
output.txt.
2. Java program to create a new file by removing all the spaces from the content of an
existing file.
3. Java program that have two text files: file1.txt and file2.txt. Write a program to
merge their contents into a new file named merged.txt.
4. Java program that are tasked with counting the number of characters in a file named
data.txt.
5. Java program to create a new file that removes all vowels from an existing file
input.txt
1.ALGORITHM:
1. Define the input file (input.txt) and output file (output.txt) paths as strings.
2. Open the input file using FileInputStream and the output file using FileOutputStream.
3. Read each byte from the input file in a loop until the end of the file is reached.
6. Close both the input and output file streams after processing is complete.
package Filehandling;
import java.io.*;
try {
int character;
fos.write(Character.toUpperCase(character));
fis.close();
fos.close();
catch (FileNotFoundException e) {
catch (IOException e) {
} }
}
2.ALGORITHM:
1. Define the input file (original.txt) and output file (nospace.txt) paths as strings.
2. Open the input file using FileInputStream and the output file using FileOutputStream.
3. Read each byte from the input file in a loop until the end of the file is reached.
4. Check if the byte represents a space character (ASCII value 32), and skip writing it to
the output file.
5. For all non-space characters, write the byte to the output file using FileOutputStream.
6. Close both the input and output file streams after processing is complete.
PROGRAM:
import java.io.*;
try {
int character;
if (character != 32) {
fos.write(character);
OUTPUT:
}
fis.close();
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
3.ALGORITHM:
4. Read the contents of file1.txt byte by byte and write them to merged.txt.
5. Write a newline character (\n) to merged.txt to separate the contents of the two files.
6. Read the contents of file2.txt byte by byte and write them to merged.txt.
7. Close all the file streams (fis1, fis2, fos) to release the resources.
PROGRAM:
import java.io.*;
public class q3 {
try {
int character;
fos.write(character);
fos.write('\n');
fos.write(character);
fis1.close();
fis2.close();
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}}}
OUTPUT:
4.ALGORITHM:
PROGRAM:
import java.io.*;
public class q4 {
int charCount = 0;
try {
int character;
charCount++;
}}
fr.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
1. Open input.txt using FileReader to read the file content character by character.
2. Open output.txt using FileWriter to write the modified content.
3. Read each character from input.txt.
4. Check if the character is a vowel (both uppercase and lowercase) using a helper
method isVowel().
5. If the character is not a vowel, write it to output.txt.
6. Continue until the entire file is processed.
7. Close both the FileReader and FileWriter to release the resources.
PROGRAM:
import java.io.*;
public class q5 {
try {
int character;
if (!isVowel((char) character)) {
fw.write(character);
fr.close();
fw.close();
} catch (IOException e) {
System.out.println("Error: An I/O error occurred while processing the file.");
return lowerC == 'a' || lowerC == 'e' || lowerC == 'i' || lowerC == 'o' || lowerC == 'u';}}
Criteria Marks
Record Preparation / 30
Viva /10
Total /100
RESULT:
Thus, the programs to
6. Java program to creating a program that reads the content of a text file named
input.txt, converts all the text to uppercase, and writes it into a new file named
output.txt.
7. Java program to create a new file by removing all the spaces from the content of an
existing file.
8. Java program that have two text files: file1.txt and file2.txt. Write a program to
merge their contents into a new file named merged.txt.
9. Java program that are tasked with counting the number of characters in a file named
data.txt.
10.Java program to create a new file that removes all vowels from an existing file
input.txt
have been compiled and executed successfully.