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(bu开发者_如何学Cffer, 0, n);
count += n;
}
outputStream.close();
}
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.
精彩评论