gpt4 book ai didi

java - 文件已创建但仍抛出异常

转载 作者:行者123 更新时间:2023-11-30 06:57:09 25 4
gpt4 key购买 nike

我正在尝试创建文件并将其添加到该目录。下面是我的代码,我能够创建文件并向其中添加文件,但最后它抛出了一个异常。我的代码:

Unzip_Main.java

import java.io.File;

public class UnZip_Main {
public static void main(String[] args) {

String zipFilePath = "D:\\News\\Zip\\";
String destDirectory = "D:\\News\\Zip\\Result\\";
new File(destDirectory).mkdir();
UnZip unzipper = new UnZip();
File dir = new File(zipFilePath);
File[] files = dir.listFiles();
if (null != files) {
for (int fileIntList = 0; fileIntList < files.length; fileIntList++) {
String ss = files[fileIntList].toString();
if (null != ss && ss.length() > 0) {
System.out.println("unzip path is ");
try {
System.out.println("dest directry is " + destDirectory);
unzipper.unzip(zipFilePath + ss.substring(ss.lastIndexOf("\\") + 1, ss.length()),
destDirectory);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
}

解压.java

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnZip {
private static final int BUFFER_SIZE = 4096;

public void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath, zipFilePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}

private void extractFile(ZipInputStream zipIn, String filePath, String zipFilePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath, true));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
File newName = new File(filePath);
String str = zipFilePath.substring(zipFilePath.lastIndexOf("\\") + 1, zipFilePath.lastIndexOf("."));
File zipPath = new File(filePath);
zipPath.mkdir();
File oldName = new File(zipPath.getParent() + "\\" + str + ".xml");
if (oldName.exists()) {
oldName.delete();
}
System.out.println("new name is " + newName + "and old name is " + oldName);
if (newName.renameTo(oldName)) {
System.out.println("Renamed");
} else {
System.out.println("Not Renamed");
}

}

}

我的输出:

new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If16c0c30613111e5850ddea403ecf0ba.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If83120c05dd311e599a896be76e2f024.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If8915610629d11e5b64da6abc0693b3d.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If93445c0661f11e5839c9a236dd16599.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If9bd10a061f411e5b445d6756f17230b.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\Ife581970612c11e5b64da6abc0693b3d.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\Ifed1c1f05f9a11e5bc448d3219668f6c.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\Iff5aa9905d4011e5bb1df062954439f5.xml
Renamed

末尾异常:

java.io.FileNotFoundException: D:\News\Zip\Result (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at UnZip.unzip(UnZip.java:17)
at UnZip_Main.main(UnZip_Main.java:19)

文件是在正确的文件夹中创建的,所有文件都已创建,但仍然出现此异常,无法知道我哪里出错了以及如何修复它。

观察到的另一件事是,如果我将 String destDirectory = "D:\\News\\Zip\\Result\\"; 更改为 String destDirectory = "D:\\News\\Zip\\";,如果 Zip 路径中有任何文件夹,我会得到与上面相同的结果但有异常,否则它不会抛出任何异常。

最佳答案

像这样添加一个检查

if(文件[fileIntList].isDirectory()) 继续;

你也应该改变你的文件重命名代码,它应该是

oldName.renameTo(newName)

理想情况下,您应该删除 new File(destDirectory).mkdir();

关于java - 文件已创建但仍抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com