Java Program to Determine Number of Bytes Written to File using DataOutputStream
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.