GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - DataOutputStream Class
Selected Reading
Java - DataOutputStream ClassIntroductionThe Java.io.DataOutputStream class lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in. Class declarationFollowing is the declaration for Java.io.DataOutputStream class − public class DataOutputStream
extends FilterOutputStream
implements DataOutput
FieldFollowing are the fields for Java.io.DataOutputStream class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
ExampleFollowing is an example to demonstrate DataInputStream and DataOutputStream. This example write a string to a file test.txt using DataOutputStream.writeUTF() method and then reads the same using DataInputStream.readUTF() method. import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataOutputStreamDemo {
public static void main(String[] args) throws IOException {
InputStream is = null;
DataInputStream dis = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
String[] buf = {"Hello", "World!!"};
try {
// create file output stream
fos = new FileOutputStream("test.txt");
// create data output stream
dos = new DataOutputStream(fos);
// for each string in the buffer
for (String j:buf) {
// write string encoded as modified UTF-8
dos.writeUTF(j);
}
// force data to the underlying stream
dos.flush();
// create file input stream
is = new FileInputStream("test.txt");
// create new data input stream
dis = new DataInputStream(is);
// read till end of the stream
while(dis.available()>0) {
// reads characters encoded with modified UTF-8
String k = dis.readUTF();
// print
System.out.print(k+" ");
}
} catch(Exception e) {
// if any I/O error occurs
e.printStackTrace();
} finally {
// releases all system resources from the streams
if(is!=null)
is.close();
if(dos!=null)
is.close();
if(dis!=null)
dis.close();
if(fos!=null)
fos.close();
}
}
}
OutputHello World!! Example - Writing a Subarray of Bytes to a FileThe following example shows the usage of Java DataOutputStream write(byte[] b, int off, int len) method. DataOutputStreamDemo.javapackage com.tutorialspoint;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
public static void main(String[] args) {
try {
// Create a DataOutputStream to write to a file
FileOutputStream fileOutput = new FileOutputStream("output.dat");
DataOutputStream dataOutput = new DataOutputStream(fileOutput);
// Byte array containing ASCII values of 'Hello, World!'
byte[] message = "Hello, World!".getBytes();
// Write only "World!" (start at index 7, length 6)
dataOutput.write(message, 7, 6);
// Close stream
dataOutput.close();
System.out.println("Data successfully written to output.dat");
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Data successfully written to output.dat Explanation
Example - Writing and Reading a Boolean to/from a FileThe following example shows the usage of Java DataOutputStream writeBoolean(boolean v) method. DataOutputStreamDemo.javapackage com.tutorialspoint;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
public static void main(String[] args) {
try {
// Create a DataOutputStream to write to a file
FileOutputStream fileOutput = new FileOutputStream("output.dat");
DataOutputStream dataOutput = new DataOutputStream(fileOutput);
// Write boolean values
dataOutput.writeBoolean(true);
dataOutput.writeBoolean(false);
dataOutput.writeBoolean(true);
// Close the output stream
dataOutput.close();
System.out.println("Boolean values successfully written to output.dat");
// Read the boolean values back
FileInputStream fileInput = new FileInputStream("output.dat");
DataInputStream dataInput = new DataInputStream(fileInput);
// Read and print the boolean values
System.out.println("First boolean: " + dataInput.readBoolean()); // true
System.out.println("Second boolean: " + dataInput.readBoolean()); // false
System.out.println("Third boolean: " + dataInput.readBoolean()); // true
// Close the input stream
dataInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Boolean values successfully written to output.dat First boolean: true Second boolean: false Third boolean: true ExplanationWriting Boolean Values
Reading Boolean Values
Advertisements
| ||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |