无法删除 NetBeans 中 servlet 目录中的所有文件



我不能删除目录中的所有文件。这是一个NetBeans中的servlet应用程序。

 public void updateRootFile(String directorypath, String appID, String[] appName) throws IOException { 
    FileInputStream  finStream =null;
    BufferedReader br = null;
    OutputStreamWriter outStream = null;
    FileInputStream  fins = null;
    String strLine="";       
    try {      
        File[] listOfFiles = fileLists("D:\root\");
        for (int i = 0; i < listOfFiles.length; i++) {
            String rootFiles = listOfFiles[i].getName();
            File rootFile = new File("D:/root/"+rootFiles);           
            File copyFile = new File("D:/root1/"+rootFiles);
            finStream = new FileInputStream(rootFile);
            //String propertyStr = "root.label." + appID;
            //properties.load(new InputStreamReader(finStream, Charset.forName("UTF-8")));
            //String getAppName = properties.getProperty("root.label." + appID);
            //String toUtf =new String(appName[i].getBytes("iso-8859-1"), "UTF-8")  ;
            //String oldChar =  propertyStr + "=" + getAppName;
            //String newChar = propertyStr + "=" + toUtf;
            fins = new FileInputStream(rootFile);
            br = new BufferedReader(new InputStreamReader(fins, "UTF-8"));
            outStream = new OutputStreamWriter(new FileOutputStream(copyFile), "UTF-8");
            while ((strLine = br.readLine()) != null) {               
                strLine = strLine.replace("hai", "haai");
                outStream.write(strLine);
                outStream.write("rn");
            }
            outStream.flush();
            outStream.close();
            br.close();
            fins.close();
            finStream.close();                
        }                 
    } catch (Exception e) {
        System.out.println("Excpetion : " + e);
    } 
    File rootFileone = new File("D:/root");     
    boolean check= deleteDir(rootFileone);      
}

public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i=1; i<children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            //if (!success) {
             //   return false;
           // }
        }
    }
    // The directory is now empty so delete it
    return dir.delete();
}

private  File[] fileLists(String directorypath) {
    folder = new File(directorypath);
    listOfFiles = folder.listFiles();
    return listOfFiles;
}

文件夹中有29个.properties文件。但是在这个循环中,总是至少有一个文件没有被删除。因此我不能删除文件夹

如果文件被其他人打开,则不能删除。

有很多可能的原因,包括代码内部和外部。我假设您只在自己的代码中管理这些文件。您需要确保您正在读取/写入文件的try块的finally块中的InputStream/OutputStreamReader/Writer上调用close()方法。

因此,请确保遵循以下创建、使用和关闭InputStream/OutputStreamReader/Writer资源的Java IO习惯。

Writer writer = null;
try {
    writer = new FileWriter(file); // Just an example. Can be any Writer.
    writer.write(something);
    // ...
} finally {
    if (writer != null) try { writer.close(); } catch (IOException ignore) {}
}

或者当你已经在Java 7上:

try (Writer writer = new FileWriter(file)) {
    writer.write(something);
    // ...
}

try块剩余时,将自动关闭AutoCloseable资源

如何使用org.apache.commons.io.FileUtils.deleteDirectory()递归删除目录?

相关内容

  • 没有找到相关文章

最新更新