import java.io.*;
public class Main {
public static void main(String[] args)
{
PrintWriter pw = null;
//case 1:
try
{
pw = new PrintWriter(new BufferedWriter(new FileWriter(new File("case1.txt"),false)),true);
}
catch(IOException e)
{
e.printStackTrace();
}
for(int i=0;i<100;i++)
pw.write("Hello " + String.valueOf(i));
pw.close();
//case 2:
try
{
pw = new PrintWriter(new FileWriter(new File("case2.txt"),false),true);
}
catch(IOException e)
{
e.printStackTrace();
}
for(int i=0;i<100;i++)
pw.write("Hello " + String.valueOf(i));
pw.close();
}
}
In both cases, the pw.write(...)
appends to file so that the output contains one hundred messages,whilst only the last is desired. What is the best(I mean most graceful or efficient) method to do what I want?
UPDATE
Answers like "just print the last value" are unacceptable as this example is only SSCCE from larger problem.