【问题标题】:Bufferedreader fails to read whole fileBufferedreader 无法读取整个文件
【发布时间】:2015-10-01 14:36:36
【问题描述】:

所以我得到了这个奇怪的错误。 我想做的是:

  1. 打开一个 zip 文件,然后解压缩其中的内容。
  2. 遍历文件的内容并修复文件的空白以及引用文件的位置,这些文件位于多个 XML 中。
  3. Zip 内容。

第 1 点和第 3 点我已经介绍过了 - 但是当我尝试读取所有 xml 文件时,出现了问题。 我有我想在数组列表中读取的所有 xml 文件(大约 30 个文件),然后我想像这样读取它们(注意:此方法在迭代 30 个 xml 文件的 for 循环中调用):

private static void readXMLFile(String path) {
    // System.out.println("Deleting");
    Iterator<String> iter = listToRecreate.iterator();
    while (iter.hasNext()) {
        String str = iter.next();
        if (listToRecreate.size() > 0) {
            iter.remove();
        }
    }

    File mFile = new File(path);
    // System.out.println(path);
    // System.out.println("Beginning to read");

    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(mFile.getPath()));
        for (String sCurr = ""; (sCurr = br.readLine()) != null;) {
            // System.out.println(sCurr);
            listToRecreate.add(sCurr);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    editFile(path);
}

然后发生的情况是只有一半的文件内容被读取。奇怪的是,如果我取出一个 xml 文件并尝试读取它,它就会正常工作。这里可能是什么错误? 在此先感谢:)

编辑: 我如何写入文件

private static void editFile(String path) {
    // System.out.println(path);
    try {
        PrintWriter writer = new PrintWriter(new File(path));
        for (String str : listToRecreate) {
            // System.out.println(str);
            int j = 0;
            for (String tag : listOfTagNames) {
                str = replaceTag(str, listOfTagNames.get(j));
                j++;
            }
            // System.out.println(str);
            writer.println(str);
        }
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

编辑 2: 我刚刚意识到这可能是我的解压缩方法惹恼了我。 更新:此代码现在有效。感谢http://www.avajava.com/tutorials/lessons/how-do-i-unzip-the-contents-of-a-zip-file.html

public static void unzipFile(String filePath, String destPath) {
    try {
        ZipFile zipFile = new ZipFile(filePath);
        Enumeration<?> enu = zipFile.entries();
        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();

            String name = zipEntry.getName();
            long size = zipEntry.getSize();
            long compressedSize = zipEntry.getCompressedSize();
            //System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n",
                   // name, size, compressedSize);

            File file = new File(destPath + File.separator + name);
            if (name.endsWith("/")) {
                file.mkdirs();
                continue;
            }

            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }

            InputStream is = zipFile.getInputStream(zipEntry);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = is.read(bytes)) >= 0) {
                fos.write(bytes, 0, length);
            }
            is.close();
            fos.close();

        }
        zipFile.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • @fge 刚刚检查过 - 是的,我愿意
  • 有关信息,要删除列表中的所有条目,只需致电listToRecreate.clear()。你从哪里得到文件路径?您是从 zip 中读取文件,然后将其复制到磁盘上的实际文件中吗?
  • 好的,谢谢@Lolo :) 我正在复制一个本地 zip 文件“something.zip”并创建一个编辑后的副本“something_corrected.zip”
  • 所以您的代码是直接读取zip文件,而不是提取里面的xml文件然后读取它们?如果是这样,那不是正确的方法。请在您的问题中澄清这一点。
  • 您的示例中没有任何内容表明文件可能被部分读取。对我来说唯一的危险信号是我们看不到listToRecreate 的声明位置——也许你有某种竞争条件。你能提供一个更完整的例子而不仅仅是这个方法吗?另外,您能否告诉我们如何您确定文件没有被完全读取?

标签: java xml file bufferedreader


【解决方案1】:

对于那些将来可能会遇到同样问题的人。 我发现是我的解压缩方法没有完全解压缩我的文件,并且我的读/写方法已检出。 我已经更新了代码,所以 unzip 方法有效。

感谢所有试图帮助我的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 2016-10-15
    • 2014-01-28
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2020-03-01
    • 2014-03-08
    相关资源
    最近更新 更多