我需要写入字符串ByteArrayOutputStream,但我需要用破坏来编写字符串.我试着这样做:
out.write("123".getBytes());
out.write("\n456".getBytes());
Run Code Online (Sandbox Code Playgroud)
但'\n'不起作用.请告诉我,我该如何解决?或者建议我替换OutputStream来存储字符串(这个操作系统必须允许使用断行)而不需要创建文件.谢谢.
ByteArrayOutputStream stream = ...;
PrintStream printer = new PrintStream(stream, true); // auto-flush
printer.println("123"); // writes newline
printer.print("hello"); // no new line
printer.print(" world");
printer.println();
Run Code Online (Sandbox Code Playgroud)
请注意,这将生成特定于平台的字节.换行符可以是\n,\r\n或\r.使用的实际字符序列在系统属性中line.separator指定.