使用java查找和替换文本文件中的单词

我试图使用java查找和替换文本文件中的某些单词。 我的代码在某种程度上工作,但我得到的输出是错误的。 我需要使用用户输入替换文本文件中的一行中的多个单词,但是当我运行我的代码时,该行会为我要替换的每个单词复制一次。

例如,如果我想替换以下3个单词:

python ycsb phase db -s -P /home/james/YCSB/workloads/workloada -p db.url=db://IP:port -p db.database=name 

我最终获得了3行副本,每行都替换了不同的单词。 而不是1行替换所有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如何替换文件中的文本)。 这种方法的一个优点是最终的移动可能是primefaces操作。

  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 。 唯一的区别是后者将第一个参数解释为正则表达式而不是文字字符串。 两者都替换所有出现的情况,在这种情况下,不需要正则表达式,并且可能仅由于特殊字符而导致不希望的行为。