Results 1 to 9 of 9

Thread: Enter

  1. #1
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Unhappy Enter

    So when I write a text file, Notepad does not seem to recognize enters.
    I use "\n" to specify enters and Notepad displays it as a square and not a new line.
    The strange thing is that Editplus does display enters as they should.
    Please, is there any other way to specify enters in Java?

  2. #2
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    This is more than likely an OS thing dating from the days of teletype. The two characters for a return and line feed are 0xD, 0xA, or \r. \n.

    There can be compatibility problems if the full sequence is missing. I had a bug recently because I was using \r rather than \n. Replacing the \r with \n solved the problem in a dos command prompt. Reasearch on the problem indicated that \n alone should be sufficient, and it was.

    But try \n\r to see if that works. The return character incompatibility is one of the differences between unix and windows/dos. There are unix utilities called something like unix2dos and dos2unix to take care of this problem.

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    If you want to use the line separator for your platform, use
    Java Code:
    System.getProperty("line.separator");
    .

    There are also other useful property values, such as "file.separator" and "path.separator".
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    Quote Originally Posted by CodesAway View Post
    If you want to use the line separator for your platform, use
    Java Code:
    System.getProperty("line.separator");
    .
    This is the most suitable way to work on, then no need to worry about UNICODE stuff in most of the cases. Let system to do things more safer in languages like Java, not like C/C++.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    "line.separator" is fine unless you actually want to move the files from one OS to another.

    Notepad is a "special" text editor and the only one, that I know of, that doesn't read files properly. (I realise that the last sentence is a bit argumentative. But, really.)

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Notepad reads files "correctly", as it defines correct.

    Notepad is a "basic" text editor. Unlike more "advanced" text editors, it doesn't recognize the different line separators and realize that they have the same overall function - to separate text into different lines. Instead, it only will treat "\r\n" as a line separator, since that's the one for windows.

    If a file is moved from OS to OS, usually the user is somewhat tech-aware. These users wouldn't use notepad for any use, because they realize that notepad has really no use (maybe argumentative). As for the nontech-aware ones, I don't know any solution that works. My only hope is that notepad and any other text editor that doesn't automatically recognize the different line separators will soon do so.

    Since line separators are different for the different OSes, it's not possible for the programmer to handle all such concerns (unless the program will only run on one OS). A program can read a file in multiple formats, write it in multiple formats, but once written, the program has no control of the file, and it is up to the text editors to be able to read the file.


    As for Java methods, for my knowledge, they work the same, regardless of the line separator. For example, BufferedReader.readLine, "[Reads] a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed." Just remember to verify that the methods you use reads a line correctly (regardless of the line separator).

    When outputting, I usually use the line separator of the system I'm working on - this allows easy usage on that OS.
    Last edited by CodesAway; 10-18-2009 at 06:08 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    Use the BufferedWriter.newLine() method.

  8. #8
    gcampton Guest

    Default

    The moral of this story is don't use notepad. Notepad uses the microsoft version of line endings being 'carrige return', not 'Line feed', like most other systems, a number of IDE's and text editors on windows still uses the more widely used 'line feeds' which are portable, even MS wordpad uses 'line feed', however notepad does not. there are many notepad replacements, some of which are much better for programming as well, notepad++ as 1 example. I suggest if you love the simplicity of notepad, then find a replacement for it, or use wordpad instead.

  9. #9
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    I tested out this code:
    Java Code:
    import java.io.*;
    class Test {
    	public static void main(String[] args) throws IOException {
    		FileWriter file;
    		file = new FileWriter(new File("Backslash n.txt"));
    		file.write("Lets test out the Backslash n method\nHere goes\n\nAfraid of heights!");
    		file.close();
    		file = new FileWriter(new File("Backslash r.txt"));
    		file.write("Lets test out the Backslash r method\rHere goes\r\rAfraid of heights!");
    		file.close();
    		file = new FileWriter(new File("Backslash both.txt"));
    		file.write("Lets test out the Backslash r,n method\r\nHere goes\r\n\r\nAfraid of heights!");
    		file.close();
    	}
    }
    Only "Backslash" both had enters in Notepad, so CodesAway was right. Solved.

Similar Threads

  1. enter key in swt
    By ashin in forum SWT / JFace
    Replies: 0
    Last Post: 07-17-2009, 09:42 AM
  2. Detect the ENTER key being pressed
    By aaronfsimons in forum New To Java
    Replies: 12
    Last Post: 05-16-2009, 08:48 PM
  3. how do you bypass the enter(/n) key in a program?
    By mtwisd in forum New To Java
    Replies: 1
    Last Post: 04-24-2009, 08:50 AM
  4. what code for not hitting enter?
    By tornbacchus in forum New To Java
    Replies: 4
    Last Post: 04-11-2009, 04:59 AM
  5. enter key problem
    By masa in forum AWT / Swing
    Replies: 3
    Last Post: 03-19-2009, 07:30 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
  •