我试图将文本文件拆分到“END OF CUSTOMER STATEMENT”行的目录中,然后将结果文件存储到临时目录中。拆分只发生在第一个文件而另一个文件被忽略,我的代码有什么问题。我原以为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);
}
}
}
}
答案 0 :(得分:3)
我认为你应该首先做这件事(可能有更多的错误)
int count = 0;
for (int m = 0; m < filenames.length; m++) {
...
UPDATE 此外,删除您的计数++并在每次创建文件后放置它
FileOutputStream fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
count++;
然后它将按预期工作
答案 1 :(得分:3)
我认为,因为目标文件没有任何区别于它们(它们都被命名为statementX.RPT
) - 最后一个文件实际上是你输出中的文件 - 但这只是猜测
尝试将输出文件更改为名为"statement." + m + "." + count ".RPT"
,这样您将拥有唯一的输出文件。
另外,请注意以下评论:
File
课程时,listFiles
API更有用(在我看来) - 您可以从每个文件中查询getName
和getPath
。< / LI>
FileInputStream fs = new FileInputStream("D:/statements/" + filenames[m]);
- 如果您使用了从listFiles
获得的结果,则可以将其替换为FileInputStream fs = new FileInputStream(files[m]);
- 无需对路径进行硬编码。答案 2 :(得分:1)
您应该修改您的代码。否则,代码不会创建两个输出文件,而是创建三个输出文件。这是正确的代码
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 = "";
int count = 0;
for (int m = 0; m < filenames.length; m++) {
try {
FileInputStream fs = new FileInputStream("D:/statements/" + filenames[m]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileOutputStream fos = null;
BufferedWriter bw = null;
while ((lines = br.readLine()) != null) {
String mine = lines.trim();
if (mine.startsWith("END OF CUSTOMER STATEMENT")) {
if(bw!=null)
{
bw.close();
}
count++;
continue;
}
if (mine.isEmpty()) {
continue;
} else {
if(bw==null)
{
fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
bw = new BufferedWriter(new OutputStreamWriter(fos));
}
bw.write(lines);
bw.newLine();
bw.flush();
}
}
fos.close();
fs.close();
br.close();
bw.close();
} catch (Exception ag) {
System.out.println(ag);
}
}
}
}