FileOutputStream, DataOutputStream in java
In the Java file handling process, both FileOutputStream and DataOutputStream are file processing function method classes used to create or write new data or information to existing Java user-defined files. The FileOutputStream and DataOutputStream functions in Java file processing use several individual file handling function concepts to manage or handle file data, where their use may vary depending on the requirements of the file handling task.

So, let’s explore the FileOutputStream and DataOutputStream file handling concepts in Java programming.
FileOutputStream Class in Java.
FileOutputStream is a built-in class feature in the Java file handling process that allows the Java user to create raw byte data in an existing file. The FileOutputStream class is a built-in part of the java.io library package and is designed to create or edit binary data in a Java user-defined file.
Important Features of the FileOutputStream Class in Java File Handling.
- Byte-oriented – It creates file data in raw byte binary format (0,1) in machine (0,1) order during the file handling process.
- Low-level writing – It creates file data byte-by-byte during the file handling process.
- File creation – The FileOutputStream function class creates a new file if the indicated file does not exist during the file handling process. If the file already exists, it overwrites the old content with the new content on disk.
- No buffering – It does not buffer the file data during the file handling process. Consequently, each time a file is created, the task of creating data in the file is performed directly in the file system. This may not be appropriate for large-volume file data.
Basic concepts of FileOutputStream in Java file handling.
- write(int p) – It creates the indicated file bytes in the output stream.
- write(byte[] q) – This creates the byte array q data indicated here in the output stream.
- close() – This closes the output stream in the current file handling program function, and frees the system resources associated with the current file.
Example of the FileOutputStream function class in Java.
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamIllustration {
public static void main(String[] args) {
FileOutputStream fleotstrm = null;
try {
fleotstrm = new FileOutputStream(“testfile.bin”);
String data = “Welcome to, Vcanhelpsu”;
fleotstrm.write(data.getBytes()); // Here it writes the string data as bytes to the indicated file
} catch (IOException e) {
System.out.println(“Here it displays an error when writing with the file – ” + e.getMessage());
} finally {
try {
if (fleotstrm != null) {
fleotstrm.close(); // Here it closes the stream after writing text information to the file
}
} catch (IOException e) {
System.out.println(“Here it displays an error when closing the file – ” + e.getMessage());
}
}
}
}
Key points of the FileOutputStream class.
- In the current program, the FileOutputStream class creates raw byte data in a file.
- The FileOutputStream class function is a perfect use case for binary data, such as images, audio, or video files. When a Java user needs to create individual byte data in a file.
- The FileOutputStream class is not suitable for creating data information from structured file data, such as integers or strings, because it does not manage or handle any data type conversions in the file handling process. The Java user must manually handle the data information encoding process task.
DataOutputStream Function Class in Java.
DataOutputStream, a subclass of the FilterOutputStream class, is a built-in feature in the Java file handling process. It allows the Java user to create data information of primitive data types, such as int, float, double, long, boolean, etc., in an existing or new file in a machine-independent order. The DataOutputStream class is mostly used to create binary data in a file. This allows the DataInputStream class to read data from a file using the functions.
Important features of the DataOutputStream class in Java.
- Primitive data type – This allows Java users to create primitive data type information, such as int, long, char, boolean, float, double, String, etc., in the output stream to a file.
- Machine-independent – This converts primitive data type element data information into a byte format in the current file handling process, which is portable or compatible across multiple individual machines. This ensures compatibility when rereading the data in the file.
- Buffered output – This helps the file handling process create file data in a device platform-independent format, which is especially important and useful when managing or handling binary data in a file.
Basic concepts of DataOutputStream in Java file handling.
- writeInt(int x ) – This function creates an int value in the output stream in file handling.
- writeDouble(double x) – This function creates a double value representing a double value in the output stream of the file handling process.
- writeBoolean(boolean x) – This function creates a boolean value in the output stream of the file handling process.
- writeUTF(String str1 ) – This function creates a UTF-8 encoded string data information in the output stream of the file handling process.
- close() – This function closes the open output stream function in the file handling process and frees resources from the file system in the current file handling process.
Example of DataOutputStream in Java file handling.
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamIllustration {
public static void main(String[] args) {
DataOutputStream dtaotptstrm = null;
try {
dtaotptstrm = new DataOutputStream(new FileOutputStream(“testfile.dat”));
// here we Writing the different types of file data into the testfile.dat file
dtaotptstrm.writeInt(13); // here we Write an integer data to the file
dtaotptstrm.writeDouble(7.2233); //here we Write a double data to the file
dtaotptstrm.writeBoolean(true); // here we Write a boolean data to the file
dtaotptstrm.writeUTF(“Welcome to, Vcanhelpsu”); // here it Write a UTF-8 string data to the file
} catch (IOException e) {
System.out.println(“Here it displays the Error when writing to file – ” + e.getMessage());
} finally {
try {
if (dtaotptstrm != null) {
dtaotptstrm.close(); // Here it closes the stream after the file writing process
}
} catch (IOException e) {
System.out.println(“Here it displays an error when closing the file – ” + e.getMessage());
}
}
}
}
}
Special features of the Java DataOutputStream class.
- The DataOutputStream class is used in Java file handling processes to create primitive data types and string data information in a machine-independent binary format. In Java, it is especially used when a Java user needs to read and write structured binary file data to a file, which requires special file data creation, such as integer, float, or string data.
- The DataOutputStream class converts data in a file to binary format, maintaining file compatibility and portability across multiple individual platforms.
