用Java查找并替换多个文本文件中的单词?
如何使用Java在多个文本文件中查找和替换一个单词 下面是我如何为一个字符串做的用Java查找并替换多个文本文件中的单词?,java,Java,如何使用Java在多个文本文件中查找和替换一个单词 下面是我如何为一个字符串做的 您可以使用BufferedReader包装的文件读取器读入文件,逐行读入,对问题中显示的字符串执行相同的替换,然后将其写回新文件。您可以使用BufferedReader包装的文件读取器读入文件,逐行读入,对问题中显示的字符串执行相同的替换,并将其写回新文件。使用FileUtils从: 从以下位置使用FileUtils: 这是工作代码:希望有帮助! } 这是工作代码:希望有帮助! } 如果您想将\r\n替换为\n
您可以使用BufferedReader包装的文件读取器读入文件,逐行读入,对问题中显示的字符串执行相同的替换,然后将其写回新文件。您可以使用BufferedReader包装的文件读取器读入文件,逐行读入,对问题中显示的字符串执行相同的替换,并将其写回新文件。使用FileUtils从: 从以下位置使用FileUtils:
这是工作代码:希望有帮助! }
这是工作代码:希望有帮助! }
如果您想将\r\n替换为\n.@aioobe,我不明白为什么会有特别大的问题。显然,如果您需要替换可能超过一行的内容,这会有点复杂,但总比一次将整个文件放入内存要好。我只想知道我必须添加到程序中的库,而不是如果您想将\r\n替换为\n。@aioobe我不明白为什么会有特别大的问题。很明显,如果你需要替换一些可能比一行长的东西,这会有点复杂,但总比一次将整个文件放入内存要好。我只想知道我必须添加到程序中的库这个代码行吗?导入java.io.File;公共类FindNReplace{public static void mainString args[]{String[]files={file1.txt,file2.txt,file3.txt};对于字符串文件:files{file f=new Filefile file;String content=FileUtils.readFileToStringnew filefilefilefilefilefilename.txt;FileUtils.writeStringToFilef,content.replaceAllhello,world;}}对不起,我不知道如何回答我似乎无法回答自己因为我是新来的,下面是错误代码:run:Exception in thread main java.lang.UnsupportedOperationException:尚未在FindReplace.FileUtils.ReadFileToString FileUtils.java:17 at FindReplace.FindReplace.MainFindReplace.java:13 java结果:1构建成功总时间:1秒您使用的是版本2.1吗。这个方法是绝对实现的。这个代码可以吗?导入java.io.File;公共类FindNReplace{public static void mainString args[]{String[]files={file1.txt,file2.txt,file3.txt};对于字符串文件:files{file f=new Filefile file;String content=FileUtils.readFileToStringnew filefilefilefilefilefilename.txt;FileUtils.writeStringToFilef,content.replaceAllhello,world;}}对不起,我不知道如何回答我似乎无法回答自己因为我是新来的,下面是错误代码:run:Exception in thread main java.lang.UnsupportedOperationException:尚未在FindReplace.FileUtils.ReadFileToString FileUtils.java:17 at FindReplace.FindReplace.MainFindReplace.java:13 java结果:1构建成功总时间:1秒您使用的是版本2.1吗。这个方法肯定是实现的。
public class ReplaceAll {
public static void main(String[] args) {
String str = "We want replace replace word from this string";
str = str.replaceAll("replace", "Done");
System.out.println(str);
}
}
String[] files = { "file1.txt", "file2.txt", "file3.txt" };
for (String file : files) {
File f = new File(file);
String content = FileUtils.readFileToString(new File("filename.txt"));
FileUtils.writeStringToFile(f, content.replaceAll("hello", "world"));
}
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class TestIO {
static StringBuilder sbword = new StringBuilder();
static String dirname = null;
static File[] filenames = null;
static Scanner sc = new Scanner(System.in);
public static void main(String args[]) throws FileNotFoundException, IOException{
boolean fileread = ReadFiles();
sbword = null;
System.exit(0);
}
private static boolean ReadFiles() throws FileNotFoundException, IOException{
System.out.println("Enter the location of folder:");
File file = new File(sc.nextLine());
filenames = file.listFiles();
String line = null;
for(File file1 : filenames ){
System.out.println("File name" + file1.toString());
sbword.setLength(0);
BufferedReader br = new BufferedReader(new FileReader(file1));
line = br.readLine();
while(line != null){
System.out.println(line);
sbword.append(line).append("\r\n");
line = br.readLine();
}
ReplaceLines();
WriteToFile(file1.toString());
}
return true;
}
private static void ReplaceLines(){
System.out.println("sbword contains :" + sbword.toString());
System.out.println("Enter the word to replace from each of the files:");
String from = sc.nextLine();
System.out.println("Enter the new word");
String To = sc.nextLine();
//StringBuilder sbword = new StringBuilder(stbuff);
ReplaceAll(sbword,from,To);
}
private static void ReplaceAll(StringBuilder builder, String from, String to){
int index = builder.indexOf(from);
while(index != -1){
builder.replace(index, index + from.length(), to);
index += to.length();
index = builder.indexOf(from,index);
}
}
private static void WriteToFile(String filename) throws IOException{
try{
File file1 = new File(filename);
BufferedWriter bufwriter = new BufferedWriter(new FileWriter(file1));
bufwriter.write(sbword.toString());
bufwriter.close();
}catch(Exception e){
System.out.println("Error occured while attempting to write to file: " + e.getMessage());
}
}