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 9252133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:52:09+00:00 2026-06-18T10:52:09+00:00

I have the following Groovy test script: def dir = new File(test) dir.mkdirs() char[]

  • 0

I have the following Groovy test script:

def dir = new File("test")
dir.mkdirs()

char[] data = new char[100]
Arrays.fill(data, (char)'q')

for(i in 0..1760){
    def file = new File(dir, "file$i")
    file.createNewFile()

    file.withOutputStream { os ->
        os << data  
    }
}

def delete (File f){
    if(f.isDirectory()){
        for(File afile : f.listFiles()){
            delete(afile);
        }
        f.delete();
    }else{
        f.delete();
    }
}

delete(dir)

dir.mkdirs()

new File(dir, "file").createNewFile() //<-- java.io.IOException: Access is denied

Which fails with:

Caught: java.io.IOException: Access is denied
java.io.IOException: Access is denied
    at java_io_File$createNewFile.call(Unknown Source)
    at test.run(test.groovy:29)

However, if I amend the test script to look like this:

def dir = new File("test")
dir.mkdirs()

char[] data = new char[100]
Arrays.fill(data, (char)'q')

for(i in 0..1760){
    def file = new File(dir, "file$i")
    file.createNewFile()

    file.withOutputStream { os ->
        os << data  
    }
}

def delete (File f){
    if(f.isDirectory()){
        for(File afile : f.listFiles()){
            delete(afile);
        }
        f.delete();
    }else{
        f.delete();
    }
}

delete(dir)

Thread.sleep(1000) // <---- added a 1 second pause after deleting

dir.mkdirs()

new File(dir, "file").createNewFile() // <-- No Exception

It will no longer fail. I am running Java 6 on Windows 7 64-bit. Anyone have any idea where the delay comes from or how to account for it?

Edit:

Same error happens in Java 6 which is why I tagged it Java (Groovy is just easier to write an example in). Here is the equivalent test which also fails in Java:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;


public class Test {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        new Test().execute();
    }

    public void execute() throws IOException{
        File dir = new File("test");
        dir.mkdirs();

        char[] data = new char[100];
        Arrays.fill(data, (char)'q');

        for(int x = 0; x < 1760; x++){
            File file = new File(dir, "file" + x);
            file.createNewFile();

            FileWriter fw = null;
            try{
                fw = new FileWriter(file);
                fw.write(data);
            }finally{
                if(fw != null){
                    fw.close();
                }
            }
        }

        delete(dir);

        dir.mkdirs();

        new File(dir, "file").createNewFile(); //<-- java.io.IOException: Access is denied
    }

    private void delete (File f){
        if(f.isDirectory()){
            for(File afile : f.listFiles()){
                delete(afile);
            }
            f.delete();
        }else{
            f.delete();
        }
    }

}
  • 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-06-18T10:52:10+00:00Added an answer on June 18, 2026 at 10:52 am

    This seems to have done the trick:

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Arrays;
    
    
    public class Test {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            new Test().execute();
        }
    
        public void execute() throws IOException{
            File dir = new File("test");
            dir.mkdirs();
    
            char[] data = new char[100];
            Arrays.fill(data, (char)'q');
    
            for(int x = 0; x < 1760; x++){
                File file = new File(dir, "file" + x);
                file.createNewFile();
    
                FileWriter fw = null;
                try{
                    fw = new FileWriter(file);
                    fw.write(data);
                }finally{
                    if(fw != null){
                        fw.close();
                    }
                }
            }
    
            delete(dir);
    
            //dir.mkdirs(); <-- commented out
            while(!dir.mkdirs()); //<-- dir.mkdirs() will return false until the directory has been successfully re-created
    
            new File(dir, "file").createNewFile();
        }
    
        private void delete (File f){
            if(f.isDirectory()){
                for(File afile : f.listFiles()){
                    delete(afile);
                }
                f.delete();
            }else{
                f.delete();
            }
        }
    
    }
    

    Still not sure where the delay when deleting the folder comes from exactly but I’m assuming it’s just a quirk of the filesystem.

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

Sidebar

Related Questions

I have a simple groovy script that generates xml def builder = new groovy.xml.StreamingMarkupBuilder()
I have following script which is aim to make a war file. def ant
I have the following code: InputStream reportFile = MyPage.this.getClass().getResourceAsStream(test.jrxml); HashMap<String, String> parameters = new
I have the following Groovy code but it wont work: xml = new groovy.xml.MarkupBuilder()
I have the following Groovy code : def number = 246 as List def
I have a following snippet with JMock expectations in my test method: context.checking(new Expectations()
Lets assume that I have the following configuration in my conf/InjectionConfig.groovy file: x {
I have the following lines in my .sh file #! /usr/bin/env /Users/myuser/Development/tools/groovy println this
I have the following Groovy domain class: class A { def lotOfBs = []
I have the following code in Groovy 1.8.5 and to me it looks valid.

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.