【问题标题】:Java find and replace textJava 查找和替换文本
【发布时间】:2011-08-20 05:50:48
【问题描述】:

您好,我正在开发一个 android 应用程序,这是我的问题

我有一个可能有 100 行长的文本文件,它因手机而异,但可以说一个部分是这样的

line 1 = 34
line 2 = 94
line 3 = 65
line 4 = 82
line 5 = 29
etc

每行将等于某个数字,但是该数字将与电话不同,因为我的应用程序将更改此数字,并且在安装我的应用程序之前它可能已经不同。所以这是我的问题,我想在文本文件中搜索“line 3 =”,然后删除整行并将其替换为“line 3 = some number”

我的主要目标是更改第 3 行末尾的数字并保持第 3 行的文本完全相同,我只想编辑数字,但问题是数字总是不同的

我该怎么做呢?感谢您的帮助

【问题讨论】:

    标签: java android search text replace


    【解决方案1】:

    您不能在文件中间“插入”或“删除”字符。即,您不能将文件中间的123 替换为123412

    所以要么你“填充”每个数字,使它们都具有相等的宽度,即你代表43,例如000043,或者你可能不得不重新生成整个文件。

    要重新生成整个文件,我建议您逐行读取原始文件,适当地处理这些行,然后将它们写到一个新的临时文件中。然后,当你完成后,用新文件替换旧文件。

    要处理line,我建议你这样做

    String line = "line 3 = 65";
    
    Pattern p = Pattern.compile("line (\\d+) = (\\d+)");
    Matcher m = p.matcher(line);
    
    int key, val;
    if (m.matches()) {
        key = Integer.parseInt(m.group(1));
        val = Integer.parseInt(m.group(2));
    
        // Update value if relevant key has been found.
        if (key == 3)
            val = 123456;
    
        line = String.format("line %d = %d", key, val);
    }
    
    // write out line to file...
    

    【讨论】:

      【解决方案2】:

      感谢大家的回复,但我最终做的是使用 bash 中的 sed 命令和通配符命令 * 替换该行,然后通过 java 运行脚本,有点像这样

      脚本

      busybox sed -i 's/line 3 = .*/line 3 = 70/g' /path/to/file

      Java

      命令

      execCommand("/path/to/script");

      方法

      public Boolean execCommand(String command) 
      {
          try {
              Runtime rt = Runtime.getRuntime();
              Process process = rt.exec("su");
              DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
              os.writeBytes(command + "\n");
              os.flush();
              os.writeBytes("exit\n");
              os.flush();
              process.waitFor();
          } catch (IOException e) {
              return false;
          } catch (InterruptedException e) {
              return false;
          }
          return true;
      }
      

      【讨论】:

      • 我认为这是一个 hack。正确地做,并使用以前接受的答案。
      【解决方案3】:

      最简单的解决方案是将整个文件读入内存,然后替换要更改的行,然后将其写回文件。

      例如:

      String input = "line 1 = 34\nline 2 = 94\nline 3 = 65\nline 4 = 82\nline 5 = 29\n";
      String out = input.replaceAll("line 3 = (\\d+)", "line 3 = some number");
      

      ...输出:

      line 1 = 34
      line 2 = 94
      line 3 = some number
      line 4 = 82
      line 5 = 29
      

      【讨论】:

        【解决方案4】:

        几个想法。一种更简单的方法(如果可能的话)是将这些行存储在一个集合中(如 ArrayList)并在您的集合中进行所有操作。

        可以找到另一个解决方案here。如果您需要替换文本文件中的内容,您可以定期调用一个方法来执行此操作:

        try {
            BufferedReader in = new BufferedReader(new FileReader("in.txt"));
            PrintWriter out = new PrintWriter(new File("out.txt"));
        
            String line; //a line in the file
            String params[]; //holds the line number and value
        
            while ((line = in.readLine()) != null) {
                    params = line.split("="); //split the line
                    if (params[0].equalsIgnoreCase("line 3") && Integer.parseInt(params[1]) == 65) { //find the line we want to replace
                            out.println(params[0] + " = " + "3"); //output the new line
                    } else {
                            out.println(line); //if it's not the line, just output it as-is
                    }
            }
        
            in.close();
            out.flush();
            out.close();
        

        }catch(Exception e) { e.printStackTrace(); }

        【讨论】:

          猜你喜欢
          • 2011-11-24
          • 2021-10-25
          • 2020-08-23
          • 2011-12-08
          • 2018-12-31
          • 2013-07-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多