[英]how to select data in txt file and print into different txt file java
我有一个名为 employees.txt 的 txt 文件,其中包含员工 ID 和姓名。 他们的 id 以 18,19 或 20 开头。如果行以 18 开头,则必须在文件中查找它需要 go 到另一个 txt 文件 employee18.txt 如果以 19 开头,则 go 到 employee19.txt 以相同的方式如果它以 20 开头转到 employee20.txt。我分享我到目前为止所做的代码。 我已经能够读取该文件,还可以通过使用扫描仪查找以 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
运行代码后
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
问题是您正在为要添加的每一行创建文件(这似乎有点低效)。 一种方法是在 append 模式下打开文件,以便将行添加到现有内容 (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()
在您认为更好的地方添加异常处理。
每次检查下一行时, FileWriter
都会覆盖文件内容。
你想做的是 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.