Determine number of bytes written to DataOutputStream - Java File Path IO

Java examples for File Path IO:DataOutputStream

Description

Determine number of bytes written to DataOutputStream

Demo Code


import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class Main {
 
  public static void main(String[] args) {
   /*from  w w  w.  ja v a  2s . c  o m*/
    String strFilePath = "C://Folder//NumberOfBytes.txt";
   
    try
    {
      //create FileOutputStream object
      FileOutputStream fos = new FileOutputStream(strFilePath);
     
       DataOutputStream dos = new DataOutputStream(fos);
     
       String str = "Example to show number of bytes written to stream";
       dos.writeBytes(str);
     
        int bytesWritten = dos.size();
        System.out.println("Total " + bytesWritten + " bytes are written to stream.");
 
        dos.close();
       
    }
    catch (IOException e)
    {
      System.out.println("IOException : " + e);
    }
 
  }
}

Result


Related Tutorials