Unable to delete the file after writing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthickkuchanur
    New Member
    • Dec 2007
    • 156

    Unable to delete the file after writing

    Code:
    package javaTesting;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class FileMove {
    	 FileInputStream in = null;
    	 FileOutputStream out = null;
    	 String newFileName ="";
    	 File fileout = null;
    	public  static void main(String[] args) throws IOException {
    		try {
    			FileMove fileMove = new FileMove();
    			fileMove.deleteAction();
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	
    	
    	public void deleteAction() {
    		try {
    			File file = new File("D:/temp");
    			String monthday = getMOnthDay();
    			String outFileName = "D:/temp1/"+monthday;
    			createFolderIfNotExist(outFileName);
    			copyFiles(file,outFileName);
    			file = null;
    			System.out.println("iam here");
    			deleteAllfiles();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	private  String getMOnthDay() {
    		 String dayMonth ="";
    		  Calendar ca1 = Calendar.getInstance();
    		  int iDay=ca1.get(Calendar.DATE);
    	        Date date = new Date();
    	        SimpleDateFormat sdf;
    	        sdf = new SimpleDateFormat("MMM");
    	        System.out.println("Month " + sdf.format(date));
    	        dayMonth = sdf.format(date)+String.valueOf(iDay);
    	        System.out.println(dayMonth);
    
    		return dayMonth;
    	}
    
    	private void copyFiles(File file, String outFileName) {
    		try {
    			File[] files = file.listFiles();
    			for (int i = 0; i < files.length; i++) {
    				
    				newFileName = outFileName+"/"+files[i].getName();
    				fileout = new File(newFileName);
    				out = new FileOutputStream(fileout);
    				in = new FileInputStream(files[i]);
    				byte[] bs = new byte[1024];
    				int len;
    				while ((len = in.read(bs)) > 0) {
    	 				out.write(bs, 0, len);
    				}
    			}
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		finally
    		{
    			try 
    			{
    				if(in!=null)
    				{
    					in.close();
    				}
    				if(out!=null)
    				{
    					out.flush();
    					out.close();
    				}
    			} 
    			catch (Exception e) 
    			{
    				System.out.println("==== Ex pppp "+e);
    				e.printStackTrace();
    			}
    		}
    	}
    
    	private  void deleteAllfiles() {
    		try {
    			File file = new File("D:/temp");
    			File[] deletefiles = file.listFiles();
    			for (int i = 0; i < deletefiles.length; i++) {
    				 System.out.println(deletefiles[i].delete());
    			}
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		
    	}
    
    	private  void createFolderIfNotExist(String outFileName) {
    		
    		try {
    			File createFolder = new File(outFileName);
    			if (createFolder.exists()) {
    				createFolder.mkdir();
    				System.out.println("exit");
    			}
    			else {
    				System.out.println("not exist");
    				createFolder.mkdir();
    				
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		
    	}
    }
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Your code is throwing null pointer exception in
    Code:
    copyFiles
    function.

    Are you sure this executes correctly to you?

    Regards
    Dheeraj Joshi

    Comment

    • karthickkuchanur
      New Member
      • Dec 2007
      • 156

      #3
      Creatre folder

      Please create folder temp1,temp,

      temp is source folder
      temp1 is destination folder

      Finally the files in the temp should be deleted

      Copy from source and destination is working fine but deletion is not working

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Deleting is also working fine. It created a folder with todays date in destination folder and deleted the file from source folder, leaving the folder empty.

        Regards
        Dheeraj Joshi

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          Ok i think i know what is the problem.

          This is because you are not closing the streams after every file being copied to the destination folder. You are closing the stream only for last file which is copied. So only the last file gets deleted not the other. So i deleted your finally block and placed it inside the for like

          Code:
          try {
          File[] files = file.listFiles();
          for (int i = 0; i < files.length; i++) {
           
          newFileName = outFileName+"/"+files[i].getName();
          fileout = new File(newFileName);
          out = new FileOutputStream(fileout);
          in = new FileInputStream(files[i]);
          byte[] bs = new byte[1024];
          int len;
          while ((len = in.read(bs)) > 0) {
               out.write(bs, 0, len);
          }
          try 
          {
              if(in!=null)
              {
                   in.close();
              }
              if(out!=null)
              {
                   out.flush();
                   out.close();
              }
          } 
          catch (Exception e) 
          {
              System.out.println("==== Ex pppp "+e);
              e.printStackTrace();
          }
          }
          }
          catch (Exception e) {
               e.printStackTrace();
          }
          Now you must be able to delete all the files.

          Regards
          Dheeraj Joshi

          Comment

          • Dheeraj Joshi
            Recognized Expert Top Contributor
            • Jul 2009
            • 1129

            #6
            Finally block executes once the execution of try/catch or try finishes.

            Regards
            Dheeraj Joshi

            Comment

            • karthickkuchanur
              New Member
              • Dec 2007
              • 156

              #7
              Thank u very much

              Comment

              • Dheeraj Joshi
                Recognized Expert Top Contributor
                • Jul 2009
                • 1129

                #8
                Welcome. Happy to help.

                Regards
                Dheeraj Joshi

                Comment

                Working...