如何 select txt 文件中的数据并打印到不同的 txt 文件中 java

how to select data in txt file and print into different txt file java

我有一个名为 employees.txt 的 txt 文件,其中包含员工 ID 和姓名。他们的 ID 以 18,19 或 20 开头。如果行以 18 开头,则必须在文件中查找它需要转到另一个 txt 文件 employee18.txt 如果以 19 开头,则转到 employee19.txt 和同样,如果它以 20 开头,则转到 employee20.txt.I 分享我到目前为止所做的代码。我已经能够读取该文件,并且还可以通过使用扫描仪查找以 18,19 或 20 开头的行。问题是当我 运行 代码时,它向控制台显示了所有值,但只添加了最后一个值在 employee.txt 文件中到 employee18.txt 或 employee19.txt 和 employee20.txt.What 我做错了 我不知道 employees.txt 中的条目是

1801234 IMRAN KHAN
1801235 ANDREW FLINTOFF
1905001 SACHIN TICKHULE
1905002 VIRAT KOHLI
2006001 KATY SPENCER
2006002 RANDOM MAN

运行宁码后

    1801234 IMRAN KHAN
    1801235 ANDREW FLINTOFF

应该出现在employee18.txt和

    1905001 SACHIN TICKHULE
    1905002 VIRAT KOHLI

应该出现在 employee19.txt 中。但是,当我 运行 编码时,它只将 1801235 ANDREW FLINTOFF 和 1905002 VIRAT KOHLI 添加到上述文件中。这是我到目前为止所做的

package separatingEmployees;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;




public class SeparateEmployees {

public static void main(String[] args)  {
    
    try {
        File file = new File("employees.txt");
        Scanner s= new Scanner(file);
        
        while(s.hasNext()){
            String st=s.nextLine();
            if(st.startsWith("18")){//LOOK FOR LINE START WITH 18 
                 try {
            BufferedWriter w=new BufferedWriter(new FileWriter("employees18.txt"));
            w.write(st);
               // System.out.println("file created");
            w.close();
             } catch (IOException ex) {
               System.out.println("cant write");
             }
               System.out.println(st);
            }if(st.startsWith("19")){//LOOK FOR LINE START WITH 19
                 try {
            BufferedWriter w=new BufferedWriter(new FileWriter("employees19.txt"));
            w.write(st);
               // System.out.println("file created");
            w.close();
             } catch (IOException ex) {
               System.out.println("cant write");
             }
               System.out.println(st);
            }if(st.startsWith("20")){ // LOOK FOR LINE STARTS WITH 20 AND SEND TO EMPLOYEES20.TXT
                 try {
            BufferedWriter w=new BufferedWriter(new FileWriter("employees20.txt"));
            w.write(st);
               // System.out.println("file created");
            w.close();
             } catch (IOException ex) {
               System.out.println("cant write");
             }
               System.out.println(st);
            }
            }
            } catch (FileNotFoundException ex) {
             System.out.println("catch block"); 
        
            }finally {
            System.out.println("finally block");
            //write();
            
            
        }
        }
   


    }


enter code here

每次检查下一行时,FileWriter 都会覆盖文件内容。

你想要做的是追加行:

//Here true is to append the content to file
BufferedWriter w=new BufferedWriter(new FileWriter("employees18.txt", true));
w.newLine();
w.write(st);

问题是您正在为要添加的每一行创建文件(这似乎有点低效)。一种方法是在附加模式下打开文件,以便将行添加到现有内容 (new FileWriter(filename, true))。但我认为更好的方法是在迭代之前打开要写入的文件并在最后关闭它们:

// Open the files before iterating:
BufferedWriter w19=new BufferedWriter(new FileWriter("employees19.txt"));
BufferedWriter w18=new BufferedWriter(new FileWriter("employees18.txt"));
BufferedWriter w20=new BufferedWriter(new FileWriter("employees20.txt"));
while(s.hasNext()){
    String st=s.nextLine();
    if(st.startsWith("18")){//LOOK FOR LINE START WITH 18 
        w18.write(st);
        System.out.println(st);
    } if (st.startsWith("19")){//LOOK FOR LINE START WITH 19
        w19.write(st);
        System.out.println(st);
    }if(st.startsWith("20")){ // LOOK FOR LINE STARTS WITH 20 AND SEND TO EMPLOYEES20.TXT
        w20.write(st);
    }

处理后关闭创建的文件:

w20.close()
w19.close()
w18.close()

在您认为更好的地方添加异常处理。