Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在读取文本文件时跳过一定数量的行-BufferedReader Java_Java_Io_Bufferedreader - Fatal编程技术网

在读取文本文件时跳过一定数量的行-BufferedReader Java

在读取文本文件时跳过一定数量的行-BufferedReader Java,java,io,bufferedreader,Java,Io,Bufferedreader,我有几个文件在一个目录中,我打算跳过一定数量的行。获取要跳过的行的唯一方法是获取字符长度为1的行的第一个匹配项。唯一可用的信息是该行出现在第60行之前的任何位置。因此,我编写了以下方法来尝试跳过字符前面的行。但我最终得到的文件与原始文件相同: public static void editSplitFiles(File sourceDir) { FilenameFilter only = new OnlyExt("RPT"); log.debug("Editing Split F

我有几个文件在一个目录中,我打算跳过一定数量的行。获取要跳过的行的唯一方法是获取字符长度为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());
    }
}
publicstaticvoideditsplitfiles(filesourcedir){
FilenameFilter only=newonlyext(“RPT”);
log.debug(“编辑分割文件…”);
String[]filenames=sourceDir.list(仅限);
试一试{
for(int k=0;k
我不确定我做错了什么,但看起来在我跳过之前文件已经被写入了。我如何做到这一点

编辑 这些文件如下所示:

我通过在地图中存储每个文件的要跳过的行值来解决这个问题。然后遍历映射并跳过每个文件的行数。我想应该有个简单的方法

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;
    }
publicstaticmap getSplitMap(文件sourceDir){
FilenameFilter only=newonlyext(“RPT”);
log.debug(“获取分割映射…”);
Map Map=newhashmap();
String[]filenames=sourceDir.list(仅限);
试一试{
for(int k=0;k
然后跳过每个文件的行:

 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());
        }
    }
publicstaticvoidsplitfile(文件sourceDir){
Map Map=getSplitMap(sourceDir);
试一试{
对于(Map.Entry:Map.entrySet()){
FileInputStream fs=newFileInputStream(sourceDir.getAbsolutePath()+System.getProperty(“file.separator”)+entry.getKey());
BufferedReader br=新的BufferedReader(新的InputStreamReader(fs));
FileOutputStream fos=新的FileOutputStream(sourceDir.getAbsolutePath()+System.getProperty(“file.separator”)+entry.getKey()+“.LST”);
BufferedWriter bw=新的BufferedWriter(新的OutputStreamWriter(fos));
对于(int i=1;i
您是否有一个文件示例,以及预期的输出是什么?有点不清楚,您是在寻找大小为1的行(就像您的代码似乎是这样),还是字符
1
?您正在评估
strLine.length()==1
,但不管您要写什么
bw.write(strLine)
。只要
splitLine<60
您就可以读取额外的一行。输出应该是第二行直到第60行,然后是您读取的文件中的每一行。还是我遗漏了什么?@LuCio我试图避免在其他大于的行中条件
strLine.length()==1的情况60@Arnaud第一次出现的长度为1的行是我应该跳到的地方。