我在目录中有几个文件,打算跳过某些行。使行跳过的唯一方法是使字符长度为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());
}
}
我不确定自己在做什么错,但是在跳过之前,似乎文件已被写入。我该怎么做?
答案 0 :(得分:0)
我已经解决了这一点,方法是存储行以跳过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());
}
}