如何在Java中替换文件
我试图通过创建一个临时文件、处理文本,然后删除原始文件以替换为临时文件来重写文件的内容。方法如下:如何在Java中替换文件,java,file,bufferedreader,bufferedwriter,Java,File,Bufferedreader,Bufferedwriter,我试图通过创建一个临时文件、处理文本,然后删除原始文件以替换为临时文件来重写文件的内容。方法如下: private void deleteLine(String lineToRemove){ File inputFile = new File("./src/class1.txt"); File tempFile = new File("./src/tempFile.txt"); BufferedReader reader; reader = new Buffer
private void deleteLine(String lineToRemove){
File inputFile = new File("./src/class1.txt");
File tempFile = new File("./src/tempFile.txt");
BufferedReader reader;
reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer;
writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
while((currentLine = reader.readLine()) != null) {
if(!currentLine.trim().equals(lineToRemove)){
writer.write(currentLine + System.getProperty("line.separator"));
}
}
writer.close();
reader.close();
System.out.println("Input to temp1: " + tempFile.renameTo(inputFile));
}
在测试这个方法时,我发现除了最后两行都返回false之外,其他一切都按预期工作。方法启动时,我的class1.txt文件存在,但tempFile.txt文件不存在。您的
tempFile.txt文件存在,但您没有看到:),请尝试刷新您的项目(我假设您使用的是eclipse)
右键单击项目文件夹上的
刷新
您将看到该文件
您还需要一个inputFile.delete()代码>呼叫
writer.close();
reader.close();
inputFile.delete();
System.out.println("Input to temp1: " + tempFile.renameTo(inputFile));
您的tempFile.txt
在那里,但您没有看到:),请尝试刷新您的项目(我假设您使用的是eclipse)
右键单击项目文件夹上的
刷新
您将看到该文件
您还需要一个inputFile.delete()代码>呼叫
writer.close();
reader.close();
inputFile.delete();
System.out.println("Input to temp1: " + tempFile.renameTo(inputFile));
使用和的工作解决方案,其中包括:
Path inputPath=Path.get(“foo.txt”);
Path outputPath=Path.get(“foo.txt.tmp”);
try(Stream-lineStream=Files.lines(inputPath);
BufferedWriter writer=Files.newBufferedWriter(outputPath)){
线流
.filter(line->!“pattern.”等于(line.trim())
.forEach(行->{
试一试{
writer.append(行);
writer.newLine();
}捕获(IOE异常){
抛出新的未选中异常(e);
}
});
}
文件。删除(输入路径);
move(outputPath,inputPath);
使用和的工作解决方案,其中包括:
Path inputPath=Path.get(“foo.txt”);
Path outputPath=Path.get(“foo.txt.tmp”);
try(Stream-lineStream=Files.lines(inputPath);
BufferedWriter writer=Files.newBufferedWriter(outputPath)){
线流
.filter(line->!“pattern.”等于(line.trim())
.forEach(行->{
试一试{
writer.append(行);
writer.newLine();
}捕获(IOE异常){
抛出新的未选中异常(e);
}
});
}
文件。删除(输入路径);
move(outputPath,inputPath);
使用apache commons io进行java复制/替换文件操作
private static void copyFileUsingApacheCommonsIO(文件源、文件目的)引发IOException{
FileUtils.copyFile(源、目标);
}
使用apache commons io进行java复制/替换文件操作
private static void copyFileUsingApacheCommonsIO(文件源、文件目的)引发IOException{
FileUtils.copyFile(源、目标);
}
下面的代码工作起来很有魅力:
private static void copyFiles(File source, File dest) throws IOException {
Files.move(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
下面的代码很有魅力:
private static void copyFiles(File source, File dest) throws IOException {
Files.move(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
为什么要使用两个文件?您只需将字符串(lineToRemove)替换为空字符串(“”)。您对“/src/class1.txt”文件有足够的权限吗?大多数情况下,由于缺乏写入权限,系统无法覆盖该文件。是的,我有足够的权限。正如我所说的,我可以写入文件,但是delete和replace函数返回false。为什么要使用两个文件?您只需将字符串(lineToRemove)替换为空字符串(“”)。您对“/src/class1.txt”文件有足够的权限吗?大多数情况下,由于缺乏写入权限,系统无法覆盖该文件。是的,我有足够的权限。正如我所说的,我可以写入文件,但是delete和replace函数返回false。