Steven
Steven

Reputation: 21

Writing an array to a file in Java

Hi i've looked at all the other questions similar to this that I could find and haven't been able to fix my last problem, though the answers to other questions got me this far.

I'm trying to write an array to a file, and so far I can do that, but the writer just writes everything on the same line. For some reason it won't accept my new line command (\n) when viewed in notepad.

An example to try and explain:

Array contents: test, test2, test3, test4

Write to file

File contents: test test2 test3 test4

Whereas i want the file to be: test test2 test3 test4

Below is the segment of my code to write to the file.

    public void save(String fileName) throws FileNotFoundException {

    try {

        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        for ( int i = 0; i < nbrMovies; i++)
        {      
        writer.write(movies[i].getName() + " \n");
        }
        writer.close();
    } catch(IOException ex) {
        ex.printStackTrace();
    }
}

The file doesn't accept the "\n" command and instead just puts a little box next to the last movie added.

Any and all help would be greatly appreciated.

Upvotes: 1

Views: 29127

Answers (8)

Ajinkya Joshi
Ajinkya Joshi

Reputation: 11

import java.io.*;

public class WriteChar 
{

    public static void main(String[] args) 
    {

        try
    {
       File f=new File("WriteChar.txt");
      char c[]={'a','b','c','d','e','f','g'};


       FileWriter out=new FileWriter(f);
       out.write(c);
       System.out.println("Done ..........");
       out.close();
    }

        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

}

Upvotes: 1

fwx
fwx

Reputation: 353

I agree with moving the writer.close() outside the loop. But there is a simpler way of doing what you want - rather than calling the newline function, just append carriage return instead of return at the end of the statement i.e.

change the statement: writer.write(movies[i].getName() + " \n"); to: writer.write(movies[i].getName() + " \r\n");

Upvotes: 0

Reddy
Reddy

Reputation: 8911

There is this writer.newLine(); and move the call to close out of the loop.

Upvotes: 1

CoolBeans
CoolBeans

Reputation: 20800

Take writer.close(); out of the for loop. Your code should be like this:-

 public void save(String fileName) throws FileNotFoundException {
    BufferedWriter writer = null;
    try {

        writer = new BufferedWriter(new FileWriter(fileName));
        for ( int i = 0; i < nbrMovies; i++)
        {      
          writer.write(movies[i].getName());
          writer.newLine();
      writer.flush();
        }

    } catch(IOException ex) {
        ex.printStackTrace();
    } finally{
        if(writer!=null){
            writer.close();
        }  
    }
}

Upvotes: 3

Binil Thomas
Binil Thomas

Reputation: 13779

Why don't you call close outside the loop?

Writer writer = null;
try {
    writer = new BufferedWriter(new FileWriter(fileName));
    for (Movie movie : movies) {      
        writer.write(movie.getName() + " \n");
    }
} catch(IOException ex) {
    ex.printStackTrace();
} finally {
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException ignored) {
        }
    }
}

Upvotes: 0

Carlos Tasada
Carlos Tasada

Reputation: 4444

Move the writer.close() outside the loop. Also, make sure that you flush the writer before closing it.

Upvotes: 0

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

Wrap the FileWriter or BufferedWriter with a PrintWriter, and you will have access to the println(...) method.

Upvotes: 0

Jacob Mattison
Jacob Mattison

Reputation: 51052

First of all, you shouldn't close() the stream after every write; move the close() outside your loop, preferably into a finally block to ensure it is called. Second, rather than writing "\n", a better approach is

writer.write(movies[i].getName());
writer.newLine();
writer.flush(); //optional

Upvotes: 3

Related Questions