Greg
Greg

Reputation: 9

Copying binary file changes newline character to window's standard..? Java

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();
  }

Upvotes: 0

Views: 555

Answers (1)

BalusC
BalusC

Reputation: 1109635

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.

Upvotes: 1

Related Questions