1

i have an Issue with deleting and renaming more than one files in a directory using servlet.While running the below code sometimes some files are deleating some others are not.following is the servlet code iam using.

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 file

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);
        }
    } 

I used synchorinized keyword inside updateRootFile() method..But still it is not working..

divz
  • 7,847
  • 23
  • 55
  • 78
  • 1
    Please format your code and post only relevant code – jmj Dec 26 '11 at 10:24
  • Read this and rethink your real problem: http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909 – BalusC Dec 27 '11 at 02:48

1 Answers1

0

Does this code behaves wrong when running in an environment where many request are issued concurrently or it even doesn't work when you run this method in one single thread? Tried to debug it?

In any case your 'synchronized' construction is just irrelevant here because your 'listOfFiles' is a variable defined locally inside a method and not a field. If does nothing with being a specifically part of servlet but rather its an improper usage of synchronization mechanism.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97