In this tutorial you will learn how to write to file primitive type double.
Write to file primitive data type double there is a class DataOutputStream that provides a writeDouble() method to write a double value in binary files. writeDouble() method first converts the argument to a long and then writes the converted value in the output stream as a 8 byte quantity.
In the example given below I have created a method named writeToFileDouble() into which created an object of FileOutputStream and passes the file name to its instance to write the character-stream further passed this object to the BufferedOutputStream instance to write the characters efficiently.
Example :
WriteToFileDouble.java
import java.io.*;
class WriteToFileDouble
{
public static void main(String args[])
{
WriteToFileDouble wtfd = new WriteToFileDouble();
wtfd.writeToFileDouble();
}
public void writeToFileDouble()
{
double[] dob = { 3.99, 124.30, 30.124};
FileOutputStream fos = null;
BufferedOutputStream bos = null;
DataOutputStream dos = null;
try
{
fos = new FileOutputStream("writeToFileDouble.txt");
bos = new BufferedOutputStream(fos);
dos = new DataOutputStream(bos);
for(int i=0; i<dob.length; i++)
{
dos.writeDouble(dob[i]);
}
dos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
How to Execute this example :
After doing the basic process to execute a java program write simply on command prompt as :
javac WriteToFileDouble.java to compile the program
And after successfully compilation to run simply type as :
java WriteToFileDouble
When you will execute this example a new file will be created at the specified place given by you with containing the text that you are trying to write into the file by java program.
|
Recommend the tutorial |
Ask Questions? Discuss: Java Write To File Double
Post your Comment