Determine number of bytes written to DataOutputStream
- /*
- Determine number of bytes written to DataOutputStream
- This Java example shows how to determine total number of bytes written to
- the output stream using size method of DataOutputStream class.
- */
- import java.io.DataOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class NumberOfBytesWritten {
- public static void main(String[] args) {
- String strFilePath = "C://FileIO//NumberOfBytes.txt";
- try
- {
- //create FileOutputStream object
- FileOutputStream fos = new FileOutputStream(strFilePath);
- /*
- * To create DataOutputStream object from FileOutputStream use,
- * DataOutputStream(OutputStream os) constructor.
- *
- */
- DataOutputStream dos = new DataOutputStream(fos);
- String str = "Example to show number of bytes written to stream";
- dos.writeBytes(str);
- /*
- * To determine total number of bytes written to underlying stream, use
- * int size() method.
- *
- */
- int bytesWritten = dos.size();
- System.out.println("Total " + bytesWritten + " bytes are written to stream.");
- dos.close();
- }
- catch (IOException e)
- {
- System.out.println("IOException : " + e);
- }
- }
- }



