1 回答
TA贡献1808条经验 获得超4个赞
我已经通过在 Map 中存储要跳过每个文件的值的行来解决这个问题。然后遍历地图并跳过每个文件的行数。我认为应该有一个简单的方法。
public static Map getSplitMap(File sourceDir) {
FilenameFilter only = new OnlyExt("RPT");
log.debug("Getting split Map........");
Map<String, Integer> map = new HashMap();
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));
String strLine;
int num = 0;
int splitLine = 0;
while ((strLine = br.readLine()) != null) {
num++;
if (strLine.length() == 1) {
splitLine = num;
}
if (splitLine < 60) {
map.put(filenames[k], splitLine);
}
}
fs.close();
br.close();
}
} catch (Exception asd) {
log.debug(asd.getMessage());
}
return map;
}
然后跳过每个文件的行:
public static void splitFile(File sourceDir) {
Map<String, Integer> map = getSplitMap(sourceDir);
try {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
FileInputStream fs = new FileInputStream(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + entry.getKey());
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileOutputStream fos = new FileOutputStream(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + entry.getKey() + ".LST");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i = 1; i < entry.getValue(); i++) {
br.readLine();
}
String strLine;
while ((strLine = br.readLine()) != null) {
bw.write(strLine);
bw.newLine();
bw.flush();
}
fs.close();
br.close();
fos.close();
bw.close();
File files = new File(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + entry.getKey());
files.delete();
}
} catch (Exception asd) {
log.debug(asd.getMessage());
}
}
添加回答
举报