可以提取文件但不能删除文件

时间:2014-03-29 17:04:15

标签: java file zip delete-file

以下是我用于提取和删除文件的JAVA代码。问题是当我成功提取zip文件,然后当我删除它时。它不会起作用。

注意:
local =我的本地文件夹
zipFile =我的zip文件名
zip和txt文件由java程序从ftp server下载 我可以手动从我的资源管理器中删除,除了我的程序

public static void extract(File local, String zipFile) {

    try {
        // destination folder to extract the contents         
        String destName = local + "";      

        byte[] buf = new byte[1024];
        ZipInputStream zis = null;
        ZipEntry zipentry;
        zis = new ZipInputStream(new FileInputStream(local + "/" + zipFile));
        zipentry = zis.getNextEntry();

        while (zipentry != null) {
            // for each entry to be extracted
            String entryName = zipentry.getName();

            int n;
            FileOutputStream fos3;
            File newFile = new File(entryName);

            String directory = "/tmp/";

            // to creating the parent directories
            if (directory == null) {
                if (newFile.isDirectory()){
                    break;
                }
            } else {
                new File(destName+directory).mkdirs();
            }

            if(!zipentry.isDirectory()){ 
                System.out.println("File to be extracted....."+ entryName);
                fos3 = new FileOutputStream(destName + directory  + entryName);

                while ((n = zis.read(buf, 0, 1024)) > -1){
                    fos3.write(buf, 0, n);
                }
                fos3.close();
            }

            zis.closeEntry();
            zipentry = zis.getNextEntry();
        }// while
        zis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static void delete(File file) {

    // Check if file is directory/folder
    if(file.isDirectory()) {
        // Get all files in the folder
        File[] files=file.listFiles();

        for(int i=0;i<files.length;i++) {

            // Delete each file in the folder
            delete(files[i]);
            System.out.println("Successfully delete file --> " + files[i]);
        }

        // Delete the folder
        file.delete();
    }else {
        // Delete the file if it is not a folder
        file.delete();
        System.out.println("Successfully delete file --> " + file);
    }
}
public static void main(String[] args) {
//extract
extract(local, zipFile);

File ftpClientZip = new File(local + "/" + zipFile);
File ftpClientMD5 = new File(local + "/" + txtFile);

delete(ftpClientZip);
delete(ftpClientMD5);
}

3 个答案:

答案 0 :(得分:1)

Files类提供了两种删除方法。

删除(Path)方法删除文件或在删除失败时抛出异常。例如,如果文件不存在,则抛出NoSuchFileException。您可以捕获异常以确定删除失败的原因,如下所示:

try {
    Files.delete(path);
} catch (NoSuchFileException x) {
    System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
    System.err.format("%s not empty%n", path);
} catch (IOException x) {
    // File permission problems are caught here.
    System.err.println(x);
}

deleteIfExists(Path)方法也会删除该文件,但如果该文件不存在,则不会抛出任何异常。当你有多个线程删除文件并且你不想仅仅因为一个线程首先抛出异常时,静默失败很有用。

答案 1 :(得分:0)

好吧,我不确定这是一个错误的帖子,还是代码中的错误。你有重复的delete()方法。

如果那不是问题,我想知道你是否试图根据zip文件中的指针创建文件......

如果不是我没有看到问题

答案 2 :(得分:0)

检查文件/包含文件夹权限。 可能是运行java程序的进程具有读权限但没有写权限