1

I am having a hard time getting my program to add all the XML data from the URL to a .txt file without always writing over the data that is already in the aforementioned file. I am trying to use the program to get XML data for all videos uploaded by a channel into a .txt file which will be referenced later.

filename = new File("xmlData.txt");
filename.createNewFile();

while (flag1 == 0)
{
    URL url = new URL("https://gdata.youtube.com/feeds/api/users/"+Name+"/uploads?    max-query=50&start-index="+index);

    try (BufferedReader br = new BufferedReader(
    new InputStreamReader(url.openStream())))
    {                
        while ((inputLine = br.readLine()) != null)
        {        
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) 
            {
                bw.write(inputLine);
                bw.newLine();
            }
        }
    }

    if (count1 < 25)
    {
        index = index+50;
        count1++;
    }
    else
    {
        flag1 = 1;
    }
}

System.out.println("Done writing to xmlData.txt");
Unihedron
  • 10,902
  • 13
  • 62
  • 72
CamthraX
  • 57
  • 6

2 Answers2

2

FileWriter constructor does allow to pass a second, boolean, parameter, append.

The Javadoc states:

Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

Parameters:
file a File object to write to
append if true, then bytes will be written to the end of the file rather than the beginning

So, create a FileWriter like so:

new FileWriter(filename, true);

Also, I would open the file outside the while loop. This will make your file open once and append to the file while you read from the XML file (URL).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 1
    +1 For "Open the Writer outside the loop", I was thinking, why did the OP do that? – Ruan Mendes Aug 27 '14 at 15:41
  • Beats me! I see the OP is using the new [Try with Resources Statement](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) so I guess that's why? – Buhake Sindi Aug 27 '14 at 15:44
  • tbh i hadnt even thought of that. it started without the loop because i had originally thought the gdata would've given me all the xml data for all the videos. it however only returns 50 results and then needs to be indexed for the next 50 and so on. so i just threw a loop in with a flag and counter. gonna change it all up now. – CamthraX Aug 27 '14 at 15:48
  • @BuhakeSindi I like that new feature, hadn't heard of it. I haven't written Java in over two years though. – Ruan Mendes Aug 27 '14 at 17:07
1

The second parameter to the file writer is boolean append. See Java FileWriter with append mode and http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.File,%20boolean)

new BufferedWriter(new FileWriter(filename, true))) 
Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • @CamthraX You should make sure you take the advice here http://stackoverflow.com/a/25531376/227299, I'm not sure why you are opening multiple writers from your loop – Ruan Mendes Aug 27 '14 at 15:42