使用java查找和替换文本文件中的单词 [英] Find and replace words in a text file using java

查看:324
本文介绍了使用java查找和替换文本文件中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to find and replace certain words in a text file using java. My code works to an extent however the output I am getting is wrong. I need to replace multiple words from a line in a text file with user input however when I run my code the line copies itself once for every word I am trying to replace.

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

For example if I want to replace 3 words from the following:

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

我最终获得了3行副本,每行都替换了不同的单词。而不是1行替换所有3个所需的单词。
以下代码,提前感谢。

I end up with 3 copies of the line, each with a different word replaced. Rather than 1 line with all 3 of the required words replaced. Code below, thanks in advance.

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中,

if I understand well the situation, maybe the problem is that you are replacing the same string,and storing in different 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();
         }
     }

这篇关于使用java查找和替换文本文件中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆