我正在尝试拆分文本文件并根据 >>>> 分隔符创建单独的文件,然后根据 java 中的标头解析每个文件。请帮助根据 java 中的标头解析文件。这是我尝试过的代码,但有些文件是空的,并且整个预期数据没有写入每个单独的文件。
公共类 MainParsing {
public static void main(String[] args) {
File inf = new File("C:\\D_Drive\\FTPFiles\\Test_MML_data.txt");
//File outf = new File("C:\\D_Drive\\FTPFiles\\output.txt");
File outf =null;
FileReader ins = null;
FileWriter outs = null;
int i=0;
try {
ins = new FileReader(inf);
outs = new FileWriter(outf);
int ch;
boolean flagfile=true;
while ((ch = ins.read()) != -1)
{
if(ch == '>')
{
if(flagfile)
{
outf = new File("C:\\D_Drive\\FTPFiles\\output"+i+".txt");
outs = new FileWriter(outf);
flagfile=false;
i++;
System.out.println("File created ...");
}
continue;
}
else
{
outs.write(ch);
flagfile=true;
}
}
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
ins.close();
outs.close();
} catch (IOException e) {
}
}
}
}