Java 没有删除文件

发布于 2024-09-13 12:49:27 字数 11943 浏览 2 评论 0原文

Java 不会删除文件。我确保它有权限(对每个人都有完全控制权,或者对 Unix 爱好者来说是 777。) System.out.println(currArchive.delete()) 总是返回 false。有谁知道为什么?我尝试用 try-catch 安全异常包装它,但没有抛出它。帮助?我确保所有流都已关闭,但它仍然返回 false!

这是我的代码:

TarGzipManagerView.java 的 addFile 方法

private void addInside(java.awt.event.ActionEvent evt) {
if (isCurrArchived == EXTRACTING){
        final ActionEvent acevt = evt;
        JFileChooser jfc = new JFileChooser();
        jfc.setDialogType(JFileChooser.OPEN_DIALOG);
        jfc.setMultiSelectionEnabled(true);
        jfc.setAcceptAllFileFilterUsed(true);
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jfc.setDialogTitle("Choose files to add:");
        int result = jfc.showOpenDialog(theframe);
        if (result == JFileChooser.APPROVE_OPTION){
            File[] addsf = jfc.getSelectedFiles();
            totalNewFilesToAdd = new ArrayList<File>();
            ArrayList<File> newFilesToAdd = new ArrayList<File>();
            ArrayList<AbsRelFile> dirEntries = new ArrayList<AbsRelFile>();
            InputStream in = null;
            try {
                in = new BufferedInputStream(new FileInputStream(currArchive));
            } catch (FileNotFoundException fnfe){
                return;
            }
            currAStream = null;
            currCStream = null;
            CompressorStreamFactory cFactory = new CompressorStreamFactory();
            try {
                CompressorInputStream temp = cFactory.createCompressorInputStream(in);
                currCStream = temp.getName();
                in = new BufferedInputStream(temp);
            } catch (CompressorException ex) {
                //if it isn't compressed, do nothing!
            }
            ArchiveStreamFactory aFactory = new ArchiveStreamFactory();
            realIn = null;
            useZipFile = false;
            try {
                realIn = aFactory.createArchiveInputStream(in);
                currAStream = realIn.getArchiveName();
                if (currAStream.equalsIgnoreCase(ArchiveStreamFactory.ZIP)){
                    useZipFile = true;
                    try {
                        realIn.close();
                    } catch (IOException ioe){
                        ioe.printStackTrace();
                    }
                }
            } catch (ArchiveException ae){
                //Bad Archive! oops!
                new ErrorDialog(theframe, false).setVisible(true);
                try {
                    in.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
                return;
            }
            bytesToWrite = 0;
            if (!useZipFile){
                ArchiveEntry ae;
                try {
                    while ((ae = realIn.getNextEntry()) != null){
                        bytesToWrite += ae.getSize();
                    }
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
                try {
                    realIn.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
            } else {
                ZipArchiveEntry ae;
                try {
                    ZipFile zf = new ZipFile(currArchive);
                    Enumeration en = zf.getEntriesEnum();
                    while (en.hasMoreElements()){
                        ae = (ZipArchiveEntry) en.nextElement();
                        bytesToWrite += ae.getSize();
                    }
                    zf.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }
            try {
                in = new BufferedInputStream(new FileInputStream(currArchive));
            } catch (FileNotFoundException fnfe){
                return;
            }
            currAStream = null;
            currCStream = null;
            cFactory = new CompressorStreamFactory();
            try {
                CompressorInputStream temp = cFactory.createCompressorInputStream(in);
                currCStream = temp.getName();
                in = new BufferedInputStream(temp);
            } catch (CompressorException ex) {
                //if it isn't compressed, do nothing!
            }
            aFactory = new ArchiveStreamFactory();
            realIn = null;
            useZipFile = false;
            try {
                realIn = aFactory.createArchiveInputStream(in);
                currAStream = realIn.getArchiveName();
                if (currAStream.equalsIgnoreCase(ArchiveStreamFactory.ZIP)){
                    useZipFile = true;
                    try {
                        realIn.close();
                    } catch (IOException ioe){
                        ioe.printStackTrace();
                    }
                }
            } catch (ArchiveException ae){
                //Bad Archive! oops!
                new ErrorDialog(theframe, false).setVisible(true);
                try {
                    in.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
                return;
            }
            for (int i = 0;i<addsf.length;i++){
                if (addsf[i].isFile()){
                    newFilesToAdd.add(addsf[i]);
                    totalNewFilesToAdd.add(addsf[i]);
                }
                if (addsf[i].isDirectory()){
                    dirEntries.addAll(recurseDir(addsf[i]));
                    totalNewFilesToAdd.addAll(recurseDir(addsf[i]));
                }
            }
            for (int i = 0;i<totalNewFilesToAdd.size();i++){
                bytesToWrite += totalNewFilesToAdd.get(i).length();
            }
            aentries = new ArchiveEntry[totalNewFilesToAdd.size()];
            for (int i = 0;i<newFilesToAdd.size();i++){
                try {
                    aentries[i] = new ArchiveStreamFactory().createArchiveEntry(currAStream, newFilesToAdd.get(i), newFilesToAdd.get(i).getName());
                } catch (ArchiveException ae){
                    ae.printStackTrace();
                }
            }
            for (int i = newFilesToAdd.size();i<totalNewFilesToAdd.size();i++){
                try {
                    aentries[i] = new ArchiveStreamFactory().createArchiveEntry(currAStream, dirEntries.get(i-newFilesToAdd.size()), dirEntries.get(i-newFilesToAdd.size()).getRelativePath());
                } catch (ArchiveException ae){
                    ae.printStackTrace();
                }
            }
            Thread iothread = new Thread(new Runnable(){
                public void run(){
                    if (!useZipFile){
                        try {
                            EventQueue.invokeLater(new Runnable(){
                                public void run(){
                                    addingBox = new AddingBox(theframe, false);
                                }
                            });
                            File outfile = File.createTempFile("ScruffArchiveManager", null);
                            ArchiveOutputStream tempout = null;
                            CompressorOutputStream tempcomp = null;
                            if (currCStream == null) tempout = new ArchiveStreamFactory().createArchiveOutputStream(currAStream ,new BufferedOutputStream(new FileOutputStream(outfile)));
                            else {
                                tempcomp = new CompressorStreamFactory().createCompressorOutputStream(currCStream, new BufferedOutputStream(new FileOutputStream(outfile)));
                                tempout = new ArchiveStreamFactory().createArchiveOutputStream(currAStream, tempcomp);
                            }
                            if (currAStream.equals(ArchiveStreamFactory.TAR)){
                                TarArchiveOutputStream t = (TarArchiveOutputStream)tempout;
                                t.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
                                tempout = t;
                            }
                            ArchiveEntry ae;
                            long bytesWritten = 0;
                            while ((ae = realIn.getNextEntry()) != null){
                                long semiBytesWritten = 0;
                                tempout.putArchiveEntry(ae);
                                byte[] buffer = new byte[8024];
                                int n = 0;
                                if (ae.getSize() < semiBytesWritten + 8024) buffer = new byte[(int)(ae.getSize()-semiBytesWritten)];
                                while (semiBytesWritten < ae.getSize() && -1 != (n = realIn.read(buffer))){
                                    tempout.write(buffer, 0, n);
                                    semiBytesWritten += n;
                                    bytesWritten += n;
                                    final long k = bytesWritten;
                                    EventQueue.invokeLater(new Runnable(){
                                        public void run(){
                                            addingBox.progressListener(new Progress(k, bytesToWrite));
                                        }
                                    });
                                    if (ae.getSize() < semiBytesWritten + 8024) buffer = new byte[(int)(ae.getSize()-semiBytesWritten)];
                                }
                                tempout.closeArchiveEntry();
                            }
                            realIn.close();
                            for (int i = 0;i<aentries.length;i++){
                                InputStream in = new BufferedInputStream(new FileInputStream(aentries[i].getFile()));
                                tempout.putArchiveEntry(aentries[i]);
                                int n = 0;
                                byte[] buffer = new byte[8024];
                                while (-1 != (n = in.read(buffer))){
                                    tempout.write(buffer, 0, n);
                                    bytesWritten += n;
                                    final long k = bytesWritten;
                                    EventQueue.invokeLater(new Runnable(){
                                        public void run(){
                                            addingBox.progressListener(new Progress(k, bytesToWrite));
                                        }
                                    });
                                }
                                in.close();
                                tempout.closeArchiveEntry();
                            }
                            tempout.close();
                            final File ne = new File(currArchive.getAbsolutePath());
                            System.out.println(currArchive.delete());
                            outfile.renameTo(ne);
                            EventQueue.invokeLater(new Runnable(){
                                public void run(){
                                    closeArchive(acevt);
                                    openArchive(ne);
                                    addingBox.progressListener(new Progress(bytesToWrite, bytesToWrite));
                                }
                            });
                        } catch (CompressorException ce){
                            ce.printStackTrace();
                        } catch (ArchiveException ae){
                            ae.printStackTrace();
                        } catch (IOException ioe){
                            ioe.printStackTrace();
                        }
                    }
                }
            });
            iothread.start();
        }
    }
}

Java isn't deleting a file. I made sure it has permissions (full control to everyone, or 777 for you Unix-ers.) The System.out.println(currArchive.delete()) always returns false. Does anyone know why? I tried wrapping it with a try-catch security exception but it wasn't thrown. help? I made sure all my streams were closed, but it still returned false!

Here is my code:

addFile method of TarGzipManagerView.java

private void addInside(java.awt.event.ActionEvent evt) {
if (isCurrArchived == EXTRACTING){
        final ActionEvent acevt = evt;
        JFileChooser jfc = new JFileChooser();
        jfc.setDialogType(JFileChooser.OPEN_DIALOG);
        jfc.setMultiSelectionEnabled(true);
        jfc.setAcceptAllFileFilterUsed(true);
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jfc.setDialogTitle("Choose files to add:");
        int result = jfc.showOpenDialog(theframe);
        if (result == JFileChooser.APPROVE_OPTION){
            File[] addsf = jfc.getSelectedFiles();
            totalNewFilesToAdd = new ArrayList<File>();
            ArrayList<File> newFilesToAdd = new ArrayList<File>();
            ArrayList<AbsRelFile> dirEntries = new ArrayList<AbsRelFile>();
            InputStream in = null;
            try {
                in = new BufferedInputStream(new FileInputStream(currArchive));
            } catch (FileNotFoundException fnfe){
                return;
            }
            currAStream = null;
            currCStream = null;
            CompressorStreamFactory cFactory = new CompressorStreamFactory();
            try {
                CompressorInputStream temp = cFactory.createCompressorInputStream(in);
                currCStream = temp.getName();
                in = new BufferedInputStream(temp);
            } catch (CompressorException ex) {
                //if it isn't compressed, do nothing!
            }
            ArchiveStreamFactory aFactory = new ArchiveStreamFactory();
            realIn = null;
            useZipFile = false;
            try {
                realIn = aFactory.createArchiveInputStream(in);
                currAStream = realIn.getArchiveName();
                if (currAStream.equalsIgnoreCase(ArchiveStreamFactory.ZIP)){
                    useZipFile = true;
                    try {
                        realIn.close();
                    } catch (IOException ioe){
                        ioe.printStackTrace();
                    }
                }
            } catch (ArchiveException ae){
                //Bad Archive! oops!
                new ErrorDialog(theframe, false).setVisible(true);
                try {
                    in.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
                return;
            }
            bytesToWrite = 0;
            if (!useZipFile){
                ArchiveEntry ae;
                try {
                    while ((ae = realIn.getNextEntry()) != null){
                        bytesToWrite += ae.getSize();
                    }
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
                try {
                    realIn.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
            } else {
                ZipArchiveEntry ae;
                try {
                    ZipFile zf = new ZipFile(currArchive);
                    Enumeration en = zf.getEntriesEnum();
                    while (en.hasMoreElements()){
                        ae = (ZipArchiveEntry) en.nextElement();
                        bytesToWrite += ae.getSize();
                    }
                    zf.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }
            try {
                in = new BufferedInputStream(new FileInputStream(currArchive));
            } catch (FileNotFoundException fnfe){
                return;
            }
            currAStream = null;
            currCStream = null;
            cFactory = new CompressorStreamFactory();
            try {
                CompressorInputStream temp = cFactory.createCompressorInputStream(in);
                currCStream = temp.getName();
                in = new BufferedInputStream(temp);
            } catch (CompressorException ex) {
                //if it isn't compressed, do nothing!
            }
            aFactory = new ArchiveStreamFactory();
            realIn = null;
            useZipFile = false;
            try {
                realIn = aFactory.createArchiveInputStream(in);
                currAStream = realIn.getArchiveName();
                if (currAStream.equalsIgnoreCase(ArchiveStreamFactory.ZIP)){
                    useZipFile = true;
                    try {
                        realIn.close();
                    } catch (IOException ioe){
                        ioe.printStackTrace();
                    }
                }
            } catch (ArchiveException ae){
                //Bad Archive! oops!
                new ErrorDialog(theframe, false).setVisible(true);
                try {
                    in.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
                return;
            }
            for (int i = 0;i<addsf.length;i++){
                if (addsf[i].isFile()){
                    newFilesToAdd.add(addsf[i]);
                    totalNewFilesToAdd.add(addsf[i]);
                }
                if (addsf[i].isDirectory()){
                    dirEntries.addAll(recurseDir(addsf[i]));
                    totalNewFilesToAdd.addAll(recurseDir(addsf[i]));
                }
            }
            for (int i = 0;i<totalNewFilesToAdd.size();i++){
                bytesToWrite += totalNewFilesToAdd.get(i).length();
            }
            aentries = new ArchiveEntry[totalNewFilesToAdd.size()];
            for (int i = 0;i<newFilesToAdd.size();i++){
                try {
                    aentries[i] = new ArchiveStreamFactory().createArchiveEntry(currAStream, newFilesToAdd.get(i), newFilesToAdd.get(i).getName());
                } catch (ArchiveException ae){
                    ae.printStackTrace();
                }
            }
            for (int i = newFilesToAdd.size();i<totalNewFilesToAdd.size();i++){
                try {
                    aentries[i] = new ArchiveStreamFactory().createArchiveEntry(currAStream, dirEntries.get(i-newFilesToAdd.size()), dirEntries.get(i-newFilesToAdd.size()).getRelativePath());
                } catch (ArchiveException ae){
                    ae.printStackTrace();
                }
            }
            Thread iothread = new Thread(new Runnable(){
                public void run(){
                    if (!useZipFile){
                        try {
                            EventQueue.invokeLater(new Runnable(){
                                public void run(){
                                    addingBox = new AddingBox(theframe, false);
                                }
                            });
                            File outfile = File.createTempFile("ScruffArchiveManager", null);
                            ArchiveOutputStream tempout = null;
                            CompressorOutputStream tempcomp = null;
                            if (currCStream == null) tempout = new ArchiveStreamFactory().createArchiveOutputStream(currAStream ,new BufferedOutputStream(new FileOutputStream(outfile)));
                            else {
                                tempcomp = new CompressorStreamFactory().createCompressorOutputStream(currCStream, new BufferedOutputStream(new FileOutputStream(outfile)));
                                tempout = new ArchiveStreamFactory().createArchiveOutputStream(currAStream, tempcomp);
                            }
                            if (currAStream.equals(ArchiveStreamFactory.TAR)){
                                TarArchiveOutputStream t = (TarArchiveOutputStream)tempout;
                                t.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
                                tempout = t;
                            }
                            ArchiveEntry ae;
                            long bytesWritten = 0;
                            while ((ae = realIn.getNextEntry()) != null){
                                long semiBytesWritten = 0;
                                tempout.putArchiveEntry(ae);
                                byte[] buffer = new byte[8024];
                                int n = 0;
                                if (ae.getSize() < semiBytesWritten + 8024) buffer = new byte[(int)(ae.getSize()-semiBytesWritten)];
                                while (semiBytesWritten < ae.getSize() && -1 != (n = realIn.read(buffer))){
                                    tempout.write(buffer, 0, n);
                                    semiBytesWritten += n;
                                    bytesWritten += n;
                                    final long k = bytesWritten;
                                    EventQueue.invokeLater(new Runnable(){
                                        public void run(){
                                            addingBox.progressListener(new Progress(k, bytesToWrite));
                                        }
                                    });
                                    if (ae.getSize() < semiBytesWritten + 8024) buffer = new byte[(int)(ae.getSize()-semiBytesWritten)];
                                }
                                tempout.closeArchiveEntry();
                            }
                            realIn.close();
                            for (int i = 0;i<aentries.length;i++){
                                InputStream in = new BufferedInputStream(new FileInputStream(aentries[i].getFile()));
                                tempout.putArchiveEntry(aentries[i]);
                                int n = 0;
                                byte[] buffer = new byte[8024];
                                while (-1 != (n = in.read(buffer))){
                                    tempout.write(buffer, 0, n);
                                    bytesWritten += n;
                                    final long k = bytesWritten;
                                    EventQueue.invokeLater(new Runnable(){
                                        public void run(){
                                            addingBox.progressListener(new Progress(k, bytesToWrite));
                                        }
                                    });
                                }
                                in.close();
                                tempout.closeArchiveEntry();
                            }
                            tempout.close();
                            final File ne = new File(currArchive.getAbsolutePath());
                            System.out.println(currArchive.delete());
                            outfile.renameTo(ne);
                            EventQueue.invokeLater(new Runnable(){
                                public void run(){
                                    closeArchive(acevt);
                                    openArchive(ne);
                                    addingBox.progressListener(new Progress(bytesToWrite, bytesToWrite));
                                }
                            });
                        } catch (CompressorException ce){
                            ce.printStackTrace();
                        } catch (ArchiveException ae){
                            ae.printStackTrace();
                        } catch (IOException ioe){
                            ioe.printStackTrace();
                        }
                    }
                }
            });
            iothread.start();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2024-09-20 12:49:28

尝试 myFile.toPath().delete() 来获取错误。并且永远不要忘记关闭FileInputStream

Try myFile.toPath().delete() to get the error. And never forget to close the FileInputStream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文