创建的文件但仍然抛出异常

时间:2015-11-18 08:39:28

标签: java

我正在尝试创建并向该目录添加文件。下面是我的代码,我可以创建并添加文件给他们,但最后它给我一个例外。 我的代码:

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

Unzip.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 个答案:

答案 0 :(得分:2)

添加一个像这样的支票

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

你也应该更改你应该的文件重命名代码

<强> oldName.renameTo(newName)将

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

答案 1 :(得分:1)

此行导致异常:

ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));

因为你给它一个目录( D:\ News \ Zip \ Result

请确保您提供文件路径,而不是目录路径。

尝试设置断点进行检查。

Get FileNotFoundException when initialising FileInputStream with File object

答案 2 :(得分:1)

您无法将目录作为文件打开。您的FileInputStream正在尝试打开D:\\News\\Zip\\Result\\这是一个目录。