Java Bufferedreader无法读取整个文件
所以我犯了一个奇怪的错误。 我想做的是:Java Bufferedreader无法读取整个文件,java,xml,file,bufferedreader,Java,Xml,File,Bufferedreader,所以我犯了一个奇怪的错误。 我想做的是: 打开一个zip文件,然后解压缩内容 迭代文件的内容,并修复文件的空白以及文件被引用的位置,这些位置在几个XML中 压缩内容 我已经介绍了第1点和第3点——但是,当我尝试读取所有xml文件时,出现了一些问题。 我在arraylist中有我想要读取的所有xml文件(大约30个文件),然后我想要像这样读取它们(注意:这个方法在一个for循环中调用,该循环迭代30个xml文件): 编辑2: 我刚刚意识到可能是我的解压方法让我感到困惑。 更新:这段代码现在可以工作
publicstaticvoidunzipfile(stringfilepath,stringdestpath){
试一试{
ZipFile ZipFile=新ZipFile(文件路径);
枚举enu=zipFile.entries();
而(enu.hasMoreElements()){
ZipEntry ZipEntry=(ZipEntry)enu.nextElement();
String name=zipEntry.getName();
long size=zipEntry.getSize();
long compressedSize=zipEntry.getCompressedSize();
//System.out.printf(“名称:%-20s |大小:%6d |压缩大小:%6d\n”,
//名称、尺寸、压缩尺寸);
File File=新文件(destPath+File.separator+name);
if(name.endsWith(“/”){
mkdirs()文件;
继续;
}
File parent=File.getParentFile();
如果(父项!=null){
parent.mkdirs();
}
InputStream is=zipFile.getInputStream(zipEntry);
FileOutputStream fos=新的FileOutputStream(文件);
字节[]字节=新字节[1024];
整数长度;
而((长度=is.read(字节))>=0){
fos.写入(字节,0,长度);
}
is.close();
fos.close();
}
zipFile.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
适用于那些将来可能会遇到同样问题的人。
我发现是我的解压方法没有完全解压我的文件,并且我的读/写方法被签出。
我已经更新了代码,因此解压方法可以工作
谢谢大家帮助我 @fge刚刚选中-yes do有关信息,要删除列表中的所有条目,只需调用
listToRecreate.clear()
。您从哪里获取文件路径?您是从zip读取文件,然后将其复制到磁盘上的实际文件中吗?好的,谢谢@Lolo:)我正在复制一个本地zip文件“something.zip”,并创建一个编辑副本“something\u corrected.zip”,因此您的代码直接读取zip文件,而不是提取内部的xml文件然后读取它们?如果是这样,那就不是正确的方法。请在您的问题中澄清这一点。您的示例中没有任何内容表明文件可能被部分读取。对我来说唯一的危险是我们看不到listToRecreate
是在哪里声明的——也许你有某种竞争条件。你能提供一个更完整的例子而不仅仅是这个方法吗?另外,您能告诉我们您是如何确定文件未被完全读取的吗?
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);
}
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();
}
}
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();
}
}