Java - 无法完成写入文本文件

标签 java bufferedreader bufferedwriter

我需要处理一个大文本文件(大约 600 MB)以便正确格式化它,并将格式化的输出写入新的文本文件。问题在于,将内容写入新文件时会在大约 6.2 MB 处停止。这是代码:

/* Analysis of the text in fileName to see if the lines are in the correct format 
     * (Theme\tDate\tTitle\tDescription). If there are lines that are in the incorrect format,
     * the method corrects them.
     */
    public static void cleanTextFile(String fileName, String destFile) throws IOException {
        OutputStreamWriter writer = null;
        BufferedReader reader = null;

        try {
            writer = new OutputStreamWriter(new FileOutputStream(destFile), "UTF8");
        } catch (IOException e) {
            System.out.println("Could not open or create the file " + destFile);
        }

        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("The file " + fileName + " doesn't exist in the folder.");
        }

        String line;
        String[] splitLine;
        StringBuilder stringBuilder = new StringBuilder("");

        while ((line = reader.readLine()) != null) {
            splitLine = line.split("\t");
            stringBuilder.append(line);

            /* If the String array resulting of the split operation doesn't have size 4,
             * then it means that there are elements of the news item missing in the line
             */
            while (splitLine.length != 4) {
                line = reader.readLine();
                stringBuilder.append(line);

                splitLine = stringBuilder.toString().split("\t");
            }
            stringBuilder.append("\n");
            writer.write(stringBuilder.toString());
            stringBuilder = new StringBuilder("");

            writer.flush();
        }

        writer.close();
        reader.close();

    }

我已经在寻找答案,但问题通常与编写器未关闭或缺少 flush() 方法有关。因此,我认为问题出在 BufferedReader 上。我错过了什么?

最佳答案

看看这个循环:

while (splitLine.length != 4) {
    line = reader.readLine();
    stringBuilder.append(line);

    splitLine = stringBuilder.toString().split("\t");
}

如果您在 splitLine 中得到超过 5 个项目,您将永远继续读取数据...您甚至不会注意到已经到达文件末尾,因为您只需继续将 null 附加到 StringBuilder 即可。我不知道这是否是正在发生的事情(我们不知道你的数据是什么样的),但它肯定是可行的,你应该警惕它。

(您还应该使用 try/finally block 来关闭资源,但这是一个单独的问题。)

关于Java - 无法完成写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12825693/

相关文章:

java - bufferedwriter close() 和flush() 方法的内存效应是什么?

java - 将实时数据写入文件

java - BufferedWriter自动刷新

java - 如何在 jflex 中创建 lambda 符号?

java - 如何修复自定义双链表上的无限循环?

java - 如何更快地阅读 BufferedReader

java - 从 .txt 文件读取、计算和写入数据

java - 将媒体项添加到 MediaBrowserService

java - 在链表java中切换节点时出现问题

java - 如何使用 Java 从文本文件中读取奇数行?