1

The following piece of code tries to introduce a newline character into a byte array and write the byte array to a file.

import java.io.*;
public class WriteBytes {
    public static void main(String args[]) {
        byte[] cities = { 'n', 'e', 'w', 'y', 'o', 'r', 'k', '\n', 'd', 'c' };
        FileOutputStream outfile = null;
        try {
            outfile = new FileOutputStream("newfile.txt");
            outfile.write(cities);
            outfile.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Contents of the newfile were: newyorkdc

What I expected was:
newyork
dc

I tried to typecast '\n' to (byte)'\n' but to no avail.

Solution: Change the array initialization to

byte[] cities = { 'n', 'e', 'w', 'y', 'o', 'r', 'k', '\r','\n', 'd', 'c' };

I had used Notepad++ to view the contents of the file.I suppose it suppressed the newline character but accepted the carriage return followed by newline combination.

Chaipau
  • 199
  • 1
  • 5
  • 14
  • 3
    What you have is correct (I've double-checked by running that code locally). The issue must be in how you're looking at the result. – T.J. Crowder Sep 09 '16 at 11:36
  • @T.J.Crowder : I didn't understand what you meant by how I am looking at the result.Isn't it suppose to write the contents of the array in a newline after '\n'? – Chaipau Sep 09 '16 at 11:41
  • 2
    I'm saying that your code worked correctly, so if you think the contents of your file are `newyorkdc`, you're mistaken. Either you were looking at an old file that wasn't written by this code, or the tool you looked at it with hid the `\n` from you. When I ran your code, unmodified, I got a file with the bytes `6e 65 77 79 6f 72 6b 0a 64 63` in it. Note that the 8th byte is a newline (`0a`). When I do `cat newfile.txt`, I see the newline in the result (I'm using \*nix). The code is fine. – T.J. Crowder Sep 09 '16 at 11:43
  • I suggest deleting the question while you investigate further, as it cannot be answered. – T.J. Crowder Sep 09 '16 at 11:45
  • 3
    Just try this: { 'n', 'e', 'w', 'y', 'o', 'r', 'k', '\r', '\n', 'd', 'c' }; (\r added). Maybe this looks better in your editor. – chris Sep 09 '16 at 11:46
  • @T.J.CrowderThanks for helping out.I used Notepad++ to view the file.Probably it skipped the newline character. – Chaipau Sep 09 '16 at 11:46
  • 2
    I agree T.J. Crowder. The code is ok writing the newline char – Egl Sep 09 '16 at 11:46
  • 1
    check here http://stackoverflow.com/questions/24243348/how-to-write-new-line-in-java-fileoutputstream – xro7 Sep 09 '16 at 11:47
  • 1
    Side note: Although the code will work and doesn't have a problem writing the newline to the file, I do suggest using try-with-resources rather than manually closing the stream: http://pastebin.com/51dZefZE – T.J. Crowder Sep 09 '16 at 11:47

2 Answers2

5

New line character is OS dependant, you should retrieve it calling System.getProperty("line.separator")

Or better, if you are writing a textfile you should use BufferedWriter which has the method newLine() to write the line separator independent of the OS

Telcontar
  • 4,794
  • 7
  • 31
  • 39
1

You can prove it works by reading and printing the file.

import java.io.*;


public class Main {
    public static void main(String args[]) {
        String filename = "newfile.txt";
        byte[] cities = {'n', 'e', 'w', 'y', 'o', 'r', 'k', '\n', 'd', 'c'};
        FileOutputStream outfile = null;
        try {
            outfile = new FileOutputStream(filename);
            outfile.write(cities);
            outfile.close();

            BufferedReader in = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] cities2 = {'n', 'e', 'w', 'y', 'o', 'r', 'k', 'd', 'c'};
        outfile = null;
        try {
            outfile = new FileOutputStream(filename);
            outfile.write(cities2);
            outfile.close();

            BufferedReader in = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

Test

newyork
dc
newyorkdc
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424