我在一个目录中有几个文件,我打算跳过一定数量的行。获取要跳过的行的唯一方法是获取字符长度为 1 的行的第一次出现。唯一可用的信息是该行出现在行号 60 之前的任何位置。因此我编写了以下方法来尝试跳过字符之前的行。但我最终得到与原始文件相同的文件:
public static void editSplitFiles(File sourceDir) {
FilenameFilter only = new OnlyExt("RPT");
log.debug("Editing Split Files........");
String[] filenames = sourceDir.list(only);
try {
for (int k = 0; k < filenames.length; k++) {
FileInputStream fs = new FileInputStream(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + filenames[k]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileOutputStream fos = new FileOutputStream(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + filenames[k] + ".LST");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
String strLine;
int num = 0;
int splitLine = 0;
while ((strLine = br.readLine()) != null) {
num++;
if (strLine.length() == 1) {
splitLine = num;
}
bw.write(strLine);
bw.newLine();
bw.flush();
}
if (splitLine < 60) {
log.debug("File Name" + filenames[k] + "Line Number - " + splitLine);//This gives me the correct line number where the character is for each file.
br.readLine();
}
fs.close();
br.close();
fos.close();
bw.close();
}
} catch (Exception asd) {
log.debug(asd.getMessage());
}
}
我不确定我做错了什么,但看起来文件在我跳过之前正在写入。我该如何实现?
炎炎设计
相关分类