FileOutputStream class belongs to byte stream and stores the data in the form of individual bytes. It can be used to create text files. A file represents storage of data on a second storage media like a hard disk or CD. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing objects) at a time. In such situations, the constructors in this class will fail if the file involved is already open.
FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.
- void close() : Closes this file output stream and releases any system resources associated with this stream.
- protected void finalize() : Cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.
- void write(byte b) : Writes b.length bytes from the specified byte array to this file output stream.
- void write(byte b, int off, int len) : Writes len bytes from the specified byte array starting at offset off to this file output stream.
- void write(int b) : Writes the specified byte to this file output stream.
Following steps are to be followed to create a text file that stores some characters (or text):
- Reading data: First of all, data should be read from the keyboard. For this purpose, associate the keyboard to some input stream class. The code for using DataInputSream class for reading data from the keyboard is as:
DataInputStream dis =new DataInputStream(System.in);
Here System.in represent the keyboard which is linked with DataInputStream object
- Send data to OutputStream: Now , associate a file where the data is to be stored to some output stream. For this , take the help of FileOutputStream which can send data to the file. Attaching the file.txt to FileOutputStream can be done as:
FileOutputStream fout=new FileOutputStream(“file.txt”);
- Reading data from DataInputStream: The next step is to read data from DataInputStream and write it into FileOutputStream . It means read data from dis object and write it into fout object, as shown here:
- Close the file: Finally, any file should be closed after performing input or output operations on it, else the data of the may be corrupted. Closing the file is done by closing the associated streams. For example, fout.close(): will close the FileOutputStream ,hence there is no way to write data into the file.
If the Program is executed again, the old data of file.txt will be lost and any recent data is only stored in the file. If we don’t want to lose the previous data of the file, and just append the new data to the end of already existing data, and this can be done by writing true along with file name.
FileOutputStream fout=new FileOutputStream(“file.txt”,true);
Improving Efficiency using BufferedOutputStream
Normally, whenever we write data to a file using FileOutputStream as:
Here, the FileOutputStream is invoked to write the characters into the file. Let us estimate the time it takes to read 100 characters from the keyboard and write all of them into a file.
- Let us assume data is read from the keyboard into memory using DataInputStream and it takes 1 sec to read 1 character into memory and this character is written into the file by FileOutputStream by spending another 1 sec.
- So for reading and writing a file will take 200 sec. This is wasting a lot of time. On the other hand, if Buffered classed are used, they provide a buffer which is first filled with characters from the buffer which can be at once written into the file. Buffered classes should be used in connection to other stream classes.
- First, the DataInputStream reads data from the keyboard by spending 1 sec for each character. This character is written into the buffer. Thus, to read 100 characters into a buffer, it will take 100 second time. Now FileOutputStream will write the entire buffer in a single step. So, reading and writing 100 characters took 101 sec only. In the same way, reading classes are used for improving the speed of reading operation. Attaching FileOutputStream to BufferedOutputStream as:
BufferedOutputStream bout=new BufferedOutputStream(fout,1024);
Here ,the buffer size is declared as 1024 bytes. If the buffer size is not specified , then a default size of 512 bytes is used
Important methods of BufferedOutputStream Class:
- void flush() : Flushes this buffered output stream.
- void write(byte b, int off, int len) : Writes len bytes from the specified byte array starting at offset off to this buffered output stream.
- void write(int b) : Writes the specified byte to this buffered output stream.
C:\> javac Create_File.java C:\> java Create_File Enter text (@ at the end): This is a program to create a file @ C:/> type file.txt This is a program to create a file
- CharacterStream vs ByteStream
- File Class in Java
- File handling in Java using FileWriter and FileReader
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Creating a Cell at specific position in Excel file using Java
- Creating Sheets in Excel File in Java using Apache POI
- Creating Frames using Swings in Java
- Creating an Server-Client Application using the DatagramPacket and DatagramSocket classes
- Containerizing Java applications | Creating a Spring Boot App using Dockerfile
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Image Processing in Java | Set 8 (Creating mirror image)
- Pinging an IP address in Java | Set 2 (By creating sub-process)
- Android | Creating a RatingBar
- Java Swing | Creating a Toast Message
- Java Swing | Creating Custom Message Dialogs
- Android | Creating a Calendar View app
- Android | Creating a Splash Screen
- Creating Sequential Stream from an Iterator in Java
- Android | Creating multiple Screen app
- Android | Creating a SeekBar
- Creating an Asynchronous Multithreaded chat Application in Java
- Creating interactive maps and Geo Visualizations in Java
- Copying file using FileStreams in Java
- File handling in Java using FileWriter and FileReader