Java.io.DataOutputStream.size() Method

Advertisements


Description

The java.io.DataOutputStream.size() method returns the value of the written field.

Declaration

Following is the declaration for java.io.DataOutputStream.size() method:

public final int size()

Parameters

  • NA

Return Value

This method returns the last value of counter written

Exception

  • NA

Example

The following example shows the usage of java.io.DataOutputStream.size() method.

package com.tutorialspoint;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
         
      FileOutputStream fos = null;
      DataOutputStream dos = null;
      byte[] buf = {87,64,72,31,90};
      
      try{
         // create file output stream
         fos = new FileOutputStream("c:/test.txt");
         
         // create data output stream
         dos = new DataOutputStream(fos);

         int size = 0;

         // for each byte in the buffer
         for (byte b:buf)
         {
            dos.write(b);
            
            // current value of the counter written
            size = dos.size();

            // print
            System.out.print("Size: "+size + "; ");
         }
      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }finally{
         
         // releases all system resources from the streams
         if(fos!=null)
            fos.close();
         if(dos!=null)
            dos.close();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

Size: 1; Size: 2; Size: 3; Size: 4; Size: 5; 



Advertisements
Advertisements