简体   繁体   English

如何将txt文件中的select数据打印成不同的txt文件java

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

I have a txt file called employees.txt which contain employee id and names.我有一个名为 employees.txt 的 txt 文件,其中包含员工 ID 和姓名。 Their id starts with either 18,19 or 20. And have to look within the file if the line starts with 18 it needs to go to another txt file employee18.txt if starts with 19 then go to employee19.txt and the same way if it starts with 20 goes to employee20.txt.I share my code for what I have done so far.他们的 id 以 18,19 或 20 开头。如果行以 18 开头,则必须在文件中查找它需要 go 到另一个 txt 文件 employee18.txt 如果以 19 开头,则 go 到 employee19.txt 以相同的方式如果它以 20 开头转到 employee20.txt。我分享我到目前为止所做的代码。 I have been able to read the file and also by using Scanner looks for line which starts with 18,19or 20. The problem is when I run the code it shows all the values to the console but adds only one last value within the employee.txt file to employee18.txt or employee19.txt and employee20.txt.What I am doing wrong I don't know entries in employees.txt are我已经能够读取该文件,还可以通过使用扫描仪查找以 18,19 或 20 开头的行。问题是当我运行代码时,它会向控制台显示所有值,但只在员工中添加最后一个值。 txt 文件到 employee18.txt 或 employee19.txt 和 employee20.txt。我做错了什么我不知道 employees.txt 中的条目是

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

After running code运行代码后

    1801234 IMRAN KHAN
    1801235 ANDREW FLINTOFF

should appear in employee18.txt and应该出现在 employee18.txt 和

    1905001 SACHIN TICKHULE
    1905002 VIRAT KOHLI

should appear in employee19.txt.However, when I run code it only adds 1801235 ANDREW FLINTOFF and 1905002 VIRAT KOHLI to the said files.应该出现在 employee19.txt 中。但是,当我运行代码时,它只会将 1801235 ANDREW FLINTOFF 和 1905002 VIRAT KOHLI 添加到上述文件中。 Here is what I have done so far这是我到目前为止所做的

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

The problem is that you are creating the file for every new line you want to add (which seems a bit inefficient).问题是您正在为要添加的每一行创建文件(这似乎有点低效)。 One approach is to open the file on append mode so lines will be add to the existing content (new FileWriter(filename, true)).一种方法是在 append 模式下打开文件,以便将行添加到现有内容 (new FileWriter(filename, true))。 But I think a better approach will be to open the files to write before iterating and closing them at the end:但我认为更好的方法是在迭代之前打开要写入的文件并在最后关闭它们:

// 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);
    }

after processing close the files created:处理后关闭创建的文件:

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

Add the Exception treatment where you consider it better.在您认为更好的地方添加异常处理。

Every time you check next line, FileWriter overwrites file content.每次检查下一行时, FileWriter都会覆盖文件内容。

What you want to do, is to append lines:你想做的是 append 行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM