Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By camickr

Thread: Printing \n to a file

  1. #1
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Printing \n to a file

    my code is below
    Java Code:
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.Console;
    import java.io.PrintWriter;
    
    public class fileComp {
    	public static void main(String args[]) throws IOException {
    		System.out
    				.println("\n-------\nThis program compares 2 files line by line and works well for things like "
    						+ "CSV files. It will print any differences between the 2 files.\n---------");
    
    		Console c = System.console(); // Check to make sure user input is accepted
    		if (c == null) {
    			System.err.println("No console");
    			System.exit(1);
    		}
    
    		String file1 = null, file2 = null;
    		BufferedReader original = null, modified = null;
    		do { // User input for 1st file and loops until file valid
    			try {
    				System.out.println("Enter the 1st file name: ");
    				file1 = c.readLine();
    				original = new BufferedReader(new FileReader(file1));
    			} catch (FileNotFoundException e) {
    				System.out.println("File not found. Try again");
    			}
    		} while (original == null);
    
    		do { // User input for 2nd file and loops until file valid
    			try {
    				System.out.println("Enter the 2st file name: ");
    				file2 = c.readLine();
    				modified = new BufferedReader(new FileReader(file2));
    				if (file1.contentEquals(file2)) { // Makes sure the 2 entries are not the same file
    					System.out.println("You entered the same file. Try again");
    					modified = null;
    				}
    			} catch (FileNotFoundException e) {
    				System.out.println("File not found. Try again");
    			}
    		} while (modified == null);
    
    		PrintWriter results = new PrintWriter(new FileWriter("report.txt"));
    		String line1 = null, line2 = null;
    		int lineNum = 1, changes = 0;
    		while ((line1 = original.readLine()) != null
    				&& (line2 = modified.readLine()) != null) { // Keep looping until either file is done
    			if (line1.contentEquals(line2) == false) { // Change?
    				changes++;
    				String row = "\nLine " + lineNum + " has changed.\n"
    						+ "Old value: " + line1 + "\n" + "New value: " + line2;
    				System.out.println(row);
    				results.println(row);
    			}
    			lineNum++;
    		}
    		System.out.println("\nRecords changed: " + changes);
    
    		original.close();
    		modified.close();
    		results.close();
    	}
    }
    At around line 54 you see a String row with a bunch of text that I later on in line 57 print to a file. However, when I print to the file, all newline (\n) become some weird symbol. Why does \n work in console but not in the text file? Is the only way to make it work in the text file to use println() over and over? Here's the file contents:
    Printing \n to a file-1.jpg
    I'd also like to mention that WordPad displays the line breaks properly, but NotePad does not.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Printing \n to a file

    Because notepad doesn't handle newlines like that. Try \r\n or something like that, I don't remember the exact combination it wants.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Printing \n to a file

    oh OK. Thanks!
    Quote Originally Posted by KevinWorkman View Post
    Because notepad doesn't handle newlines like that. Try \r\n or something like that, I don't remember the exact combination it wants.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Printing \n to a file

    Actually your code should be more like:
    Java Code:
    results.print("something");
    results.println();
    results.print("something else");
    results.println();
    Then the appropriate newline string will be used depending on the platform you are running on.

    If you still want to use to create one long string then you should use the following to get the newline string:

    Java Code:
    String newline = System.getProperty("line.separator");
    Eranga and KevinWorkman like this.

  5. #5
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Printing \n to a file

    Quote Originally Posted by camickr View Post
    Actually your code should be more like:
    Java Code:
    results.print("something");
    results.println();
    results.print("something else");
    results.println();
    Then the appropriate newline string will be used depending on the platform you are running on.

    If you still want to use to create one long string then you should use the following to get the newline string:

    Java Code:
    String newline = System.getProperty("line.separator");
    OK thanks for the advice!

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Printing \n to a file

    Quote Originally Posted by camickr View Post
    Actually your code should be more like:
    Java Code:
    results.print("something");
    results.println();
    results.print("something else");
    results.println();
    Then the appropriate newline string will be used depending on the platform you are running on.

    If you still want to use to create one long string then you should use the following to get the newline string:

    Java Code:
    String newline = System.getProperty("line.separator");
    This is much better, readable and clear code. :)

Similar Threads

  1. Printing file through browser
    By BobswYourUcle in forum New To Java
    Replies: 5
    Last Post: 07-18-2011, 04:53 PM
  2. Printing JPEG file using java
    By venkateshcoolmoon in forum Advanced Java
    Replies: 2
    Last Post: 02-18-2010, 06:50 AM
  3. Sorting and Printing file
    By spry.chipper in forum New To Java
    Replies: 3
    Last Post: 12-13-2009, 09:15 PM
  4. Printing text file
    By amitcs1001 in forum NetBeans
    Replies: 2
    Last Post: 11-30-2009, 03:17 AM
  5. Job name when printing file
    By pjmorce in forum Advanced Java
    Replies: 3
    Last Post: 12-23-2008, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •