无法将所有列表写入文件

时间:2015-03-13 05:21:42

标签: java

我有一个来自文本文件的某些行的ArrayList列表。我试图在文本文件中找到这些行,如果我发现它我想将它写入另一个文本文件并从原始文件中删除它。

我为此编写了一个代码,它正在运行但不适用于整个列表,有时需要一行,有时需要更多。并给我这个消息:

  

1 R101 100850 0   捕获异常:java.io.IOException:Stream closed

static void moveLines(ArrayList posList, int topic) {

    //=======================To read lines=======
    File inputFile = new File("U:\\Research\\Projects\\sef\\enhancfeaturtm\\TestData\\topic\\" + "Test" + topic + ".txt");
    File outputFile = new File("U:\\Research\\Projects\\sef\\enhancfeaturtm\\TestData\\topic\\" + "Training" + topic + ".txt");

    try {
        FileReader fr = new FileReader(inputFile);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fr1 = new FileWriter(outputFile);
        BufferedWriter writer = new BufferedWriter(fr1);

        String line;
        int count = 1;
        int z = 1;
        while ((line = br.readLine()) != null) {
          //  System.out.println(z++ + ": ");
            String subLine = line.substring(5, line.length() - 2);
          //  System.out.println(subLine);

            if (posList.contains(subLine)) {
                System.out.println(count++ + " " + line);
                fr1.write(line);
                fr1.write("\n");
                fr1.flush();
               fr.close();
                removeLineFromFile(inputFile.getAbsolutePath(), line);

            }
        }
        br.close();
        fr1.close();
        writer.close();
    } catch (Exception e) {
        System.out.println("Exception caught : " + e);
    }
}

static void removeLineFromFile(String file, String lineToRemove) {

    try {

        File inFile = new File(file);

        //Construct the new file that will later be renamed to the original filename. 
        File tempFile = new File(inFile.getAbsolutePath() + ".tmp");

        BufferedReader br = new BufferedReader(new FileReader(file));
        PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

        String line = null;

        //Read from the original file and write to the new 
        //unless content matches data to be removed.
        while ((line = br.readLine()) != null) {

            if (!line.trim().equals(lineToRemove)) {

                pw.println(line);
                pw.flush();
            }
        }
        pw.close();
        br.close();

        //Delete the original file
        if (!inFile.delete()) {
            System.out.println("Could not delete file");
            return;
        }

        //Rename the new file to the filename the original file had.
        if (!tempFile.renameTo(inFile)) {
            System.out.println("Could not rename file");
        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

String subLine = line.substring(5, line.length() - 2);

你很难从索引5中获取子字符串。

当线长小于5时会发生什么?检查线的长度是否小于5,然后继续。

为什么要追上'异常'呢?尝试捕获较低级别的异常,如ArrayIndexOutOfBoundsException等。

答案 1 :(得分:0)

谢谢大家的帮助。

我找出了问题,这是因为我删除了文件并从临时文件中再次创建它。在这种情况下,我丢失了指向该文件的指针。

如果有人感兴趣,这是我修复后的代码。

 static void moveLines(ArrayList posList, int topic) {

    //=======================To read lines=======
    File inputFile = new File("U:\\Research\\Projects\\sef\\enhancfeaturtm\\Data1\\topic\\" + "Test" + topic + ".txt");
    File outputFile = new File("U:\\Research\\Projects\\sef\\enhancfeaturtm\\Data1\\topic\\" + "Training" + topic + ".txt");

    try {
        FileReader fileReader = new FileReader(inputFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        FileWriter fileWriter = new FileWriter(outputFile);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String subLine = line.substring(5, line.length() - 2);

            if (posList.contains(subLine)) {
                System.out.println(count++ + " " + line);
                bufferedReader.close();
                fileReader.close();


                bufferedWriter.write(line+"\n");
                bufferedWriter.flush();


                bufferedReader = removeLineFromFile(inputFile.getAbsolutePath(), line);

            }
        }
        bufferedWriter.close();
        fileWriter.close();

        bufferedReader.close();
        fileReader.close();
    } catch (Exception e) {
        System.out.println("Exception caught : " + e);
    }
}

static BufferedReader removeLineFromFile(String file, String lineToRemove) {

    BufferedReader bufferedReader = null;

    try {

        File inFile = new File(file);

        //Construct the new file that will later be renamed to the original filename. 
        File tempFile = new File(inFile.getAbsolutePath() + ".tmp");

        BufferedReader br = new BufferedReader(new FileReader(file));
        BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));

        String line = null;

        //Read from the original file and write to the new 
        //unless content matches data to be removed.
        while ((line = br.readLine()) != null) {

            if (!line.trim().equals(lineToRemove)) {

                bw.write(line+"\n");
                bw.flush();
            }
        }
        bw.close();
        br.close();

        //Delete the original file
        if (!inFile.delete()) {
            System.out.println("Could not delete file");
            return null;
        }

        //Rename the new file to the filename the original file had.
        if (!tempFile.renameTo(inFile)) {
            System.out.println("Could not rename file");
        }

        FileReader fileReader = new FileReader(inFile);
        bufferedReader = new BufferedReader(fileReader);

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return bufferedReader;
}