0% found this document useful (0 votes)
11 views13 pages

File Handling

File handling java codes

Uploaded by

rajiv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

File Handling

File handling java codes

Uploaded by

rajiv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Ex.

No:8 PROGRAMS ON FILE HANDLING


Date:19.11.2024

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.

4. Convert each byte (character) to uppercase using Character.toUpperCase().

5. Write the uppercase character to the output file using FileOutputStream.

6. Close both the input and output file streams after processing is complete.

7. Handle FileNotFoundException and IOException exceptions to manage errors during


file operations.
OUTPUT:
PROGRAM:

package Filehandling;

import java.io.*;

public class Filehandling1 {

public static void main(String[] args) {

String inputFile = "input.txt";

String outputFile = "output.txt";

try {

FileInputStream fis = new FileInputStream(inputFile);

FileOutputStream fos = new FileOutputStream(outputFile);

int character;

while ((character = fis.read()) != -1) {

fos.write(Character.toUpperCase(character));

System.out.println("File content has been successfully converted to uppercase and


saved to " + outputFile);

fis.close();

fos.close();

catch (FileNotFoundException e) {

System.out.println("Error: The file " + inputFile + " was not found");

catch (IOException e) {

System.out.println("Error: An I/O error occurred while processing the file");

} }

}
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.

7. Handle FileNotFoundException and IOException exceptions to manage errors during


file operations.

PROGRAM:

import java.io.*;

public class Filehandling2{

public static void main(String[] args) {

String inputFile = "input.txt";

String outputFile = "nospace.txt";

try {

FileInputStream fis = new FileInputStream(inputFile);

FileOutputStream fos = new FileOutputStream(outputFile);

int character;

while ((character = fis.read()) != -1) {

if (character != 32) {

fos.write(character);
OUTPUT:
}

System.out.println("Spaces have been successfully removed and saved to " +


outputFile);

fis.close();

fos.close();

} catch (FileNotFoundException e) {

System.out.println("Error: The file " + inputFile + " was not found");

} catch (IOException e) {

System.out.println("Error: An I/O error occurred while processing the file");}

3.ALGORITHM:

1. Open file1.txt using FileInputStream to read its contents.

2. Open file2.txt using FileInputStream to read its contents.

3. Open merged.txt using FileOutputStream to write the combined content.

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 {

public static void main(String[] args) {


OUTPUT:
String file1 = "file1.txt";

String file2 = "file2.txt";

String mergedFile = "merged.txt";

try {

FileInputStream fis1 = new FileInputStream(file1);

FileInputStream fis2 = new FileInputStream(file2);

FileOutputStream fos = new FileOutputStream(mergedFile);

int character;

while ((character = fis1.read()) != -1) {

fos.write(character);

fos.write('\n');

while ((character = fis2.read()) != -1) {

fos.write(character);

System.out.println("Files have been successfully merged into " + mergedFile);

fis1.close();

fis2.close();

fos.close();

} catch (FileNotFoundException e) {

System.out.println("Error: One of the files was not found");

} catch (IOException e) {

System.out.println("Error: An I/O error occurred while processing the files");

}}}
OUTPUT:
4.ALGORITHM:

1. Open data.txt using FileReader to read the content character by character.


2. Initialize a counter charCount to zero.
3. Read each character from the file.
4. Check if the character is neither a space (' ') nor a line break ('\n' or '\r').
5. Increment the counter charCount for each valid character (non-space and non-line
break).
6. After reading the entire file, display the total count of characters.
7. Close the FileReader to release resources.

PROGRAM:

import java.io.*;

public class q4 {

public static void main(String[] args) {

String fileName = "input.txt";

int charCount = 0;

try {

FileReader fr = new FileReader(fileName);

int character;

while ((character = fr.read()) != -1) {

if (character != ' ' && character != '\n' && character != '\r') {

charCount++;

}}

System.out.println("Total number of characters: " + charCount);

fr.close();

} catch (FileNotFoundException e) {

System.out.println("Error: The file " + fileName + " was not found.");

} catch (IOException e) {

System.out.println("Error: An I/O error occurred while reading the file.");}}}


OUTPUT:
5.ALGORITHM:

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 {

public static void main(String[] args) {

String inputFile = "input.txt";

String outputFile = "output.txt";

try {

FileReader fr = new FileReader(inputFile);

FileWriter fw = new FileWriter(outputFile);

int character;

while ((character = fr.read()) != -1) {

if (!isVowel((char) character)) {

fw.write(character);

fr.close();

fw.close();

System.out.println("Vowels have been successfully removed and saved to " +


outputFile);

} catch (IOException e) {
System.out.println("Error: An I/O error occurred while processing the file.");

public static boolean isVowel(char c) {

char lowerC = Character.toLowerCase(c);

return lowerC == 'a' || lowerC == 'e' || lowerC == 'i' || lowerC == 'o' || lowerC == 'u';}}

Criteria Marks

Program 1 and its Output Verification / 20

Program 2 and its Output Verification / 20

Program 3 and its Output Verification / 20

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.

You might also like