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 How to Read Write Object's Data in CSV Format Using Notepad in Java? The CSV stands for Comma-Separated Values. CSV files can be used with almost any spreadsheet program, such as Microsoft Excel or Google Spreadsheets. They differ from other spreadsheet file types because you can only have a single sheet in a file, they can not save cell, column, or row. Also, you ca 8 min read Java program to merge contents of all the files in a directory Prerequisite : PrintWriter, BufferedReader. We are given a directory/folder in which n number of files are stored(We dont know the number of files) and we want to merge the contents of all the files into a single file lets say output.txt For the below example lets say the folder is stored at the pat 2 min read How to Read and Write Files Using the New I/O (NIO.2) API in Java? In this article, we will learn how to read and write files using the new I/O (NIO) API in Java. For this first, we need to import the file from the NIO package in Java. This NIO.2 is introduced from the Java 7 version. This provides a more efficient way of handling input and output operations compar 3 min read Java Program to Create String from Contents of a File A File is a computer resource which is deployed to store different types of data such as text, image, video, to name a few. It is basically a collection of data bound to a single entity. While using your computer, it becomes essential to be able to deal with files and in this article we will be lear 6 min read Java Program to Get the Creation Time of a File Use java.nio package for extracting the creation date and time of any file through java. To extract the date and time of the file use BasicFileAttributes class. java.nio package helps us to get the creation time, last access time, and last modified time, it works for both file and directory. Approac 2 min read Java Program to Read Content From One File and Write it into Another File File handling plays a major role in doing so as the first essential step is writing content to a file. For this is one must know how to write content in a file using the  FileWriter class. The secondary step is reading content from a file and print the same. For this, one must have good hands on Fil 5 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 Create a New File There are two standard methods to create a new file, either directly with the help of the File class or indirectly with the help of the FileOutputStream class by creating an object of the file in both approaches.Methods to Create Files in JavaThere are two methods mentioned belowUsing the File Class 4 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 Like