2015-07-13 53 views
2

所以我得到了这个奇怪的错误。 我想要做的是这样的:Bufferedreader无法读取整个文件

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

你使用Java 7+吗? – fge

+0

@fge刚刚检查过 - 是的,我做 –

+0

有关信息,要删除列表中的所有条目,只需调用'listToRecreate.clear()'。你从哪里得到文件路径?你是否从zip中读取文件,然后将其复制到磁盘上的实际文件中? – Lolo

回答

1

对于那些谁在将来可能会面临同样的问题找到。 我发现这是我的解压缩方法,没有完全解压我的文件,并且我的读/写方法被检出。 我已经更新了代码,以便解压缩方法起作用。

感谢大家试图帮助我!