Java Program to Determine Number of Bytes Written to File using DataOutputStream Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 9 Likes 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: Create Quiz Comment M mishrapriyank17 Follow 9 Improve M mishrapriyank17 Follow 9 Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-File Class +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like