1

I am trying to make a Java application that will take in a bulk file with multiple messages, and then split them up and write each message to its own file. The issue that I am having is that its only creating a file with the last message inside, so I think it is overwriting for each iteration of the while loop. My code is below:

public void writeFile(StringBuilder contents, String outputFilePath) throws IOException {

    String messages = contents.toString();
    StringTokenizer st = new StringTokenizer(messages, "$");

    FileWriter fileWriter = null;
    BufferedWriter bufferedFileWriter = null;

    while (st.hasMoreTokens()) {

        int i = 0;
        i++;

        File output = new File(outputFilePath + "_" + i + ".txt");

        try {       
            fileWriter = new FileWriter(output);
            bufferedFileWriter = new BufferedWriter(fileWriter);
            bufferedFileWriter.append(st.nextToken());
        }
        finally {
            if (bufferedFileWriter != null) {
                bufferedFileWriter.close();
            }
            if (fileWriter != null) {
                fileWriter.close();
            }   

        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
MR JACKPOT
  • 206
  • 1
  • 3
  • 15
  • This is where stepping through the code with a debugger could have found the problem faster than posting the code here. every time you run `int i = 0;` it sets `i` to `0`. NOTE: creating lots of files will take you a long time especially on a spinning disk. – Peter Lawrey Jun 25 '18 at 14:45
  • I did try and debug but I guess I was feeling a bit sleepy today. Yes I am just doing this to test that the tokenizer works basically, I will be doing other things with the messages in the future, so I am not too worried about the speed at the moment of creating files. – MR JACKPOT Jun 25 '18 at 14:52
  • If you use try-with-resource to replace about half the code. – Peter Lawrey Jun 25 '18 at 14:57

2 Answers2

4

move the declaration of i:

int i = 0;

outside the while loop:

int i = 0;
while(st.hasMoreTokens(){
    ...
}

That way you're not overwriting it for every iteration. Leaving it always with the value of 1.

An even better approach would be to use a for:

for(int i = 1; st.hasMoreTokens(); i++){
    ...
}

Which leaves you with a nicely scoped variable i only accessible inside the loop

Lino
  • 19,604
  • 6
  • 47
  • 65
1

You can use try-with-resource and the i in an outer loop to simplify the code.

public void writeFile(StringBuilder contents, String outputFilePath) throws IOException {

    StringTokenizer st = new StringTokenizer(contents.toString(), "$");

    for (int i = 0; st.hasMoreTokens(); i++) {
        File output = new File(outputFilePath + "_" + i + ".txt");
        try(FileWriter fileWriter = new FileWriter(output)) {
            fileWriter.append(st.nextToken());
        }
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • So will that automatically close the Writers for each iteration of the loop? – MR JACKPOT Jun 25 '18 at 15:01
  • @AdamH yes it will. If the first one gets an exception it won't blow up trying to close the buffered writer. BTW The BufferedWriter won't do anything useful as you only call append on it once which is what would happen if there was no BufferedWriter. – Peter Lawrey Jun 25 '18 at 15:04