java - Bufferedreader 无法读取整个文件

标签 java xml file bufferedreader

所以我得到了这个奇怪的错误。 我正在尝试做的是:

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

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

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();
    }
}

最佳答案

对于那些将来可能会遇到同样问题的人。 我发现是我的解压方法没有完全解压我的文件,我的读/写方法被检查出来了。 我已经更新了代码,因此解压缩方法可以正常工作。

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

关于java - Bufferedreader 无法读取整个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31381527/

相关文章:

java - JFrame 上没有显示任何内容

java - 在应用程序服务器上提供静态内容

java - 以高效的方式获取所有可能的 JavaPairRDD 键组合

xml - 为什么固定宽度的文件格式仍在使用?

android - 使用默认 xml 属性复制 Android 首选项屏幕样式

linux - 为指定文件夹中存在的每个文件将特定文本/字符串写入文本文件

java - 获取 Scala 类型的 Java 反射表示

java - 列出所有内容为空的 td 节点

ios - 尝试使用 swift 2.0 从 Document 目录中读取 plist 文件

python - python 中的正则表达式从文件读取文本