我正在尝试制作一个程序,该程序从 database.txt 中读取一行并将奇数/偶数行写入文件 1.txt 和 2.txt。Fe database.txt 的第 1(奇)行转到 1.txt,database.txt 的第 2(偶)行转到 2.txt
这是我到目前为止得到的代码:
import java.io.*;
import java.util.Scanner;
public class Main {
public Main(){
op(null);
}
public void op(String args[]){
try{
FileReader fr = new FileReader("database.txt");
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
Scanner scan = null;
int ln = 1;
String even = "2txt";
String odd = "1.txt";
while ((line=reader.readLine())!=null){
scan = new Scanner(line);
if(ln%2==0){
wtf(even, line);
}else{
wtf(odd, line);
}
ln++;
line=reader.readLine();
}
reader.close();
}
catch (FileNotFoundException e){
System.out.println("File not found");
}
catch (IOException e) {
System.out.println("Impossibru to read");
}
}
public void wtf(String filename, String ltw){
try
{
FileReader fr = new FileReader(filename);
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
FileWriter writer = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(writer);
while(line==null){
bw.write(ltw);
bw.newLine();
}
bw.close();
}
catch ( IOException e)
{
}
}
}
目前它处于无限循环中,仅读取第二行并将其发送到 1.txt