много-много раз через отладчик пыталась понять, почему прога не работает. Создается временный файл, туда все хорошо записывается, а потом Could not delete file, почему???? соответственно прога завершает свою работу, исходный файл остался, остался и непереименованный временный файл.
public void removeLineFromFile(String file, String lineToRemove)
{
try
{
File inFile = new File(file);
if (!inFile.isFile())
{
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null)
{
if (!line.trim().equals(lineToRemove))
{
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete())
{
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
госик
Ваш код у меня вполне корректно работает.
Если где-то раньше по программе идёт работа с входным файлом — убедитесь, что вы его закрыли, как правило, именно это вызывает ошибку (]]>http://forum.codeby.net/go.php?http://forum.sources.ru/index.php?showtopic=146994&hl=]]>).
"Could not rename file" является прямым следствием того, что исходный файл не был удалён — под windows невозможно переименовать в уже существующий файл.
попробуй явно закрывать FileReader и FileWriter, возможно у тебя какая то специфическая версия jvm
Форум Invision Power Board (http://nulled.ws)
© Invision Power Services (http://nulled.ws)