Java 有人能检查一下我关于替换.txt文件中的数字的代码吗?我能';不要替换里面的数字

Java 有人能检查一下我关于替换.txt文件中的数字的代码吗?我能';不要替换里面的数字,java,io,pattern-matching,matcher,Java,Io,Pattern Matching,Matcher,我有以下代码,用于替换文本文件中的数字的赋值: import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.Random; import java.util.Scanner; public class L20 { public static void ma

我有以下代码,用于替换文本文件中的数字的赋值:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Random;
import java.util.Scanner;

public class L20 {

   public static void main(String[] args) throws IOException {
      Scanner input = new Scanner(System.in);

      System.out.println("Enter file name");
      String in = input.nextLine();
      try {
         textWriter(in);
         textReader(in);
         textChanger(in);
      } catch (Exception e) {
      }
   }

   public static void textWriter(String path) throws IOException {
      String[] alphabet = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
            "k", "m", "l", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
            "x", "y", "z" };
      File file = new File(path);
      Writer output = null;
      Random number = new Random();
      output = new BufferedWriter(new FileWriter(file));
      int lines = 10 + number.nextInt(11);
      for (int i = 0; i < lines; i++) {
         int it2 = 1 + number.nextInt(9);
         int n1 = number.nextInt(26);
         int n2 = number.nextInt(26);
         int n3 = number.nextInt(26);

         String t2 = Integer.toString(it2);
         String t1 = alphabet[n1] + alphabet[n2] + alphabet[n3];
         String text = t1 + t2;
         output.write(text);
         ((BufferedWriter) output).newLine();
      }
      output.close();
      System.out.println("Your file has been written");
   }

   public static void textReader(String path) throws IOException {
      File file = new File(path);
      Scanner input;
      input = new Scanner(file);
      String line;
      while ((line = input.nextLine()) != null) {
         System.out.println(line);
      }
      input.close();
   }
}
我想做的是只替换Text.txt文件中每行的数字部分。每一行由三个字母和一个数字组成。我只想更改数字部分。

试试这个

public class Reg {

    public static void main(String[] args){

        String s = "abc1";
        Pattern p = Pattern.compile("\\d");
        Matcher m = p.matcher(s);
        String no = new String();

        while (m.find()){

              no = m.group();
        }

        String newStr = s.replace(no, "hi");
        System.out.println(newStr);
       }
}

@BheshGurung但是我怎么才能只改变数字部分呢?这条线看起来怎么样?你想用什么来代替数字部分呢?每一条线就像dfg6——由三个字母和一个数字组成。我想,你在另一个问题中找到了这个问题的答案了。。。由于某种原因,我的TextChanger()方法不起作用。。您的方法完全有效,但我需要TextChanger()方法
public class Reg {

    public static void main(String[] args){

        String s = "abc1";
        Pattern p = Pattern.compile("\\d");
        Matcher m = p.matcher(s);
        String no = new String();

        while (m.find()){

              no = m.group();
        }

        String newStr = s.replace(no, "hi");
        System.out.println(newStr);
       }
}