我有以下Groovy测试脚本:
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
哪个失败了:
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)
但是,如果我将测试脚本修改为如下所示:
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
它将不再失败。我在Windows 7 64位上运行Java 6。任何人都知道延迟来自何处或如何解释?
修改
在Java 6中发生了同样的错误,这就是我将其标记为Java的原因(Groovy只是更容易编写示例)。这是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();
}
}
}
答案 0 :(得分:0)
这似乎已经成功了:
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();
}
}
}
仍然不确定删除文件夹时的延迟来自哪里,但我认为这只是文件系统的一个怪癖。