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

时间:2011-12-26 10:23:00

标签: java file servlets

我有一个问题,即使用servlet删除和重命名目录中的多个文件。运行下面的代码时,有时某些文件正在删除其他一些文件。以下是我使用的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);
        }
    } 

我在updateRootFile()方法中使用了synchorinized关键字。但仍然无法正常工作..

1 个答案:

答案 0 :(得分:0)

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

在任何情况下,你的'synchronized'构造在这里都是无关紧要的,因为你的'listOfFiles'是一个在方法内而不是字段内局部定义的变量。如果不是servlet的特定部分,而是不正确地使用同步机制。