msd
msd

Reputation: 11

I'm trying to save a file name with variables that increment

int it_number = 1;
public void write() throws IOException {
       StringBuffer sb = new StringBuffer(name);
       sb.append(it_number);
       System.out.println(sb); 
       it_number++;


      File file = new File(sb + ".txt");
      file.createNewFile();
      FileWriter writer = new FileWriter(file);




      writer.write(results[0] + " " + results[1] + " " + velocity + "\n"     );


     writer.flush();
     writer.close();

    }
}

The String 'name' is a user input. And the iteration number 'it_number' is supposed to increment every time the same user repeats this with the same so I can a few different files with the same name. But this keeps rewriting the file over and over. What can I do to make each iteration different?

Upvotes: 0

Views: 75

Answers (1)

GC_
GC_

Reputation: 508

Try:

static int it_number = 1;

You probably are creating a new object for each write. The static will make all objects share the value.

Note this is will only be unique for a single run of the program. If you want more just use file_<date_time_stamp>.

Upvotes: 1

Related Questions