Java FileIO错误,未分析所有类
我正在尝试创建一个jar文件,该文件分析目录中的所有文件以查找提供的字符串,并将其替换为提供的字符串+。getInstance(),我有以下代码:Java FileIO错误,未分析所有类,java,file-io,iterator,Java,File Io,Iterator,我正在尝试创建一个jar文件,该文件分析目录中的所有文件以查找提供的字符串,并将其替换为提供的字符串+。getInstance(),我有以下代码: public static String toAnalyze; public static String path; public static void main(String[] args) { if (args.length != 2) { System.out.println("Usa
public static String toAnalyze;
public static String path;
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java InstanceFixer <To Analyze> <Path>" );
System.exit(1);
}
toAnalyze = args[0];
path = args[1];
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date resultDate = new Date(System.currentTimeMillis());
LoggingUtils.log("Instance Fixer","Started InstanceFixer on analysis " + toAnalyze + " at " + sdf.format(resultDate));
startIterating();
}
public static void startIterating() {
File dir = new File(path);
System.out.println(dir);
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
filterFile(child);
}
LoggingUtils.log("Iterator","Analyzed " + directoryListing.length + " files.");
} else {
LoggingUtils.log("Iterator", path + " is not valid \n won't be able to Analyze files (if any).");
}
}
public static void filterFile(File file) {
try {
BufferedReader in = new BufferedReader(new FileReader(file));
BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
String read = in.readLine();
if (read.contains(toAnalyze + "Manager")) {
out.write(read.replace(toAnalyze + "Manager", toAnalyze + "Manager.getInstance()"));
LoggingUtils.log("Filter","Analyzed file " + file.getName() + ", found and fixed instance.");
out.close();
} else {
LoggingUtils.log("Filter","Analyzed file " + file.getName() + ", didn't find anything to rename, skipped.");
}
in.close();
} catch (IOException e) {
LoggingUtils.log("Filter","There was a problem: " + e);
e.printStackTrace();
}
}
公共静态字符串进行分析;
公共静态字符串路径;
公共静态void main(字符串[]args){
如果(参数长度!=2){
System.out.println(“用法:JavaInstanceFixer”);
系统出口(1);
}
toAnalyze=args[0];
path=args[1];
SimpleDataFormat sdf=新的SimpleDataFormat(“HH:mm:ss”);
日期结果日期=新日期(System.currentTimeMillis());
LoggingUtils.log(“实例修复程序”,“分析时启动InstanceFixer”+toAnalyze+”位于“+sdf.format(resultDate));
星滴度();
}
公共静态无效起始值(){
文件目录=新文件(路径);
系统输出打印项次(dir);
File[]directoryListing=dir.listFiles();
if(directoryListing!=null){
用于(文件子项:目录列表){
过滤器文件(儿童);
}
LoggingUtils.log(“迭代器”,“分析的”+目录列表.length+“文件”);
}否则{
LoggingUtils.log(“迭代器”,路径+”无效\n将无法分析文件(如果有)。”;
}
}
公共静态无效筛选器文件(文件){
试一试{
BufferedReader in=新的BufferedReader(新文件读取器(文件));
BufferedWriter out=new BufferedWriter(new FileWriter(file,true));
String read=in.readLine();
if(read.contains(toAnalyze+“Manager”)){
out.write(read.replace(toAnalyze+“Manager”,toAnalyze+“Manager.getInstance()”);
LoggingUtils.log(“过滤器”,“分析文件”+文件.getName()+”,找到并修复了实例。”);
out.close();
}否则{
LoggingUtils.log(“过滤器”,“分析文件”+文件.getName()+”,未找到任何要重命名的内容,已跳过。“);
}
in.close();
}捕获(IOE异常){
LoggingUtils.log(“过滤器”,“出现问题:”+e);
e、 printStackTrace();
}
}
问题在于,它只在第一行或最后一行搜索,而不是替换文本,而是将其写入旧文本的一侧。我能做些什么来解决这个问题
它不替换文本,而是将其写在旧文本的侧面
在newfilewriter(file,true)
中,参数true
正在设置append
选项意味着writer将在文件末尾添加新内容。另一方面,将其设置为false
意味着文件将被清理,writer将从一开始就开始写入它(如果它包含多行,这可能不是您想要的)
但无论如何,您不应该同时对分析的同一文件进行读写。反而
- 创建新的临时文件
- 迭代原始文件的所有行
- 在临时文件中写入行-包含所需的更改
- 删除原始文件
- 将临时文件重命名为与原始文件同名
xxx:
的内容,其中x
表示数字(如果数字没有三位数字,则为空格)。所以我们可以这样做
try {
File original = new File("input.txt");
File tmp = new File("tmp.txt");
BufferedReader in = new BufferedReader(new FileReader(original));
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(tmp)));
String line = null;
int i = 1;
while ((line = in.readLine()) != null) {
out.format("%3d: %s%n", i++, line);// %d digit, %s string, %n
// line separator
}
in.close();
out.close();
// lets swap files
if (original.delete()) {
System.out.println("original file removed");
if (tmp.renameTo(original)) {
System.out
.println("temporary file renamed to original file");
} else {
System.out.println("temporary file couldn't be renamed");
}
} else {
System.out.println("original file couldn't be removed");
}
} catch (IOException e) {
e.printStackTrace();
}
@user3241491添加了示例。这与您想要完成的事情并不完全相同,但应该给您一个基本的想法。@user3241491实际上,最后一部分(用包含新数据版本的临时文件替换原始文件)可以使用
Files.copy(tmp.toPath()、original.toPath()、StandardCopyOption.REPLACE_EXISTING)缩短代码>