有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将数字增加1?

我需要输入数字的文件

https://github.com/eggham0518/helpme/blob/master/yukim_720.sql

我把它从1放到84

我的一次尝试

    public class main {

    public static void main(String[] args) throws IOException {
        String fileName = "C:/Users/kang/Downloads/yukim.txt";
        List<String> newLines = new ArrayList<>();
        for (String line : Files.readAllLines(Paths.get(fileName), StandardCharsets.ISO_8859_1)) {
             if (line.contains("INSERT INTO yukim_720 VALUES ( '")) {        

                         newLines.add(line.replace("INSERT INTO yukim_720 VALUES ( '",    "INSERT INTO yukim_720 VALUES ( '"+ "'" +i++));
                    } else {
                       newLines.add(line);
                    }                     
        }
        Files.write(Paths.get(fileName), newLines, StandardCharsets.ISO_8859_1);

    }
}

共 (1) 个答案

  1. # 1 楼答案

    你在正确的轨道上。只需声明并初始化一个行号计数器,就可以了。遵循以下步骤:

    public class main {
    
        public static void main(String[] args) throws IOException {
            String fileName = "C:/Users/kang/Downloads/yukim.txt";
            List<String> newLines = new ArrayList<>();
            int i = 85; // <- you lack this one, start from 85 and not 0 since there are already lines that has value and the last one is 84
            for (String line : Files.readAllLines(Paths.get(fileName), StandardCharsets.ISO_8859_1)) {
                 if (line.contains("INSERT INTO yukim_720 VALUES ( ''")) {        
    
                             newLines.add(line.replace("INSERT INTO yukim_720 VALUES ( '",    "INSERT INTO yukim_720 VALUES ( '"+(i++))); // this line was edited
                        } else {
                           newLines.add(line);
                        }                     
            }
            Files.write(Paths.get(fileName), newLines, StandardCharsets.ISO_8859_1);
    
        }
    }