Java Program to Determine Number of Bytes Written to File using DataOutputStream Last Updated : 03 Jun, 2024 Comments Improve Suggest changes Like Article Like Report The Java OutputStream class, java.io.OutputStream, is the base class of all output streams in the Java IO API. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. To get the number of bytes written we firstly create an object of FileOutputStream by passing the file's path that is shown in code below as FileOS.Then we create an object of DataOutputStream by passing the FileOS i.e. object of FileOutputStream that is shown in code below as DataOS.Now, we will inject text into this text file by using writeBytes() method of DataOutputStream. To get the size of bytes written we use size method of DataOutputStream Class on the object DataOS.Program 1: Java // Java program to determine number of bytes // written to DataOutputStream import java.io.*; public class NumberOfBytesInOutputStream { public static void main(String[] args) { try { // creates object of FileOutputStream by passing // file Bytes.txt FileOutputStream FileOS = new FileOutputStream("C:/Bytes.txt"); // creates object of DataOutputStream by // passing object of FileOutputStream i.e. // FileOS DataOutputStream DataOS = new DataOutputStream(FileOS); // writes the string passed to object of // DataOutputStream DataOS.writeBytes( "GeeksforGeeks is the best place to learn Coding online."); // Stores the number of bytes to total_bytes // variable using size() method of // DataOutputStream class int total_bytes = DataOS.size(); // Showing the number of bytes as output in // console DataOS.close(); System.out.println("Total " + total_bytes + " bytes were written to stream."); } catch (Exception e) { System.out.println("Exception: " + e.toString()); } } } Output: Program 2: Java // Java program to determine number of bytes // written to DataOutputStream import java.io.*; public class NumberOfBytesInOutputStream2 { public static void main(String[] args) { try { // creates object of FileOutputStream by passing // file Bytes.txt FileOutputStream FileOS = new FileOutputStream("C:/NumberOfBytes.txt"); // creates object of DataOutputStream by passing // object of FileOutputStream i.e. FileOS DataOutputStream DataOS = new DataOutputStream(FileOS); // create string S with the desired text String S = "GeeksforGeeks article is best for getting cs concepts."; // writes the string passed to object of // DataOutputStream DataOS.writeBytes(S); // Stores the number of bytes to total_bytes // variable using size() method of // DataOutputStream class int total_bytes = DataOS.size(); // Showing the number of bytes as output in // console System.out.println("Total " + total_bytes + " bytes were written to stream."); DataOS.close(); } catch (Exception e) { System.out.println("Exception: " + e.toString()); } } } Output: Comment More infoAdvertise with us Next Article Java Program to Determine Number of Bytes Written to File using DataOutputStream M mishrapriyank17 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-File Class +1 More Practice Tags : Java Similar Reads Java Program to Write Bytes using ByteStream Java Byte streams are used to perform input and output of 8-bit bytes. To write Bytes using BytesStream to a file Java provides a specialized stream for writing files in the file system known as FileOutputStream. This stream provides the basic OutputStream functionality applied for writing the conte 4 min read Java Program to Convert File to a Byte Array Here, we will go through the different ways to convert file to byte array in Java. Note: Keep a check that prior doing anything first. Create a file on the system repository to deal with our program\writing a program as we will be accessing the same directory through our programs. Methods: Using rea 3 min read Java Program to Get the Size of Given File in Bytes, KiloBytes and MegaBytes The in-built File.length() method in java can be used to get file size in java. The length() function is a part of the File class in Java. This function returns the length of the file whose path was specified. If the file does not exist or some error happens then it returns 0. Parameters: This metho 2 min read Java Program to Convert InputStream to String Read and Write operations are basic functionalities that users perform in any application. Every programming language provides I/O streams to read and write data. The FileInputStream class and FileOutputStream class of Java performs I/O operations on files. The FileInputStream class is used to read 4 min read Java Program to Get the Basic File Attributes Basic File Attributes are the attributes that are associated with a file in a file system, these attributes are common to many file systems. In order to get the basic file attributes, we have to use the BasicFileAttributes interface. This interface is introduced in 2007 and is a part of the nio pack 3 min read Java Program to Get the Size of a Directory The size of files in Java can be obtained using the File class. The in-built function of 'fileName.length()' is used to find size of file in Bytes. The directory may contain 'N' number of files, for calculating the size of the directory summation of the size of all the files is required. length() Me 2 min read Different Ways to Copy Content From One File to Another File in Java In Java, we can copy the contents of one file to another file. This can be done by the FileInputStream and FileOutputStream classes. FileInputStream Class It is a byte input stream class which helps in reading the bytes from a file. It provides different methods to read the data from a file. FileInp 3 min read Java Program to Read a Large Text File Line by Line As we are well verse with this topic let us put more stress in order to figure out minute differences between them. Here we are supposed to read from a file on the local directory where a text file is present say it be 'gfg.txt'. Let the content inside the file be as shown below: Geeks for Geeks. A 3 min read How to Read and Write Binary Files in Java? The Binary files contain data in a format that is not human-readable. To make them suitable for storing complex data structures efficiently, in Java, we can read from and write to binary files using the Input and Output Streams.In this article, we will learn and see the code implementation to read a 2 min read Checking Last Modification of a File On the Server in Java In Java, we have different classes like File, URL which provides the functionality to read the Attributes of file like creation time, last access time, and last modified time. Method 1(Using BasicFileAttributes) This example uses java.nio.* to display the metadata of the file and other file attribut 3 min read Like