import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
try
{
FileOutputStream fout = new FileOutputStream("D:\\testout.txt");
fout.write(65);
fout.close();
System.out.println("success.. by TechSarvam.");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
This program shows how to write data into a file using Java’s FileOutputStream class. It writes the ASCII value of the number 65 to a file named testout.txt located on the D: drive.
Step-by-Step Breakdown:
1. Importing FileOutputStream Class:
import java.io.FileOutputStream;
- The FileOutputStream class is in the java.io package, and it is used to write bytes to a file.
- This class has several constructors and methods for writing data to files, including write() to write single bytes or arrays of bytes.
2. Main Method:
public class FileOutputStreamExample {
public static void main(String args[]) {
The method main is where the program’s execution begins.
3. Creating a FileOutputStream Object:
FileOutputStream fout = new FileOutputStream(“D:testout.txt”);
The class FileOutputStream constructor also takes as input the path for the file, D: testout.txt which creates an associated output stream corresponding to the file testout.txt on the drive D:-
If testout.txt has not already existed, it is created. As for testout.txt already exists, then an existing contents may be overwritten.
4 Writing Data to a File:
fout.write(65);
- The write(int b) method writes the specified byte to the file.
- The value 65 is an ASCII value for the character ‘A’.
- The program writes the byte whose ASCII value is 65 to the testout.txt file.
- This indicates that the character ‘A’ is in the bytes written into the file.
5. Closing the File Output Stream:
fout.close();
- After writing the data, the output stream must be closed by using the close() method.
- This will ensure that all the data that has been written to memory gets flushed out and is written to the file. Also, the resources associated with the output stream get freed up.
6. Printing a Success Message:
System.out.println(“success. by TechSarvam.”);
After completing the file writing process, a success message is printed to the console.
7. Exception Handling:
} catch(Exception e) {
System.out.println(e);
}
In case of some operation on a file, which does not permit such an action or is actually in an incorrect path, exception catches it, then prints that in the console.
Expected Result
1. It writes to testout.txt byte corresponding to the ASCII value 65 that is ‘A’.
2. On successful execution of file-writing task, then this program on console prints such success message as well:
success. by TechSarvam.
3. In the file D:testout.txt, you will find the character ‘A’ (ASCII value 65) written.
Summary:
- The program uses FileOutputStream to write the byte 65 which represents the character ‘A’ to a file. Then it prints out a success message, and the file testout.txt contains the character ‘A’ as its content.
- The program catches exceptions and cleans up resources using close() when closing the file to free up any file resources.
Important Comments
Note that file path D:testout.txt exists and the application can write to this directory. If the path does not exist, or the application cannot write to it, an exception is thrown, and a message is printed.