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 - DataInputStream Class
Selected Reading
Java - DataInputStream ClassIntroductionThe Java.io.DataInputStream class lets an application read primitive Java data types from an underlying input stream in a machine-independent way.Following are the important points about DataInputStream −
Class declarationFollowing is the declaration for Java.io.DataInputStream class − public class DataInputStream
extends FilterInputStream
implements DataInput
FieldFollowing are the fields for Java.io.DataInputStream 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("F:\\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("F:\\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!! java_files_io.htm
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. |