当前位置: 首页 > 面试题库 >

IOException-使用FileOutputStream拒绝访问

堵鸿光
2023-03-14
问题内容

我得到以下IOException:

java.io.IOException: Access is denied
 at java.io.WinNTFileSystem.createFileExclusively(Native Method)
 at java.io.File.createNewFile(File.java:850)
 at zipUnzipper.main(zipUnzipper.java:41)

尝试运行以下代码时:

public class zipUnzipper {
    public zipUnzipper() {
    }

    public static void main(String[] args){

        //Unzip to temp folder. Add all files to mFiles. Print names of all files in mFfiles.
        File file = new File("C:\\aZipFile.zip");
        String  filename = file.getName();
        String filePathName = new String();

        int o = filename.lastIndexOf('.');
            filename = filename.substring(0,o);

        try {      
                ZipFile zipFile = new ZipFile (file.getAbsoluteFile());
                Enumeration entries = zipFile.entries();
                while(entries.hasMoreElements()) {
                    ZipEntry zipEntry = (ZipEntry) entries.nextElement();
                    System.out.println("Unzipping: " + zipEntry.getName());
                    BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
                    byte[] buffer = new byte[2048];
                    filePathName = "C:\\TEMP\\"+filename+"\\";
                    File fileToWrite = new File(filePathName+ zipEntry.getName());
                    fileToWrite.mkdirs();
                    fileToWrite.createNewFile();
                    FileOutputStream fos = new FileOutputStream(fileToWrite);
                    BufferedOutputStream bos = new BufferedOutputStream( fos , buffer.length );
                    int size;
                    while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
                        bos.write(buffer, 0, size);
                    }
                    bos.flush();
                    bos.close();
                    bis.close();
                }
                zipFile.close();
                File folder = new File (filePathName);
                File [] mFiles = folder.listFiles();

                for (int x=0; x<mFiles.length; x++) {
                                System.out.println(mFiles[x].getAbsolutePath());
                        }
        } catch (ZipException ze) {
            ze.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    }
}

在我看来,由于某种原因,JVM无法创建新文件。如果文件已经存在,则代码可以很好地运行。是否存在某种访问文件来指示JVM是否可以创建新文件还是我只是在做错什么?

任何帮助深表感谢 :-)

我正在运行Java 1.4,并且已经在Windows XP中的JDeveloper中进行了测试。


问题答案:

问题在于这些调用彼此接替:

  fileToWrite.mkdirs(); //creates a directory e.g. C:\temp\foo\x
  fileToWrite.createNewFile(); //attempts to create a file C:\temp\foo\x

创建操作失败,因为您刚刚创建的目录名与要创建的文件相同。

您要执行的操作是:

fileToWrite.getParentFile().mkdirs()

而且,createNewFile()没有必要拨打电话。

根据您的代码。以下“解压缩”一个zip文件:

import java.io.*;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.Enumeration;

public class Unzipper {
    public static void main(String[] args)
            throws IOException {
        final File file = new File(args[0]);
        final ZipFile zipFile = new ZipFile(file);
        final byte[] buffer = new byte[2048];
        final File tmpDir = new File(System.getProperty("java.io.tmpdir"), zipFile.getName());

        if(!tmpDir.mkdir() && tmpDir.exists()) {
            System.err.println("Cannot create: " + tmpDir);
            System.exit(0);
        }

        for(final Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
            final ZipEntry zipEntry = (ZipEntry) entries.nextElement();
            System.out.println("Unzipping: " + zipEntry.getName());

            final InputStream is = zipFile.getInputStream(zipEntry);
            final File fileToWrite = new File(tmpDir, zipEntry.getName());
            final File folder = fileToWrite.getParentFile();
            if(!folder.mkdirs() && !folder.exists()) {
                System.err.println("Cannot create: " + folder);
                System.exit(0);
            }

            if(!zipEntry.isDirectory()) {
                //No need to use buffered streams since we're doing our own buffering
                final FileOutputStream fos = new FileOutputStream(fileToWrite);
                int size;
                while ((size = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, size);
                }
                fos.close();
                is.close();
            }
        }
        zipFile.close();
    }
}

免责声明:我还没有测试过最基本的内容。



 类似资料:
  • 问题内容: 我在使它工作时遇到问题。它接收一个字符串,其中包含几条信息。但是,当我尝试将String写入文件以跟踪程序随时间的变化时,我收到拒绝访问错误: 叠层纱 完整的堆栈跟踪: 58行: 问题答案: 您必须先创建文件夹。但是您不能调用file.mkdirs()-您需要调用file.getParentFile()。mkdirs()-否则,您将使用文件名创建一个文件夹(这将阻止您使用以下命令创建文

  • 我正在开发一个必须访问访问者文件系统的Java小程序,所以我压缩了我的

  • 我正试图将一个实时WordPress站点复制到我的本地服务器上。我通过MAMP从现场站点导入数据库,在尝试连接到phpMyAdmin时收到了一条错误消息: 我在这一点上被难住了。有人能帮忙吗?

  • 当我创建新用户或授予现有特权,我得到了这个错误: 授予所有表上的权限ok(信息\u架构除外),在此表上我得到了拒绝访问错误。我怎么能修理?转储所有数据库,删除所有数据库,然后从转储还原?

  • 为什么当我试图创建InputStream时,下面的代码会给我一个File Not Found异常?我的inputdirectory定义为一个文件,其值为“D:\general\images\small_images”(不带引号),我的用户对该文件具有完全的写权限。我正在使用Windows7,并以管理员的身份运行eclipse IDE。 如有任何帮助,不胜感激。

  • 我找到了商店定位器的谷歌教程:https://developers.google.com/maps/articles/phpsqlsearch_v3#findnearsql MySQL表完成了,它在我的服务器上工作。我可以在phpmyadmin中正确地写入地址和执行操作 但是在“用PHP输出XML”的教程中,我不能继续了。 我已经创建了文件phpsqlsearch_dbinfo.php,把我的数据