2009-10-21 38 views
4

我得到以下IOException異常:IOException異常 - 拒絕訪問使用FileOutputStream中

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,並已在JDeveloper在Windows XP測試。

+1

您的Windows帳戶是否有權在該目錄中創建文件? – 2009-10-21 17:03:04

+0

http://stackoverflow.com/a/23697734/715269 – Gangnus 2014-05-16 14:25:31

回答

11

的問題是,這些調用對方步驟:因爲你剛纔創建的目錄比你想要創建的文件同名

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()是不必要的。

根據您的代碼。下面「解壓縮」一個壓縮文件:

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

聲明:我沒有測試過它的基礎知識。

2

你爲什麼打電話給createNewFile()?只需創建FileOutputStream。

0

也可能是在您啓動應用程序的情況下,您沒有權限訪問您嘗試創建文件的位置。以管理員身份啓動應用程序或在項目文件夾中創建文件。

相關問題