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

我在一个目录中有几个文件,我打算跳过一定数量的行。获取要跳过的行的唯一方法是获取字符长度为 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());

    }

}

我不确定我做错了什么,但看起来文件在我跳过之前正在写入。我该如何实现?

http://img1.mukewang.com/617a3d550001a2cd02520519.jpg

慕码人8056858
浏览 517回答 1
1回答

炎炎设计

我已经通过在 Map 中存储要跳过每个文件的值的行来解决这个问题。然后遍历地图并跳过每个文件的行数。我认为应该有一个简单的方法。public static Map getSplitMap(File sourceDir) {&nbsp; &nbsp; &nbsp; &nbsp; FilenameFilter only = new OnlyExt("RPT");&nbsp; &nbsp; &nbsp; &nbsp; log.debug("Getting split Map........");&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Integer> map = new HashMap();&nbsp; &nbsp; &nbsp; &nbsp; String[] filenames = sourceDir.list(only);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int k = 0; k < filenames.length; k++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileInputStream fs = new FileInputStream(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + filenames[k]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader br = new BufferedReader(new InputStreamReader(fs));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String strLine;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int num = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int splitLine = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((strLine = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strLine.length() == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; splitLine = num;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (splitLine < 60) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(filenames[k], splitLine);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fs.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception asd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.debug(asd.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return map;&nbsp; &nbsp; }然后跳过每个文件的行:&nbsp;public static void splitFile(File sourceDir) {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Integer> map = getSplitMap(sourceDir);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Map.Entry<String, Integer> entry : map.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileInputStream fs = new FileInputStream(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + entry.getKey());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader br = new BufferedReader(new InputStreamReader(fs));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileOutputStream fos = new FileOutputStream(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + entry.getKey() + ".LST");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < entry.getValue(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String strLine;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((strLine = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bw.write(strLine);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bw.newLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bw.flush();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fs.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fos.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bw.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File files = new File(sourceDir.getAbsolutePath() + System.getProperty("file.separator") + entry.getKey());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; files.delete();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception asd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.debug(asd.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java