我有这段代码:
public void makeFile(String sFileName, int gene_ration)
{
try
{
//deleteFile("genetic_algorithm.txt");
if (!myfile.exists()) {
myfile.mkdirs();
}
File gpxfile = new File(myfile, sFileName);
FileWriter writer = new FileWriter(gpxfile, true);
writer.append("Generation " + gene_ration + "\r\n");
for(chromosomesNumber = 0; chromosomesNumber < indexOfChromosomes ; chromosomesNumber++)
{ writer.append(chromosomesNumber + ". " );
writer.append(population[chromosomesNumber] + " ");
writer.append("Nilai fitness : " + population[chromosomesNumber].fitness + "\r\n");
}
writer.append("\r\n");
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
String importError = e.getMessage();
}
}
我想在每次编译时删除textfile上的数据,然后再将数据添加到textfile中。我怎样才能做到这一点?任何的想法?
答案 0 :(得分:0)
有一个File.delete()方法
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#delete()