有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

已创建java文件,但仍引发异常

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

解压主文件。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路径中有任何文件夹,我将得到与上述相同的异常结果,否则它不会引发任何异常


共 (3) 个答案

  1. # 1 楼答案

    无法将目录作为文件打开。您的FileInputStream正试图打开D:\\News\\Zip\\Result\\目录

  2. # 3 楼答案

    像这样添加一个检查

    如果(文件[fileIntList].isDirectory()) 继续

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

    oldName。重命名为(新名称)

    理想情况下,您应该删除新文件(destDirectory)。mkdir()