Results 1 to 3 of 3
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question Not able to delete the file from the temp location after creating a zip file.

    Sir

    Can any one tell me , looking at my code , what the problem is and the solution for it .

    I am running a sql select query , which brings me clob data and the record ID number.

    Each record is put into a file and all together i get around 200 files . All these files are placed in a folder in the temp location. All these files are taken and zipped , that zip file is kept at different directory. Once the zip file is created, these individual files have to be deleted from the temp location .

    Out of 200 files , 17 files are not getting deleted, if i run the program from eclipse.
    And if i run the jar from console, 1 file is not getting deleted. 199 files are deleted.

    Not able to figure it out , any help is appreciated.
    here is the code ..
    Java Code:
     try {
    			//get the list of files from the given path.
    			File[] listOfFiles = Foil.listFiles();
    			int countOfFiles = listOfFiles.length;
    			System.out.println("the count of files: "+countOfFiles);
    			BufferedInputStream origin = null;
    			FileOutputStream dest = new FileOutputStream(ZipEmailFile,false);         
    			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));            
    			//out.setMethod(ZipOutputStream.DEFLATED);
    			byte data[] = new byte[BUFFER];      
    
    			for (int i = 0; i < listOfFiles.length; i++) {
    				FileInputStream fi = new FileInputStream(listOfFiles[i]);              
    				origin = new BufferedInputStream(fi, BUFFER);  
    				// Add ZIP entry to output stream.
    				ZipEntry entry = new ZipEntry(listOfFiles[i].getName());
    				out.putNextEntry(entry);    
    				int count;
    				while((count = origin.read(data, 0,BUFFER)) != -1) {              
    					out.write(data, 0, count);
    				}
    				origin.close();
    			}// end of for loop
    			out.close();
    //			Delete all files from the Temp directory.
    			for(int i=0;i<listOfFiles.length;i++){
    				System.out.println("the name of the file before deleting: "+listOfFiles[i].getName());
    				boolean successDelFiles = listOfFiles[i].delete();
    				if(!successDelFiles)
    					System.out.println("Delete: Delete of files in Temp directory failed");
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    		}	
    		System.out.println(" ");
    		System.out.println("Name of the output Email zip file: "+ZipEmailFile+"\n"); 		
    		}

    This above code is for zipping and deleting the individual files after zipping.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    it throws any error? Or exception? If yes, post it here

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Two approaches you can try:
    -- explicitly set the variable referencing the File to null
    -- invoke System.gc() before deleting the files.

    (Disclaimer: I haven't read your code, only the problem description)

    db

Similar Threads

  1. Can't Delete A File
    By blazecode in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-07-2011, 10:16 AM
  2. Delete A File
    By nitinverma in forum CLDC and MIDP
    Replies: 13
    Last Post: 06-15-2010, 08:12 AM
  3. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 03:53 PM
  4. File Location
    By bcbird in forum New To Java
    Replies: 1
    Last Post: 09-17-2009, 08:03 AM
  5. Delete From .txt file
    By Sarinam in forum New To Java
    Replies: 86
    Last Post: 06-28-2008, 10:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •