Carriage Return \ Line Feed in Java

I created a text file in a Unix environment using Java code.

To write a text file, I use java.io.FileWriter and BufferedWriter . And for a new line after each line, I use the bw.newLine() method (where bw is the BufferedWriter object).

And I send this text file, attaching it by mail from the Unix environment itself (automated using Unix commands).

My problem is that after downloading a text file from mail on Windows, if I opened this text file, the data was not correctly aligned. The newline() symbol does not work, I think so.

I want the same text file alignment as in Unix if I opened the text file in Windows.

How to solve a problem?

The Java code below is for your reference (runs on a Unix environment):

 File f = new File(strFileGenLoc); BufferedWriter bw = new BufferedWriter(new FileWriter(f, false)); rs = stmt.executeQuery("select * from jpdata"); while ( rs.next() ) { bw.write(rs.getString(1)==null? "":rs.getString(1)); bw.newLine(); } 
+43
java carriage-return eol line-endings linefeed
May 14, '10 at 8:25
source share
6 answers

Java only knows about the platform it is currently running on, so it can only give you platform-specific output on that platform (using bw.newLine() ). The fact that you open it on a Windows system means that you either need to convert the file before using it (using what you wrote, or using a program such as unix2dos ), or you must display the file with the window format carriage return in it natively in your Java program. Therefore, if you know that the file will always be open on a Windows machine, you will have to output

 bw.write(rs.getString(1)==null? "":rs.getString(1)); bw.write("\r\n"); 

It is worth noting that you will not be able to output a file that will look correct on both platforms, if it is just plain text that you use, you may want to use html if it is a letter, or xml if it is data. In addition, you may need some kind of client that reads the data and then formats it for the platform that the viewer uses.

+54
May 14 '10 at 8:44
source share

The newLine() method adds a new line compatible with the platform ( 0Dh 0Ah for DOS, 0Dh for older Mac computers, 0Ah for Unix / Linux). Java does not know on which platform you are going to send text. This conversion must be performed by mail senders.

+6
May 14, '10 at 8:30
source share

I don’t know who is looking at your file, but if you open it in a text field instead of notepad, the lines will be displayed correctly. If you use a special file extension, combine it with the word and you will end with it. Or use any other more advanced text editor.

+3
May 14 '10 at 13:55
source share

bw.newLine(); cannot provide compatibility with all systems.

If you are sure that it will open in Windows, you can format it in a new line of Windows.

If you are already using your own unix commands, try unix2dos and convert the already generated file to Windows format, and then send mail.

If you do not use unix commands and prefer to do it in java, use `` bw.write ("\ r \ n") `, and if this does not complicate your program, use a method that detects the operating line and writes the corresponding new line.

+2
May 14 '10 at 8:44 a.m.
source share

If I understand correctly, we are talking about embedding a text file. This is unfortunate, because if it was the body of an email message, you can always use "\ r \ n" referring to http://www.faqs.org/rfcs/rfc822.html

But since this is attachment, you must live with systemic differences. If I were in your place, I would choose one of the following options:

a) only supports Windows clients, using "\ r \ n" as the end of the line.

b) provide two attachment files, one with the linux format and one with the Windows format.

c) I don’t know if the attachment should be read by people or machines, but if it is people, I would consider attaching an HTML file instead of plain text. more portable and much more beautiful :)

+2
May 14 '10 at 9:16
source share

Encapsulate your author to provide a char replacement, for example:

 public class WindowsFileWriter extends Writer { private Writer writer; public WindowsFileWriter(File file) throws IOException { try { writer = new OutputStreamWriter(new FileOutputStream(file), "ISO-8859-15"); } catch (UnsupportedEncodingException e) { writer = new FileWriter(logfile); } } @Override public void write(char[] cbuf, int off, int len) throws IOException { writer.write(new String(cbuf, off, len).replace("\n", "\r\n")); } @Override public void flush() throws IOException { writer.flush(); } @Override public void close() throws IOException { writer.close(); } } 
0
Sep 11 '17 at 12:23
source share



All Articles