Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

1. Start the program.

2. Create a FileReader object "fr" and initialize it with the input file name "input.c".
This FileReader is responsible for reading characters from the input file.
3. Create a FileWriter object "fw" and initialize it with the output file name
"output.txt". This FileWriter is responsible for writing characters to the output file.
4. Create an empty string variable "str" to store the content of the file. This string
will accumulate the characters read from the input file.
5. Create an integer variable "i" to store the current character being read from the
file.
6. Enter a while loop that reads characters from the input file using the FileReader
object "fr" until the end of the file is reached. The condition (i = fr.read()) != -1
reads a character and assigns it to "i". If the character is not -1 (indicating the
end of the file), the loop continues.
7. Inside the loop, append each character (converted to char type) to the "str" string
using the concatenation operator +=. This builds the content of the file in the "str"
string.
8. Print the content of the "str" string using System.out.println(str). This displays the
content of the input file on the console.
9. Write the content of the "str" string to the output file using the FileWriter object
"fw" and its write() method. This writes the content of the input file to the output
file.
10. Close the FileReader and FileWriter objects by calling their close() methods. This
releases system resources and ensures that any buffered data is written to the
files.
11. Print a message using System.out.println() indicating that both reading and
writing of the file have been completed successfully.
12. If any IOException occurs during the file reading or writing process, catch the
exception using a try-catch block and print a corresponding error message using
System.out.println().
13. End the program.

You might also like