Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 978095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:00:30+00:00 2026-05-16T04:00:30+00:00

Java isn’t deleting a file. I made sure it has permissions (full control to

  • 0

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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:00:31+00:00Added an answer on May 16, 2026 at 4:00 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

two questions: how do i compile a .java file that isn't on my username
How do you scan a file with java that isn't in the directory the
I'm sure this isn't a hugely complex problem but I'm relatively new to java
I have a java web service I am trying to support java isn't really
I know that in GWT the java reflection tools isn't emulated, but is there
Isn't there another way of using global variables in a Java stlye manner in
In Java, I'm wondering why the length attribute of the String class isn't private?
Why is Java Vector considered a legacy class, obsolete or deprecated? Isn't its use
When I tested this code, it gave me java.lang.ArrayIndexOutOfBoundsException: 2 The code isn't finished
I'm getting blocked on a jsp page and our 1 java engineer isn't able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.