Java 删除和重命名目录中的多个文件时出现问题

Java 删除和重命名目录中的多个文件时出现问题,java,file,servlets,Java,File,Servlets,我在使用servlet删除和重命名目录中的多个文件时遇到问题。运行以下代码时,有时某些文件正在删除,而另一些文件则没有。以下是iam使用的servlet代码 public class SendRedirect extends HttpServlet { RootSipResourceApp app =new RootSipResourceApp(); protected void doPost(HttpServletRequest request, Ht

我在使用servlet删除和重命名目录中的多个文件时遇到问题。运行以下代码时,有时某些文件正在删除,而另一些文件则没有。以下是iam使用的servlet代码

public class SendRedirect extends HttpServlet { 



    RootSipResourceApp app =new RootSipResourceApp();

            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                 
                        response.setContentType("text/html; charset=UTF-8");                         if (strSaveFile != null)
                   app.updateRootFile(strDirectorypath, strappID, appNames);

                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
                dispatcher.forward(request, response);
            }



    }
RootSipResource.java文件

public void updateRootFile(String directorypath, String appID, String[] appName) throws IOException {
        FileInputStream fins=null;
        try {
                  File[] listOfFiles = fileLists(directorypath);
            for (int i = 0; i < listOfFiles.length; i++) { 
                synchronized(listOfFiles) { 
                            if (listOfFiles[i].isFile()) {                                      
                    rootFiles = listOfFiles[i].getName();                                           
                    if (rootFiles.endsWith(".properties") || rootFiles.endsWith(".PROPERTIES")) {
                        fins = new FileInputStream(directorypath + rootFiles);
                        properties.load(new InputStreamReader(fins, Charset.forName("UTF-8")));
                        String getAppName = properties.getProperty("root.label." + appID);
                        String propertyStr = "root.label." + appID;                                         
                                             String toUtf =new String(appName[i].getBytes("iso-8859-1"), "UTF-8")  ;
                         saveFile(fins, getAppName, directorypath + rootFiles, propertyStr,toUtf);
                    }                            

                }
                       }

            }

        } catch (Exception e) {
            System.out.println("Exception Inside updateRootFile():: " + e);
        }
    }

    public  void saveFile(FileInputStream fIns, String oldAppName, String filePath, String propertyStr, String appName)
            throws IOException {
        FileInputStream  finStream =null;
        try {                        
                     String oldChar = propertyStr + "=" + oldAppName;
             String newChar = propertyStr + "=" + appName;
             String strLine;
            File rootFile = new File(filePath);
            File copyFile = new File("D:\\root\\root_created.properties");

                    finStream = new FileInputStream(rootFile);
                    BufferedReader br = new BufferedReader(new InputStreamReader(finStream, "UTF-8"));
                OutputStreamWriter outStream = new OutputStreamWriter(new FileOutputStream(copyFile), "UTF-8");
            while ((strLine = br.readLine()) != null) {
                strLine = strLine.replace(oldChar, newChar);
                outStream.write(strLine);
                outStream.write("\r\n");
            }
            outStream.flush();
            outStream.close();
            br.close();
            fIns.close();
                    finStream.close();                        
                rootFile.delete();
            copyFile.renameTo(rootFile);               

        } catch (IOException e) {
            System.out.println(" Excpetion in save file--*******************---" + e);
        }
    } 
public void updateRootFile(String directorypath、String appID、String[]appName)引发IOException{
FileInputStream=null;
试一试{
File[]listOfFiles=文件列表(directorypath);
对于(int i=0;i

我在updateRootFile()方法中使用了Synchornized关键字。但它仍然不起作用。

在多个请求同时发出的环境中运行时,此代码是否行为错误,或者在一个线程中运行此方法时甚至不起作用?试过调试吗


在任何情况下,“synchronized”构造都与此无关,因为“listOfFiles”是在方法内部本地定义的变量,而不是字段。如果它与成为servlet的特定部分无关,而是对同步机制的不当使用。

请格式化您的代码并仅发布相关代码阅读此内容并重新思考您真正的问题: