Jared Trujillo Jared Trujillo - 1 year ago 77
Java Question

how to make a new line on tex file for FileOutputStream

How can I create a new line for each loop? My program keeps on displaying everything in a single line... or how can I set a diff class to write data from arrays

static void writetofile(studentClass[] students)
try(DataOutputStream str= new DataOutputStream(new FileOutputStream("new.txt")) )

for(int i=0;i<students.length;i++)
str.writeBytes(students[i].getStudentFname()+", ");
str.writeBytes(students[i].getStudentLname()+" ");
str.writeBytes(" ");

catch(Exception e)

Answer Source

I suspect that you are using the wrong line separator for however you are trying to view the file: Windows uses \r\n as the line separator, *nix uses \n etc.

If you are using a text editor/viewier worth its salt to view the file (i.e. not notepad), this shouldn't matter, though - it should detect the line separator.

You might find it easier to use a java.util.Formatter instead of a DataOutputStream (and an enhanced for loop):

try (Formatter fmt = new Formatter(new FileOutputStream(...)) {
  for (studentClass student : students) {
    fmt.format("%s, %s %d %s%n",

Note that this uses %n, the platform-specific line separator for the platform on which the code is run. If you want to specify a particular line separator, you can use \n (or \r\n, or whatever) instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download