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

Jam*_*esF 6 java

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

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

python ycsb phase db -s -P /home/james/YCSB/workloads/workloada -p 
db.url=db://IP:port -p db.database=name
Run Code Online (Sandbox Code Playgroud)

我最终获得了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
    }
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*rez 9

如果我很了解这种情况,可能问题是你要替换相同的字符串,并存储在不同的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();
         }
     }
Run Code Online (Sandbox Code Playgroud)

  • @ user3504042:只需注释:您可以使用String.replace()执行此任务.String.replaceAll()将标记视为正则表达式,可能会也可能不会产生所需的结果 (2认同)