2011-12-26 56 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的一個特定部分,而是不適當地使用同步機制。

相關問題