0

I've been trying this for the last half an hour, but all I can come up with is the following:

for (int y = 0; y < colectieTotala.size(); y++)
        for (int k = 0; k < colectieTotala.get(y).size(); k++)
        {
            try(BufferedWriter output = new BufferedWriter(new FileWriter("D:\\Programe\\NetBeans\\Proiecte NetBeans\\[PA]Tema3Ex1\\Colectie.txt", true)))
            {
                output.write(colectieTotala.get(y).get(k)); output.newLine();
            }

        }

This is the structure of Colectie.txt: http://postimg.org/image/656f1mow5/

The problem is that it wrote the current String only on the first line of the file Colectie.txt, replacing the String introduced earlier, so that at the end of the program all I have in the Colectie.txt is this: /

I think it's just something easy, but can't figure it out what...

EDIT:

OK, I did edit my code, and this is how it looks now:

BufferedWriter output = new BufferedWriter(new FileWriter("D:\\Programe\\NetBeans\\Proiecte NetBeans\\[PA]Tema3Ex1\\Colectie.txt"));

for (int y = 0; y < colectieTotala.size(); y++)
    for (int k = 0; k < colectieTotala.get(y).size(); k++)
    {
        System.out.println("Element curent -> " + colectieTotala.get(y).get(k));

        try
        {
            output.write(colectieTotala.get(y).get(k)); 
            output.newLine();
        }
        catch (IOException e)
        {
            System.err.println(e);
        }

    }

The problem now is that Colectie.txt it's empty after this gets executed. Where's the mistake now ?

miTzuliK
  • 93
  • 1
  • 1
  • 11
  • 3
    When you want to write several things in a file, you open a writer, write your different things, and then close the writer. You don't open the writer, write one thing, close the writer, open the writer, write one thing, close the writer, etc. Also, PrintWriter has a println() method. – JB Nizet Mar 08 '15 at 08:00
  • `BufferedWriter output = new BufferedWriter(new FileWriter("D:\\Programe\\NetBeans\\Proiecte NetBeans\\[PA]Tema3Ex1\\Colectie.txt", true));)` keep this line before the for loops – Tushar Gupta Mar 08 '15 at 08:00
  • Since you use Java 7+, please use `Files.newBufferedWriter()` – fge Mar 08 '15 at 08:06

5 Answers5

2

The problem is that it wrote the current String only on the first line of the file Colectie.txt

This is because you keep creating the new file inside the nested loop.

Move try(BufferedWriter output = ... block outside your nested loops to fix this:

BufferedWriter output = new BufferedWriter(new FileWriter("D:\\Programe\\NetBeans\\Proiecte NetBeans\\[PA]Tema3Ex1\\Colectie.txt", true));
try {
    for (int y = 0; y < colectieTotala.size(); y++) {
        for (int k = 0; k < colectieTotala.get(y).size(); k++) {
            output.write(colectieTotala.get(y).get(k));
            output.newLine();
        }
    }
} finally {
    output.close();
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Try appending all the data first into a StringBuffer object and in the end write it to the file and then flush the BufferedWriter.

Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
0
BufferedWriter output = new BufferedWriter(new FileWriter("D:\\Programe\\NetBeans\\Proiecte NetBeans\\[PA]Tema3Ex1\\Colectie.txt", true));)

Keep this before the nested for's

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
0

It's best to move up buffered writer creating. Otherwise you are wasting resources.

So move following line out side of loop.

BufferedWriter output = new BufferedWriter(new FileWriter("D:\\Programe\\NetBeans\\Proiecte NetBeans\\[PA]Tema3Ex1\\Colectie.txt", true));

And then in the body of the loop change write to append as following.

output.append(colectieTotala.get(y).get(k));
            output.newLine();
Don Srinath
  • 1,565
  • 1
  • 21
  • 32
0

You're opening and closing the file in the inner loop, which means it gets overwritten all the time. You need to open it at the beginning and close it at the end.

Also using index variables isn't very idiomatic, it's much cleaner to use the for-each construction as in:

BufferedWriter output = new BufferedWriter(new FileWriter("youtfile.txt", true));
try {
  for(List<String> lst : colectieTotala) {
    for(String line : lst) {
      output.write(line);
      output.newLine();
    }
  }
} finally {
  output.close();
}
Community
  • 1
  • 1
Petr
  • 62,528
  • 13
  • 153
  • 317
  • Hmmm, good point, I will try to update my style of coding right after I finish this project. I guess this is why I get this warning: "Use enhanced for to iterate over the array.". – miTzuliK Mar 08 '15 at 08:16