Replacing newline character in UNIX - java

I'm making a code to replace a newline character from a string. On Windows, when I use
String.replaceAll(System.getProperty("line.separator"), "\\n");
this works fine, but it fails in UNIX.
What should i use in UNIX ?

\n is correct for Unix. Windows uses \r\n and Mac uses \r IIRC.
The problem may lie in the fact that Java, being a multiplatform language, automatically replaces \n with the system's separator. I don't know Java but I assume this is the case.
Edit: If the code you posted is what you're using, I think I see the problem. String is a class. It is also immutable in Java. It should instead be this:
String myStr = "abc\ndef";
myStr = myStr.replaceAll(/* params */);

The text being received probably contained windows line separators, so replacing just the \n character had no effect.
If you don't know the origin of the text (or the text contains a mixture of line separators), the following approach can help. First convert windows and mac line separators into unix separators, then convert the unix separators to the system separator.
final String DOS = "\r\n", NIX = "\n", MAC = "\r";
String converted = original.replace(DOS, NIX)
.replace(MAC, NIX)
.replace(NIX, System.lineSeparator());

Related

Java: PrintWriter and newlines in a string

My question is pretty straight forward, if I have a single long string with alot of "\n" newlines within it, i.e:
strings = "Hey\nThere\nFriend\n"
And use a PrintWriter in Java to do the following:
PrintWriter save = new PrintWriter("test.txt");
save.println(strings);
save.close();
Will the file I end up with be formatted with the \n? i.e the file will have:
Hey
There
Friend
Or will it have:
Hey\nThere\nFriend
If it's the latter, can someone guide me on how I might change my code (and understanding of how PrintWriter works) to create the former output?
In fact, \n will work but only for Unix based OS. Windows based OS use \r\n as separator.
You should avoid using specific OS line separator if you want to write a portable code.
Favor System.lineSeparator() to not be OS dependent.
Note also that PrintWriter provides println() to achieve a break line that is not OS dependent (even if it is not necessary useful for you use case)
You will get a text file containing a single text line Hey\nThere\nFriend\n followed by your operating system new-line sequence (inserted by println()).
The meaning of \n depends on the operating system and possibly the text editor. On Linux \n usually will be interpreted as new-line sequence but on Windows the new-line sequence is \r\n so most text editors (e.g. native Notepad) will display a single HeyThereFriend line.
On windows platform \n means char(13) +Char(10) you can use
String nl = Character.toString ((char) 13)+Character.toString ((char) 10);
String strings = "Hey"+nl+"There"+nl+"Friend"+nl;
System.out.print(strings);

What splitter should I use for every other line?

I have a text file that contains data every other line. I want to get the content of every non-empty line. Given the whole text of the file, I first tried using myText.split("\n\n"). To my surprise, it does not work. I'm working on Windows.
Windows uses CRLF as line separators. And you are splitting on LF. That wouldn't work.
A safe way is to use:
System.getProperty("line.separator");
to get the appropriate separator on your OS.
String newLine = System.getProperty("line.separator");
myText.split("(?:" + newLine + ")+");
It might be possible that you are reading a file created on a different OS. Then the above method won't work. A better way would be use a character class with CR and LF, as specified in comments by #Marko:
myText.split("[\r\n]+");

How to create a String with carriage returns?

For a JUnit test I need a String which consists of multiple lines. But all I get is a single lined String. I tried the following:
String str = ";;;;;;\n" +
"Name, number, address;;;;;;\n" +
"01.01.12-16.02.12;;;;;;\n" +
";;;;;;\n" +
";;;;;;";
I also tried \n\r instead of \n. System.getProperty("line.separator") doesn't work too. it produces a \n in String and no carriage return. So how can I solve that?
It depends on what you mean by "multiple lines". Different operating systems use different line separators.
In Java, \r is always carriage return, and \n is line feed. On Unix, just \n is enough for a newline, whereas many programs on Windows require \r\n. You can get at the platform default newline use System.getProperty("line.separator") or use String.format("%n") as mentioned in other answers.
But really, you need to know whether you're trying to produce OS-specific newlines - for example, if this is text which is going to be transmitted as part of a specific protocol, then you should see what that protocol deems to be a newline. For example, RFC 2822 defines a line separator of \r\n and this should be used even if you're running on Unix. So it's all about context.
The fastest way I know to generate a new-line character in Java is: String.format("%n")
Of course you can put whatever you want around the %n like:
String.format("line1%nline2")
Or even if you have a lot of lines:
String.format("%s%n%s%n%s%n%s", "line1", "line2", "line3", "line4")
Try \r\n where \r is carriage return. Also ensure that your output do not have new line, because debugger can show you special characters in form of \n, \r, \t etc.
Do this:
Step 1: Your String
String str = ";;;;;;\n" +
"Name, number, address;;;;;;\n" +
"01.01.12-16.02.12;;;;;;\n" +
";;;;;;\n" +
";;;;;;";
Step 2: Just replace all "\n" with "%n" the result looks like this
String str = ";;;;;;%n" +
"Name, number, address;;;;;;%n" +
"01.01.12-16.02.12;;;;;;%n" +
";;;;;;%n" +
";;;;;;";
Notice I've just put "%n" in place of "\n"
Step 3: Now simply call format()
str=String.format(str);
That's all you have to do.
Try append characters .append('\r').append('\n'); instead of String .append("\\r\\n");
Thanks for your answers. I missed that my data is stored in a List<String> which is passed to the tested method. The mistake was that I put the string into the first element of the ArrayList. That's why I thought the String consists of just one single line, because the debugger showed me only one entry.

String.split to split data lines doesn't work correctly

I use VB.NET to create data for my game (for Android, Java code), this is how it look like:
5;0000000100011100010000000;2;2
5;1000001100010000000000000;0,1;0,1
where each line is a level. In VB.NET, I create new line by vbNewLine constant (I think its ASCII code is 13) then use IO.File.WriteAllText to write it to the file.
In my game in Java, I use \n to split the levels:
String[] levelData = rawData.split("\n");
However, when processing throught the data, the levelData always has a "new line" after the end. For example, the levelData[0] is 5;00...2;2<new line>, which cause Integer.parseInt exception. Then I debug, and found this:
rawData.charAt(31) //It's a \r, not \n
So, I change the split line:
String[] levelData = rawData.split("\r");
But now, the levelData[1] will be <newline>5....
What exactly do I have to do to solve this problem? And please explain how "new line" work in Java String.
I suppose that vbNewLine constant put both "\r\n" at the end and hence one character is left while splitting. Try to split it by using both.
Most probably it is from the code you show in VB that is the problem.
I create new line by vbNewLine constant (I think its ASCII code is 13)
First verify this for certain, then look up what code 13 is! Here is a general ascii table.
code 13 is a carrige return and is represented in Java as \r
code 10 is line feed and is represented in Java as \n
A good tip would be to read up a little about NewLines, It's completely fu**ed up, Windows and Linux uses different ways of representing a new line.
CR+LF: Microsoft Windows, DEC TOPS-10, RT-11 and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC-DOS, etc.), Atari TOS, OS/2, Symbian OS, Palm OS
LF: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others.
CR: Commodore 8-bit machines, Acorn BBC, TRS-80, Apple II family, Mac OS up to version 9 and OS-9
Why don't you use Scanner to read your file and split for lines instead?
Scanner sc = new Scanner(new File("levels.text"));
while (sc.hasNextLine()) {
String nextLine = sc.nextLine();
if(nextLine.lenght() > 0) { // you could even use Java regexes to validate the format of every line
String[] levelElements = nextLine.split(";");
// ...
}
}
vbNewLine is platform dependant. on windows newline is comprissed of two characters \n and \r and not just \n

Where's definitive reference on string formatting

does anyone know of a good online resource that simply and definitevly explains how to use the string formatter method...?
I need to write a series of "records" into a set ascii text files. I need to "delimit" each "record" with a cr-lf sequence in a windows 2008 server environment.
Therefore I'm trying to figure out how to add a \r\n character string at the end of each "record". I tried a "record_string.append(CR) and LF" ; but it didn't work.
Thanks much
Guy
The documentation on the Formatter class appears to be comprehensive.
It has this to say about line separators:
Line Separator
The conversion does not correspond to any argument.
'n' - the platform-specific line separator as returned by System.getProperty("line.separator").
Flags, width, and precision are not applicable. If any are provided an IllegalFormatFlagsException, IllegalFormatWidthException, and IllegalFormatPrecisionException, respectively will be thrown.
If you specifically need to add CR LF to the end of each record (carriage return, linefeed), then you can just use exactly \r\n. The \r translates to a carriage return, and \n to linefeed. For example:
StringBuilder sb = new StringBuilder();
sb.append("some data");
// ...
sb.append("\r\n"); // add CR LF record separator
You can find the exact list of escape sequences that exist in Java in section 3.10.6 of the Java Language Specification.
Just do the:
record_string = record_string + "\n"
on widnows \n means CR-LF
Or you can use FileWriter to use writeLine(record);

Categories