Carriage Return\Line feed in Java

后端 未结 6 911
滥情空心
滥情空心 2020-12-12 21:46

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

For writing the text file I am using java.io.FileWriter and BufferedWriter.

相关标签:
6条回答
  • 2020-12-12 22:18

    Java only knows about the platform it is currently running on, so it can only give you a platform-dependent output on that platform (using bw.newLine()) . The fact that you open it on a windows system means that you either have to convert the file before using it (using something you have written, or using a program like unix2dos), or you have to output the file with windows format carriage returns in it originally in your Java program. So if you know the file will always be opened on a windows machine, you will have to output

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

    It's worth noting that you aren't going to be able to output a file that will look correct on both platforms if it is just plain text you are using, you may want to consider using html if it is an email, or xml if it is data. Alternatively, you may need some kind of client that reads the data and then formats it for the platform that the viewer is using.

    0 讨论(0)
  • 2020-12-12 22:24

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

    If you are sure it is going to be opened in windows, you can format it to windows newline.

    If you are already using native unix commands, try unix2dos and convert teh already generated file to windows format and then send the mail.

    If you are not using unix commands and prefer to do it in java, use ``bw.write("\r\n")` and if it does not complicate your program, have a method that finds out the operating system and writes the appropriate newline.

    0 讨论(0)
  • 2020-12-12 22:28

    If I understand you right, we talk about a text file attachment. Thats unfortunate because if it was the email's message body, you could always use "\r\n", referring to http://www.faqs.org/rfcs/rfc822.html

    But as it's an attachment, you must live with system differences. If I were in your shoes, I would choose one of those options:

    a) only support windows clients by using "\r\n" as line end.

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

    c) I don't know if the attachment is to 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 prettier, too :)

    0 讨论(0)
  • 2020-12-12 22:30

    Don't know who looks at your file, but if you open it in wordpad instead of notepad, the linebreaks will show correct. In case you're using a special file extension, associate it with wordpad and you're done with it. Or use any other more advanced text editor.

    0 讨论(0)
  • 2020-12-12 22:42

    The method newLine() ensures a platform-compatible new line is added (0Dh 0Ah for DOS, 0Dh for older Macs, 0Ah for Unix/Linux). Java has no way of knowing on which platform you are going to send the text. This conversion should be taken care of by the mail sending entities.

    0 讨论(0)
  • 2020-12-12 22:42

    Encapsulate your writer to provide char replacement, like this:

    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 讨论(0)
提交回复
热议问题