Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: PrintStream not printing in file

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default PrintStream not printing in file

    I have this program to write into a file. I have a problem with my PrintStream

    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!


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default 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.
    	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...

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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..

  4. #4
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: PrintStream not printing in file

    What are you getting in the file?

    	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

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default 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?

  6. #6
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: PrintStream not printing in file

    Quote Originally Posted by Zula View Post
    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!

  7. #7
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default 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

Similar Threads

  1. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 03:41 AM
  2. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 04:43 AM
  3. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 09:15 AM
  4. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 07:57 AM
  5. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 02:50 PM