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 3 of 3

Thread: Format difference in System.out Vs. file output

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Format difference in System.out Vs. file output

    In my program I print the results and output them to a file. The format of the output is different. Can someone tell me why and how I can
    make the output to file the same format as the print output.

    here is the code:
    PrintWriter outFile = new PrintWriter("C:\\users\\remo\\downloads\\Ch3_Ex7Output.txt");
     
     
    			outPut = e1FirstName + " " + e1LastName + " " + String.format("%.2f", employee1Final) + "\n" +
    			         e2FirstName + " " + e2LastName + " " + String.format("%.2f", employee2Final) + "\n" +
    						e3FirstName + " " + e3LastName + " " + String.format("%.2f", employee3Final) + "\n";
     
    			System.out.printf(outPut);			
    			outFile.printf(outPut);
     
    			inFile.close();
    			outFile.close();

    Here is the print output:

    Andrew Miller 3289.49
    Sheila Green 4553.55
    Amit Sethi 4568.93

    here is the file output:
    Andrew Miller 3289.49Sheila Green 4553.55Amit Sethi 4568.93

    Thanks!


  2. #2
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Format difference in System.out Vs. file output

    I answered this question in Your other post.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Format difference in System.out Vs. file output

    What program are you using to view the file? Notepad and Wordpad will give different results.
    Look at the files properties to see how many bytes are in the file.
    Does that number match what you see when you look at the text of the file in a viewer?
    I suspect the file has 3 or 4 more bytes than you see in the editor or viewer.

    Use the String.length() method to print out the length of the String before writing it to the file.

Similar Threads

  1. Format Java output
    By wildman0501 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 11th, 2011, 06:09 PM
  2. Obtaining File System in Use
    By ebm1991 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 9th, 2011, 09:20 AM
  3. Downloading the file in Lotus Symphony spreadsheet format (ods)
    By chinnu in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 13th, 2011, 02:47 AM
  4. Saving an image file (PGM format)
    By Mickey2315 in forum Algorithms & Recursion
    Replies: 3
    Last Post: September 12th, 2009, 01:18 AM
  5. Replies: 2
    Last Post: June 26th, 2009, 11:08 AM