0

我读取一个文件并创建一个新文件,该文件复制其中的某些部分,删除一些行并用其他行替换它们。输入数组字符串 raw 的类型为 [Aaa,Bbb,Ccc,..],用于替换部分行。在新文件中,未编辑的部分正确打印,但已编辑的部分以这种方式打印。第一个打印,第二个不打印,第三个是,第四个没有,第五个是......看起来当我编辑一行时,我也删除了下面的一行。我尝试删除 out.write("\n") 或scanner.nextLine() ,但它也没有工作。任何想法我可以尝试什么?提前致谢

For example:

    OLD TEXT: 
    .....
    LINE 6 / contains j(ac)
    LINE 7 / contains i(ac)
    LINE 8 / contains k(ac)
    LINE 9 / contains mp(ac)
    LINE 10 /contains bp(ac)
    .....

    NEW TEXT (NEW FILE):
     .....
    LINE NEW6 
    LINE NEW7 
    LINE NEW8 
    LINE NEW9 
    LINE NEW10 
    .....

public static void main(String[] args) {
    new class();
    class.query();

    File file = new File("file");   
    File filenew = new File("file");

    try {


        PrintWriter out = new PrintWriter(new FileWriter(filenew, true));
        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.contains("j(ac)")) {  
                String newline = line.replaceAll("/.*?/", "/"+raw1+"/");
                scanner.nextLine();
                out.write(newline);
                out.write("\n");
            } else if (line.contains("i(ac)")) {  
                String newline = line.replaceAll("/.*?/", "/"+raw2+"/");
                scanner.nextLine();
                out.write(newline);
                out.write("\n");   
            } else if (line.contains("k(ac)")) { 
                String newline = line.replaceAll("/.*?/", "/"+raw3+"/");
                scanner.nextLine();
                out.write(newline);
                out.write("\n");       
            }else if (line.contains("mp(k)")) {   
                String newline = line.replaceAll("/.*?/", "/"+raw4+"/");
                scanner.nextLine();
                out.write(newline);
                out.write("\n");           
            }else if (line.contains("bp(k)")) {    
                String newline = line.replaceAll("/.*?/", "/"+raw5+"/");
                scanner.nextLine();
                out.write(newline);
                out.write("\n");
            } else{ 
                out.write(line);
                out.write("\n");
            }
      }
      out.flush();
      out.close();
      scanner.close();

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

}
}
4

1 回答 1

1

这就是您在评论中提供的信息的假设方式。

  1. 它的 line.contains 显示我何时要编辑以及哪一行。

  2. 包含“”的那一行是我要替换的那一行。

  3. 我包含的所有 ifs 都应该被使用和打印。

String line; 
while (scanner.hasNextLine()) {
        // Get the line
        line = scanner.nextLine();
       
        // if line contains xxx then replace
        if (line.contains("j(ac)")) {  
            line = line.replaceAll("/.*?/", "/"+raw1+"/");

        } else if (line.contains("i(ac)")) {  
            line = line.replaceAll("/.*?/", "/"+raw2+"/");

        } else if (line.contains("k(ac)")) { 
            line = line.replaceAll("/.*?/", "/"+raw3+"/");

        }else if (line.contains("mp(k)")) {   
            line  = line.replaceAll("/.*?/", "/"+raw4+"/");
    
        }else if (line.contains("bp(k)")) {    
            line = line.replaceAll("/.*?/", "/"+raw5+"/");
           
        } 

        // Write the line with replaced items
        out.write(line);
        out.write("\n");
}

现在你的代码在做什么:

 while (scanner.hasNextLine()) {
            // get the line (very first line) and store that in "line"
            String line = scanner.nextLine();

            // check if line contains "j(ac)"
            if (line.contains("j(ac)")) { 

            // if "line" contains replace everything and save it to "newline" 
                String newline = line.replaceAll("/.*?/", "/"+raw1+"/");

            // get next line getting used for nothing (second line stored nowhere)
                scanner.nextLine();

            // write the newline to output file.
                out.write(newline);
                out.write("\n");
            } 


            // some more if else blocks executing same patterns explained above
            

             else{ 
            // if nothing contains in "line" then write to output file
                out.write(line);
                out.write("\n");
            }
      }
于 2013-09-18T20:17:19.130 回答