如何在 Java 中向字节数组添加换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39410670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:20:47  来源:igfitidea点击:

How to add newline character to a byte array in Java?

javabytearrayfileoutputstream

提问by Chaipau

The following piece of code tries to introduce a newline character into a byte array and write the byte array to a file.

以下代码尝试将换行符引入字节数组并将字节数组写入文件。

import java.io.*;
public class WriteBytes {
    public static void main(String args[]) {
        byte[] cities = { 'n', 'e', 'w', 'y', 'o', 'r', 'k', '\n', 'd', 'c' };
        FileOutputStream outfile = null;
        try {
            outfile = new FileOutputStream("newfile.txt");
            outfile.write(cities);
            outfile.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Contents of the newfile were: newyorkdc

新文件的内容是: newyorkdc

What I expected was:
newyork
dc

我的预期是:
纽约
dc

I tried to typecast '\n'to (byte)'\n'but to no avail.

我试图打字'\n'(byte)'\n'但无济于事。

Solution: Change the array initialization to

解决方法:将数组初始化改为

byte[] cities = { 'n', 'e', 'w', 'y', 'o', 'r', 'k', '\r','\n', 'd', 'c' };

I had used Notepad++ to view the contents of the file.I suppose it suppressed the newline character but accepted the carriage return followed by newline combination.

我曾使用 Notepad++ 查看文件的内容。我想它抑制了换行符,但接受了回车,然后是换行符组合。

回答by Telcontar

New line character is OS dependant, you should retrieve it calling System.getProperty("line.separator")

换行符取决于操作系统,您应该调用 System.getProperty("line.separator") 检索它

Or better, if you are writing a textfile you should use BufferedWriterwhich has the method newLine() to write the line separator independent of the OS

或者更好的是,如果您正在编写文本文件,则应该使用具有 newLine() 方法的BufferedWriter来编写独立于操作系统的行分隔符

回答by Niklas

You can prove it works by reading and printing the file.

您可以通过阅读和打印文件来证明它有效。

import java.io.*;


public class Main {
    public static void main(String args[]) {
        String filename = "newfile.txt";
        byte[] cities = {'n', 'e', 'w', 'y', 'o', 'r', 'k', '\n', 'd', 'c'};
        FileOutputStream outfile = null;
        try {
            outfile = new FileOutputStream(filename);
            outfile.write(cities);
            outfile.close();

            BufferedReader in = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] cities2 = {'n', 'e', 'w', 'y', 'o', 'r', 'k', 'd', 'c'};
        outfile = null;
        try {
            outfile = new FileOutputStream(filename);
            outfile.write(cities2);
            outfile.close();

            BufferedReader in = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

Test

测试

newyork
dc
newyorkdc