所以我试图从文件中删除一行数据,我通过打开一个新文件并写下与我想要删除的数据不匹配的所有信息来成功完成。问题是,在我完成之后,我想删除我的原始文件,然后将新文件重命名为不包括我想要删除的信息,与原始文件同名。我在代码中添加了这样做,但由于某种原因它无法正常工作。
public static void delete() throws IOException
{
File inputFile = new File("Elements.txt");
File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String element = JOptionPane.showInputDialog(null, "Enter the name of the Element you wish to delete.", "Remove an Element.", JOptionPane.INFORMATION_MESSAGE);;
String currentLine;
while((currentLine = reader.readLine()) != null) {
String trimmedLine = currentLine.trim();
if(trimmedLine.startsWith(element)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
JOptionPane.showMessageDialog(null, "Data has been removed from the file: Elements.txt");
}
正如你在底部附近看到的那样,我有以下几行:
inputFile.delete();
tempFile.renameTo(inputFile);
这些行旨在删除我的原始文件(inputFile),然后将我的新文件(tempFile)重命名为原始文件所具有的文件名。然而,在运行代码之后,我只得到一个名为“myTempFile.txt”的文件,该文件已成功删除了我想要的数据行,但我的原始文件仍然存在且未删除,新文件也未重命名为原始文件。
知道为什么会这样吗?
答案 0 :(得分:3)
Use the java.nio.file
API。这是2015年。
final Path src = Paths.get("Elements.txt").toAbsolutePath();
final Path tmp = src.resolveSibling("Elements.txt.new");
try (
final BufferedReader reader = Files.newBufferedReader(src, StandardCharsets.UTF_8);
final BufferedWriter writer = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8,
StandardOpenOption.CREATE_NEW);
) {
// yadda yadda
}
Files.move(tmp, src, StandardCopyOption.REPLACE_EXISTING);
File
is unreliable。它一直都是。
答案 1 :(得分:0)
在这种情况下,我会开始摆弄,阅读文档,也许谷歌搜索一下。但我也会给你一个答案!
inputFile.delete();
这可能会出错,例如,如果您在文本编辑器中打开了文件。
幸运的是delete()
返回一个布尔值,请尝试检查!
正如Niels正确提到的那样File.renameTo()
如果您可以访问Java 7则使用files.nio
替代方案是非常不可靠的。在Java 7中,您可以使用Files.move(Path source, Path target, CopyOption... options)
Java 7文档的文档:http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
答案 2 :(得分:0)
但你的代码对我来说是正常的。我只更改文件的路径,并确保文件未在编辑器中打开
public class NewClass {
public static void main(String[] args) {
try {
delete();
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void delete() throws IOException {
File inputFile = new File("C:\\Users\\olyjosh\\Desktop\\Elements.txt");
File tempFile = new File("C:\\Users\\olyjosh\\Desktop\\myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String element = JOptionPane.showInputDialog(null, "Enter the name of the Element you wish to delete.", "Remove an Element.", JOptionPane.INFORMATION_MESSAGE);;
String currentLine;
while ((currentLine = reader.readLine()) != null) {
String trimmedLine = currentLine.trim();
if (trimmedLine.startsWith(element)) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
JOptionPane.showMessageDialog(null, "Data has been removed from the file: Elements.txt");
}
}