使用java查找和替换文本文件中的单词
我正在尝试使用java查找并替换文本文件中的某些单词。我的代码在一定程度上可以工作,但是我得到的输出是错误的。 我需要用用户输入替换文本文件中一行中的多个单词,但是当我运行代码时,每替换一个单词,该行就会复制一次 例如,如果我想替换以下三个单词:使用java查找和替换文本文件中的单词,java,Java,我正在尝试使用java查找并替换文本文件中的某些单词。我的代码在一定程度上可以工作,但是我得到的输出是错误的。 我需要用用户输入替换文本文件中一行中的多个单词,但是当我运行代码时,每替换一个单词,该行就会复制一次 例如,如果我想替换以下三个单词: python ycsb phase db -s -P /home/james/YCSB/workloads/workloada -p db.url=db://IP:port -p db.database=name 最后我得到了这行的3个副本,每个副
python ycsb phase db -s -P /home/james/YCSB/workloads/workloada -p
db.url=db://IP:port -p db.database=name
最后我得到了这行的3个副本,每个副本都替换了一个不同的单词。而不是一行替换了所有3个所需单词。
代码如下,提前谢谢
public static void main(String[] args) {
System.out.print("Phase: ");
Scanner sp = new Scanner(System.in);
String p = sp.nextLine();
System.out.print("Database: ");
Scanner sd = new Scanner(System.in);
String d = sd.nextLine();
System.out.print("IP address: ");
Scanner sip = new Scanner(System.in);
int ip = sip.nextInt();
try {
File file = new File("C://users//James//Desktop//newcommand.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while((line = reader.readLine()) != null) {
oldtext += line + "\r\n";
}
reader.close();
String phase = oldtext.replaceAll("phase", "" + p);
String database = oldtext.replaceAll("db", "" + d);
String ips = oldtext.replaceAll("IP", "" + ip);
FileWriter writer = new FileWriter("C://users//James//Desktop//newcommand.txt");
writer.write(phase + ips + database);
writer.close();
} catch (IOException e) {
// handle e
}
}
如果我能很好地理解这种情况,可能问题在于您正在替换相同的字符串,并存储在不同的var中 试试看:
public static void main(String[] args) {
System.out.print("Phase: ");
Scanner sp = new Scanner(System.in);
String p;
p = sp.nextLine();
System.out.print("Database: ");
Scanner sd = new Scanner(System.in);
String d;
d = sd.nextLine();
System.out.print("IP address: ");
Scanner sip = new Scanner(System.in);
int ip = sip.nextInt();
{
try
{
File file = new File("C://users//James//Desktop//newcommand.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while((line = reader.readLine()) != null)
{
oldtext += line + "\r\n";
}
reader.close();
String replacedtext = oldtext.replaceAll("phase", "" + p);
replacedtext = replacedtext.replaceAll("db", "" + d);
replacedtext = replacedtext.replaceAll("IP", "" + ip);
FileWriter writer = new FileWriter("C://users//James//Desktop//newcommand.txt");
writer.write(replacedtext);
writer.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
尽管我没有写下任何东西,所以我没有测试它,但我认为问题在代码的这一部分中已经清楚地看到了:
String phase = oldtext.replaceAll("phase", "" + p);
String database = oldtext.replaceAll("db", "" + d);
String ips = oldtext.replaceAll("IP", "" + ip);
FileWriter writer = new FileWriter("C://users//James//Desktop//newcommand.txt");
writer.write(phase + ips + database);
您正在创建3个新字符串。第一个字符串替换了相位,第二个字符串替换了db,第三个字符串替换了IP。这显然不是你想要的。你应该这样做:
String phase = oldtext.replaceAll("phase", "" + p);
String database = phase.replaceAll("db", "" + d);
String ips = database.replaceAll("IP", "" + ip);
FileWriter writer = new FileWriter("C://users//James//Desktop//newcommand.txt");
writer.write(ips);
编辑:哎呀,太晚了^ ^现有答案的更好版本:
public static void main(String[] args)
{
Scanner sp = new Scanner(System.in);
System.out.print("Phase: ");
String pstr = s.nextLine();
System.out.print("Database: ");
String dstr = s.nextLine();
System.out.print("IP address: ");
String ipstr = String.valueOf(s.nextInt());
读取输入后,我们可以使用两种有效的方法从文件中读取和写回。首先,我建议写一个临时文件(这也是sed
替换文件中文本的方式)。这种方法的一个优点是,最后一步可能是原子操作
File f = new File("C://users//James//Desktop//newcommand.txt");
File ftmp = new File("C://users//James//Desktop//~tmp.newcommand.txt", ".txt");
try
{
BufferedReader br = new BufferedReader(new FileReader(f));
BufferedWriter bw = new BufferedWriter(new FileWriter(ftmp));
String ln;
while((ln = br.readLine()) != null)
{
bw.write(ln
.replace("phase", pstr)
.replace("db", dstr)
.replace("IP", ipstr)
);
bw.newLine();
}
br.close();
bw.close();
Files.move(ftmp.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException e)
{
e.printStackTrace();
}
或者,如果您确实不想拥有一个临时文件,从而将所有内容都保存在RAM中,那么请使用以下代码:
File f = new File("C://users//James//Desktop//newcommand.txt");
try
{
String ENDL = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(f));
String ln;
while((ln = br.readLine()) != null)
{
sb.append(ln
.replace("phase", pstr)
.replace("db", dstr)
.replace("IP", ipstr)
).append(ENDL);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(sb.toString());
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
不要忘记结束括号;)
请注意,与其他答案相反,我使用的是
.replace
而不是.replaceAll
。唯一的区别是后者将第一个参数解释为正则表达式而不是文本字符串。两者都将替换所有出现的情况,在这种情况下,不需要正则表达式,并且可能只会由于特殊字符而导致不必要的行为。@user3504042:只是一条注释:您可以使用String.replace()执行此任务。replaceAll()将标记视为正则表达式,这可能会产生也可能不会产生所需的结果
}