Advertisement
DataOutputStream(OutputStream is) |
---|
This constructor takes an OutputStream object in the parameters to write data to this output stream.
Example-:
|
Methods | Description |
---|---|
flush() | This method flushes any buffered data to be written out of this output stream. |
write() | This method writes one byte at a time to this output stream. |
write(byte[]) | This method writes a whole byte array at a time to this output stream. |
writeChar() | This method writes a character(primitive data type) at a time to this output stream. |
writeFloat() | This method writes a float (primitive data type) at a time to this output stream. |
writeShort() | This method writes a short(primitive data type) at a time to this output stream. |
writeInt() | This method writes an int(primitive data type) at a time to this output stream. |
writeLong() | This method writes a long(primitive data type) at a time to this output stream. |
writeBoolean() | This method writes a boolean(primitive data type) at a time to this output stream. |
size() | This method returns the total number of bytes written to DataOutputStream so far. |
close() | This method closes this output stream and also frees any resources connected with this output stream. |
Advertisement
//Program to write primitive data types to file using by DataOutputStream
import java.io.*;
class A
{
public static void main(String... ar)
{
try
{
FileOutputStream fos= new FileOutputStream("D:\\TextBook.txt");
DataOutputStream dos =new DataOutputStream(fos);
dos.writeInt(1985);
dos.writeDouble(245.45);
dos.writeChar('c');
dos.writeBoolean(true);
dos.writeLong(9999999999999l);
dos.writeFloat(12.15f);
System.out.println("Number of bytes written to this stream = "+ dos.size());
dos.close();
fos.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Number of bytes written to this stream 27
After the execution of this program, a file TextBook.txt is created in the D: drive, with contents -:
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement