复制二进制文件会将换行符更改为窗口标准..?爪哇
由于某种原因,此代码正在更改输入中的任何“\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
特定的代码并没有这样做。可能这些
\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 usingPrintWriter#println()
which appends the platform default newlines. Probably the other side is doing that? After all, aReader
/Writer
shouldn't be used for binary data. It may malform it. UseInputStream
/OutputStream
for it.