














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
How to use java input and output streams to read and write primitive data types such as integers, doubles, booleans, and characters in binary format. It covers the use of datainputstream and dataoutputstream classes, their creation, and methods supported. It also includes an example program to write and read primitive data from a file.
What you will learn
Typology: Thesis
1 / 22
This page cannot be seen from the preview
Don't miss anything!
FileInputStream fis = new FileInputStream(“InFile”);
DataInputStream dis = new DataInputStream( fis );
readBoolean(), readByte(), readChar(), readShort(), readInt(), readLong(), readFloat(), readDouble()
FileOutputStream fos = new FileOutputStream(“OutFile”);
DataOutputStream dos = new DataOutputStream( fos );
writeBoolean(), writeByte(), writeChar(), writeShort(), writeInt(), writeLong(), writeFloat(), writeDouble()
How many bytes are written to file when for statements:
import java.io.; public class ReadWriteFilter { public static void main(String args[]) throws IOException { // write primitive data in binary format to the "mydata" file FileOutputStream fos = new FileOutputStream("mydata"); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(120); dos.writeDouble(375.50); dos.writeInt('A'+1); dos.writeBoolean(true); dos.writeChar('X'); dos.close(); fos.close(); // read primitive data in binary format from the "mydata" file FileInputStream fis = new FileInputStream("mydata"); DataInputStream dis = new DataInputStream(fis); System.out.println(dis.readInt()); System.out.println(dis.readDouble()); System.out.println(dis.readInt()); System.out.println(dis.readBoolean()); System.out.println(dis.readChar()); dis.close(); fis.close(); } }*
120 375. 66 true X
C:\MHB\examples>type mydata
Size of int+double+int+boolean+char
Buffered streams sit between the program and data source/destination and functions like a filter or support efficient I/O. Buffered can be created using BufferedInputStream and BufferedOutputStream classes.
import java.io.; public class CombineStreams { public static void main(String args[]) throws IOException { // declare file streams FileInputStream file1 = new FileInputStream("file1.dat"); FileInputStream file2 = new FileInputStream("file2.dat"); // declare file3 to store combined streams SequenceInputStream file3 = null; // concatenate file1 and file2 streams into file file3 = new SequenceInputStream(file1, file2); BufferedInputStream inBuffer = new BufferedInputStream(file3); BufferedOutputStream outBuffer = new BufferedOutputStream(System.out); // read and write combined streams until the end of buffers int ch; while((ch = inBuffer.read()) != -1 ) outBuffer.write(ch); outBuffer.flush(); // check out the output by removing this line System.out.println("\nHello, This output is generated by CombineFiles.java program"); inBuffer.close(); outBuffer.close(); file1.close(); file2.close(); file3.close(); } }*
Hello, I am C++, born in AT&T. Hello, I am Java, born in Sun Microsystems! Hello, This output is generated by CombineFiles.java program
Hello, This output is generated by CombineFiles.java program Hello, I am C++, born in AT&T. Hello, I am Java, born in Sun Microsystems!
myFileHandleName = new RandomAccessFile (“filename”, “mode”);
Create buffered reader for standard input by wrapping System.in object: BufferedReader dis = new BufferedReader(new InputStreamReader(System.in)); Read a line of text from the console String str = dis.readLine(); Create Tokenens StringTokenizer st; st = new StringTokenizer(str); Convert String Token into basic integer: int stdID = Integer.parseInt(st.nextToken());