我正在尝试沿“客户声明结束”行拆分目录中的文本文件,并将结果文件存储到临时目录中。拆分仅针对第一个文件发生,而忽略另一个文件,我的代码有什么问题。我期待 for 循环会吞没目录中的所有文件?这是我的代码。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* @author Administrator
*/
public class SplitFiles {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
File f = new File("D:/statements/");
String[] filenames = f.list();
File[] texts = f.listFiles();
String lines = "";
for (int m = 0; m < filenames.length; m++) {
try {
int count = 0;
FileInputStream fs = new FileInputStream("D:/statements/" + filenames[m]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileOutputStream fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
while ((lines = br.readLine()) != null) {
String mine = lines.trim();
if (mine.startsWith("END OF CUSTOMER STATEMENT")) {
bw.close();
count++;
fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
bw = new BufferedWriter(new OutputStreamWriter(fos));
continue;
}
if (mine.isEmpty()) {
continue;
} else {
bw.write(lines);
bw.newLine();
bw.flush();
}
}
fos.close();
fs.close();
br.close();
bw.close();
} catch (Exception ag) {
System.out.println(ag);
}
}
}
}