Java 无法重命名文件
我正在尝试从文件中删除不以特定字符串开头的行。 其想法是将所需的行复制到临时文件中,删除原始文件并将临时文件重命名为原始文件 我的问题是我无法重命名文件Java 无法重命名文件,java,file,file-rename,Java,File,File Rename,我正在尝试从文件中删除不以特定字符串开头的行。 其想法是将所需的行复制到临时文件中,删除原始文件并将临时文件重命名为原始文件 我的问题是我无法重命名文件 tempFile.renameTo(new File(file)) 或 不工作 谁能告诉我出了什么问题吗?代码如下: /** * The intention is to have a method which would delete (or create * a new file) by deleting lines starting
tempFile.renameTo(new File(file))
或
不工作
谁能告诉我出了什么问题吗?代码如下:
/**
* The intention is to have a method which would delete (or create
* a new file) by deleting lines starting with a particular string. *
*/
package com.dr.sort;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class RemoveLinesFromFile {
public void removeLinesStartsWith(String file, String startsWith, Boolean keepOrigFile) {
String line = null;
BufferedReader rd = null;
PrintWriter wt = null;
File tempFile = null;
try {
// Open input file
File inputFile = new File(file);
if (!inputFile.isFile()) {
System.out.println("ERROR: " + file + " is not a valid file.");
return;
}
// Create temporary file
tempFile = new File(file + "_OUTPUT");
//Read input file and Write to tempFile
rd = new BufferedReader(new FileReader(inputFile));
wt = new PrintWriter(new FileWriter(tempFile));
while ((line = rd.readLine()) != null) {
if (line.substring(0, startsWith.length()).equals(startsWith)) {
wt.println(line);
wt.flush();
}
}
rd.close();
if (!keepOrigFile) {
inputFile.delete();
if (tempFile.renameTo(new File(file))) {
System.out.println("OK");
} else {
System.out.println("NOT OK");
}
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
finally {
if (tempFile != null && tempFile.isFile()) {
wt.close();
}
}
}
}
我想您需要在重命名前关闭PrintWriter。我想您需要在重命名前关闭PrintWriter 相反,应该是相反的,因为我们不希望包含指定的行。 因此: 相反,应该是相反的,因为我们不希望包含指定的行。 因此:
您还没有告诉我们您遇到了什么错误,或者您的问题到底是什么。非常抱歉。我的问题是我无法重命名文件。tempFile.renameTo(新文件(File))或tempFile.renameTo(inputFile)不起作用。您没有告诉我们您遇到了什么错误,或者您的问题是什么。非常抱歉。我的问题是我无法重命名文件。tempFile.renameTo(新文件(File))或tempFile.renameTo(inputFile)不起作用。
/**
* The intention is to have a method which would delete (or create
* a new file) by deleting lines starting with a particular string. *
*/
package com.dr.sort;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class RemoveLinesFromFile {
public void removeLinesStartsWith(String file, String startsWith, Boolean keepOrigFile) {
String line = null;
BufferedReader rd = null;
PrintWriter wt = null;
File tempFile = null;
try {
// Open input file
File inputFile = new File(file);
if (!inputFile.isFile()) {
System.out.println("ERROR: " + file + " is not a valid file.");
return;
}
// Create temporary file
tempFile = new File(file + "_OUTPUT");
//Read input file and Write to tempFile
rd = new BufferedReader(new FileReader(inputFile));
wt = new PrintWriter(new FileWriter(tempFile));
while ((line = rd.readLine()) != null) {
if (line.substring(0, startsWith.length()).equals(startsWith)) {
wt.println(line);
wt.flush();
}
}
rd.close();
if (!keepOrigFile) {
inputFile.delete();
if (tempFile.renameTo(new File(file))) {
System.out.println("OK");
} else {
System.out.println("NOT OK");
}
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
finally {
if (tempFile != null && tempFile.isFile()) {
wt.close();
}
}
}
}
if (line.substring(0, startsWith.length()).equals(startsWith))
if (!line.substring(0, startsWith.length()).equals(startsWith))