Get Website Learn Coding Tech Story

Java Programming Examples

/* 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

Learn more about FileOutputStream, FileInputStream, DataOutputStream and DataInputStream