PrintStream not printing in file
I have this program to write into a file. I have a problem with my PrintStream
Code :
public static void main(String[] args) throws Exception {
int i;
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("e:\\My Ebooks\\rahulverma.txt")));
out.writeBytes("Hello Rahul Verma1 \n");
out.writeDouble(93.6);
PrintStream printer = new PrintStream("e:\\My Ebooks\\rahulverma.txt"); //Using another outputstream object to write to the file
double val = 93.6;
printer.print(val);
out.close();
printer.close();
DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream("e:\\my ebooks\\rahulverma.txt")));
String str = input.readLine(); //Pardon the deprecated method!
double value = input.readDouble();
System.out.println(str+"........"+value);
input.close();
}
I am not getting the value 93.6 in the human readable format in my file. I just cant get the problem.
Please help!
Re: PrintStream not printing in file
Is it necessary to use PrintStream? I am not familiar with it, but this works to print a double to file.
Code :
public static void main(String[] args) throws Exception {
BufferedWriter bOut = new BufferedWriter(new FileWriter("C:\\txt.txt"));
double d = 93.2;
Double d2 = new Double(d);
bOut.write(d2.toString());
bOut.flush();
}
Not sure if that helps...
Re: PrintStream not printing in file
Thanks! :)
But actually the thing is, i know there are other options to use, but i just wanted to know what the problem was with PrintStream here...may be some hidden concept i am overooking..
Re: PrintStream not printing in file
What are you getting in the file?
Code :
public static void main(String[] args) throws Exception {
int i;
PrintStream printer = new PrintStream("c:\\txt.txt"); //Using another outputstream object to write to the file
double val = 93.6;
printer.println("Hello!\n\r");
printer.print(val);
printer.close();
DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream("c:\\txt.txt")));
String str = input.readLine();
System.out.println(str); //Pardon the deprecated method!
double value = input.readDouble();
System.out.println(str+"........"+value);
input.close();
}
writes fine but reads wrong for me...gets EOFException
Re: PrintStream not printing in file
Okay, I see my problem was that I am printing the String representation of a double, and trying to read the bytes of a double...Perhaps your problem as well?
Re: PrintStream not printing in file
Quote:
Originally Posted by
Zula
Okay, I see my problem was that I am printing the String representation of a double, and trying to read the bytes of a double...Perhaps your problem as well?
i think that should not be the reason for the EOFException. There seems no way printing of some variable would cause EOF.
EOF should happen only when we are reading.
That, anyways, is not helping with my problem, i guess.
Thanks, anyways! :)
Dozing off now!
Re: PrintStream not printing in file
What I mean to say is that I print a string, fine, but then trying to read "93.6", String, with readDouble was giving the EOF error
And if your looking for a human readable, or string version) of a double to be printed and getting gibberish, you are perhaps writing the data value of a double...if that makes sense