Thu Oct 07 2021

DataStream File

File Name: datastream-file.java

/* Data stream */
import java.io.*;

class datafile {
	public static void main(String args[ ]) throws IOException {

		/* Declare and create input file */
		FileOutputStream wf = new FileOutputStream("datafile.txt");
		DataOutputStream wd = new DataOutputStream(wf);

		/* Write double in the file */
		wd.writeDouble(22.05);

		/* Write integer in the file */
		wd.writeInt(2014);

		/* Write string in the file */
		wd.writeUTF("DataStream File");
		System.out.println("File Created!");
		wd.close();
		wf.close();

		/* Read the file */
		FileInputStream rf = new FileInputStream("datafile.txt");
		DataInputStream rd = new DataInputStream(rf);
		System.out.println("Reading File -");

		/* Read double from the file */
		System.out.println(rd.readDouble());

		/* Read integer from the file */
		System.out.println(rd.readInt());

		/* Read string from the file */
		System.out.println(rd.readUTF());
		rd.close();
		rf.close();
	}
}



/* Output */
File Created!

Reading File -
22.05
2014

DataStream File

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.