Java file not written to stream with new line characters

We transfer the CSV file from the web service. It seems that we lose new string characters when streaming - the client receives the file in one line. Any idea what we're doing wrong?

the code:

public static void writeFile(OutputStream out, File file) throws IOException { BufferedReader input = new BufferedReader(new FileReader(file)); //File input stream String line; while ((line = input.readLine()) != null) { //Read file out.write(line.getBytes()); //Write to output stream out.flush(); } input.close(); } 
+4
source share
7 answers

Do not use BufferedReader . You already have an OutputStream in your hands, so just get the InputStream file and connect the bytes to the input to output its normal Java IO path . That way, you also don't have to worry about new lines being eaten by BufferedReader :

 public static void writeFile(OutputStream output, File file) throws IOException { InputStream input = null; byte[] buffer = new byte[10240]; // 10KB. try { input = new FileInputStream(file); for (int length = 0; (length = input.read(buffer)) > 0;) { output.write(buffer, 0, length); } } finally { if (input != null) try { input.close(); } catch (IOException logOrIgnore) {} } } 

Using Reader / Writer will include problems with character encoding , if you do not know / specify the encoding in advance. You also should not know about them here. So just leave it aside.

To improve performance a bit more, you can always wrap InputStream and OutputStream in BufferedInputStream and BufferedOutputStream respectively.

+10
source

The readline method uses newline characters to delimit what is read, so newlines themselves are not returned by readLine.

Do not use readline, you can use BufferedInputStream and read the file one byte at a time if you want, or pass your own buffer to OutputStream.write.

Note that, like BalusC and Michael Borgwardt, readers and writers for text, if you just want to copy the file, you should use InputStream and OutputStream, you are only interested in bytes.

+6
source

There are several errors in this code. It can also distort any text without ASCII code, as it is converted twice through the default encoding of the platform and has no good reason.

Do not use Reader to read the file, use FileInputStream and transfer bytes, avoiding unnecessary and potentially destructive encoding conversions. The line break problem will also disappear.

+3
source

Any idea what we're doing wrong?

Yes. This line discards the "new line character"

 while ((line = input.readLine()) != null) { 

And then you write it without it:

  out.write(line.getBytes()); 

This is due to question .

+3
source

BufferedReader.ReadLine () does not save a new line. So you have to add it when writing

+1
source

You can also use the New I / O API ( java.nio.* ):

 private static void copy(File source, File destination) throws IOException { long length = source.length(); FileChannel input = new FileInputStream(source).getChannel(); try { FileChannel output = new FileOutputStream(destination).getChannel(); try { for (long position = 0; position < length; ) { position += input.transferTo(position, length-position, output); } } finally { output.close(); } } finally { input.close(); } } 

The loop may not be needed, but the Channel.trasnferTo() documentation says

may or may not transmit all requested bytes

+1
source

You can use PrintWriter, which offers the prinln () method. It will also save you from converting a string to an array of characters.

 public static void writeFile(OutputStream o, File file) throws IOException { PrintWriter out = new PrintWriter(new OutputStreamWriter(o)); BufferedReader input = new BufferedReader(new FileReader(file)); //File input stream String line; while ((line = input.readLine()) != null) { //Read file out.println(line); //Write to output stream out.flush(); } input.close(); } 
0
source

All Articles