复制二进制文件会将换行符更改为窗口标准..?爪哇

发布于 2024-09-24 18:47:56 字数 602 浏览 1 评论 0原文

由于某种原因,此代码正在更改输入中的任何“\n”字符,并在新的输出文件中将其替换为“\n\r”。 我参考了几个网站,但仍然没有弄清楚..有人有想法吗? 多谢!

Socket connectionSocket = sData.accept();
InputStream inputStream = connectionSocket.getInputStream();
BufferedInputStream inputBufferedStream = new BufferedInputStream(inputStream);
FileOutputStream outputStream = new FileOutputStream("/home/greg/1");


     byte[] buffer = new byte[1024];
     long count = 0;
     int n = 0;
     while ((n = inputBufferedStream.read(buffer))>=0) {
         outputStream.write(buffer, 0, n);
         count += n;
     }
    outputStream.close();
  }

For some reason, this code is changing any '\n' characters from the input and replacing it with '\n\r' in the new outputed file.
I reference a couple websites, and still haven't figured it out.. Anyone have an idea?
Thanks a lot!

Socket connectionSocket = sData.accept();
InputStream inputStream = connectionSocket.getInputStream();
BufferedInputStream inputBufferedStream = new BufferedInputStream(inputStream);
FileOutputStream outputStream = new FileOutputStream("/home/greg/1");


     byte[] buffer = new byte[1024];
     long count = 0;
     int n = 0;
     while ((n = inputBufferedStream.read(buffer))>=0) {
         outputStream.write(buffer, 0, n);
         count += n;
     }
    outputStream.close();
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-10-01 18:47:56

特定的代码并没有这样做。可能这些 \r\n 已经在输入源中了。

仅当您使用 BufferedReader#readLine() 读取换行符并使用 PrintWriter#println() 附加平台默认值写入时,才会发生这种情况换行符。对方大概也是这么做的吧?毕竟,Reader/Writer 不应该用于二进制数据。它可能会使其变形。使用 InputStream/OutputStream 来实现。

The particular code isn't doing that. Likely those \r\n were simply already in the input source.

It can only happen when you're reading it using for example BufferedReader#readLine() which eats the newlines and writing it using PrintWriter#println() which appends the platform default newlines. Probably the other side is doing that? After all, a Reader/Writer shouldn't be used for binary data. It may malform it. Use InputStream/OutputStream for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文