2011-12-26 74 views
1

我有一个使用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关键字)method..But它仍然无法正常工作..

+1

请格式化您的代码并发布相关代码 – 2011-12-26 10:24:09

+0

阅读此内容并重新考虑您的真实问题:http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909 – BalusC 2011-12-27 02:48:12

回答

0

是否在运行时,该代码错误行为许多请求同时发布的环境,或者在单线程中运行此方法时甚至无法工作?试图调试它?

在任何情况下,您的“同步”结构在这里都是无关紧要的,因为您的'listOfFiles'是在方法内而不是字段中定义的变量。如果不作为servlet的一个特定部分,而是不适当地使用同步机制。