473,431 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,431 software developers and data experts.

Unable to delete the file after writing

karthickkuchanur
156 100+
Expand|Select|Wrap|Line Numbers
  1. package javaTesting;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10.  
  11. public class FileMove {
  12.      FileInputStream in = null;
  13.      FileOutputStream out = null;
  14.      String newFileName ="";
  15.      File fileout = null;
  16.     public  static void main(String[] args) throws IOException {
  17.         try {
  18.             FileMove fileMove = new FileMove();
  19.             fileMove.deleteAction();
  20.  
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.  
  26.  
  27.  
  28.     public void deleteAction() {
  29.         try {
  30.             File file = new File("D:/temp");
  31.             String monthday = getMOnthDay();
  32.             String outFileName = "D:/temp1/"+monthday;
  33.             createFolderIfNotExist(outFileName);
  34.             copyFiles(file,outFileName);
  35.             file = null;
  36.             System.out.println("iam here");
  37.             deleteAllfiles();
  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         }
  41.     }
  42.  
  43.     private  String getMOnthDay() {
  44.          String dayMonth ="";
  45.           Calendar ca1 = Calendar.getInstance();
  46.           int iDay=ca1.get(Calendar.DATE);
  47.             Date date = new Date();
  48.             SimpleDateFormat sdf;
  49.             sdf = new SimpleDateFormat("MMM");
  50.             System.out.println("Month " + sdf.format(date));
  51.             dayMonth = sdf.format(date)+String.valueOf(iDay);
  52.             System.out.println(dayMonth);
  53.  
  54.         return dayMonth;
  55.     }
  56.  
  57.     private void copyFiles(File file, String outFileName) {
  58.         try {
  59.             File[] files = file.listFiles();
  60.             for (int i = 0; i < files.length; i++) {
  61.  
  62.                 newFileName = outFileName+"/"+files[i].getName();
  63.                 fileout = new File(newFileName);
  64.                 out = new FileOutputStream(fileout);
  65.                 in = new FileInputStream(files[i]);
  66.                 byte[] bs = new byte[1024];
  67.                 int len;
  68.                 while ((len = in.read(bs)) > 0) {
  69.                      out.write(bs, 0, len);
  70.                 }
  71.             }
  72.  
  73.         } catch (Exception e) {
  74.             e.printStackTrace();
  75.         }
  76.         finally
  77.         {
  78.             try 
  79.             {
  80.                 if(in!=null)
  81.                 {
  82.                     in.close();
  83.                 }
  84.                 if(out!=null)
  85.                 {
  86.                     out.flush();
  87.                     out.close();
  88.                 }
  89.             } 
  90.             catch (Exception e) 
  91.             {
  92.                 System.out.println("==== Ex pppp "+e);
  93.                 e.printStackTrace();
  94.             }
  95.         }
  96.     }
  97.  
  98.     private  void deleteAllfiles() {
  99.         try {
  100.             File file = new File("D:/temp");
  101.             File[] deletefiles = file.listFiles();
  102.             for (int i = 0; i < deletefiles.length; i++) {
  103.                  System.out.println(deletefiles[i].delete());
  104.             }
  105.  
  106.         } catch (Exception e) {
  107.             e.printStackTrace();
  108.         }
  109.  
  110.  
  111.     }
  112.  
  113.     private  void createFolderIfNotExist(String outFileName) {
  114.  
  115.         try {
  116.             File createFolder = new File(outFileName);
  117.             if (createFolder.exists()) {
  118.                 createFolder.mkdir();
  119.                 System.out.println("exit");
  120.             }
  121.             else {
  122.                 System.out.println("not exist");
  123.                 createFolder.mkdir();
  124.  
  125.             }
  126.         } catch (Exception e) {
  127.             e.printStackTrace();
  128.         }
  129.  
  130.  
  131.     }
  132. }
  133.  
Oct 11 '10 #1

✓ answered by Dheeraj Joshi

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

Expand|Select|Wrap|Line Numbers
  1. try {
  2. File[] files = file.listFiles();
  3. for (int i = 0; i < files.length; i++) {
  4.  
  5. newFileName = outFileName+"/"+files[i].getName();
  6. fileout = new File(newFileName);
  7. out = new FileOutputStream(fileout);
  8. in = new FileInputStream(files[i]);
  9. byte[] bs = new byte[1024];
  10. int len;
  11. while ((len = in.read(bs)) > 0) {
  12.      out.write(bs, 0, len);
  13. }
  14. try 
  15. {
  16.     if(in!=null)
  17.     {
  18.          in.close();
  19.     }
  20.     if(out!=null)
  21.     {
  22.          out.flush();
  23.          out.close();
  24.     }
  25. catch (Exception e) 
  26. {
  27.     System.out.println("==== Ex pppp "+e);
  28.     e.printStackTrace();
  29. }
  30. }
  31. }
  32. catch (Exception e) {
  33.      e.printStackTrace();
  34. }
  35.  
Now you must be able to delete all the files.

Regards
Dheeraj Joshi

7 9496
Dheeraj Joshi
1,123 Expert 1GB
Your code is throwing null pointer exception in
Expand|Select|Wrap|Line Numbers
  1. copyFiles
  2.  
function.

Are you sure this executes correctly to you?

Regards
Dheeraj Joshi
Oct 12 '10 #2
karthickkuchanur
156 100+
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
Oct 12 '10 #3
Dheeraj Joshi
1,123 Expert 1GB
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
Oct 12 '10 #4
Dheeraj Joshi
1,123 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. try {
  2. File[] files = file.listFiles();
  3. for (int i = 0; i < files.length; i++) {
  4.  
  5. newFileName = outFileName+"/"+files[i].getName();
  6. fileout = new File(newFileName);
  7. out = new FileOutputStream(fileout);
  8. in = new FileInputStream(files[i]);
  9. byte[] bs = new byte[1024];
  10. int len;
  11. while ((len = in.read(bs)) > 0) {
  12.      out.write(bs, 0, len);
  13. }
  14. try 
  15. {
  16.     if(in!=null)
  17.     {
  18.          in.close();
  19.     }
  20.     if(out!=null)
  21.     {
  22.          out.flush();
  23.          out.close();
  24.     }
  25. catch (Exception e) 
  26. {
  27.     System.out.println("==== Ex pppp "+e);
  28.     e.printStackTrace();
  29. }
  30. }
  31. }
  32. catch (Exception e) {
  33.      e.printStackTrace();
  34. }
  35.  
Now you must be able to delete all the files.

Regards
Dheeraj Joshi
Oct 12 '10 #5
Dheeraj Joshi
1,123 Expert 1GB
Finally block executes once the execution of try/catch or try finishes.

Regards
Dheeraj Joshi
Oct 12 '10 #6
karthickkuchanur
156 100+
Thank u very much
Oct 12 '10 #7
Dheeraj Joshi
1,123 Expert 1GB
Welcome. Happy to help.

Regards
Dheeraj Joshi
Oct 12 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
3
by: Paul | last post by:
Is it possible to delete a file on a client side machine using VB/JAVA script? My website allows the user to upload a file to the website and after that I would like to delete the file on their...
4
by: Michael K. | last post by:
Hi there I am having trouble deleting an uploaded file that I have just written to disc. I am using the SaveAs method of a HttpPostedFile to write the file to disc, then straight after I'm...
3
by: facicad | last post by:
I would like to know if as event before delete file exist. My probleme is with FileSystemWatch, it is only Deleted event, and went I want copy for bakup my file, the file do not exist :(
2
by: TOI DAY | last post by:
Hi all, How can I delete the file on the server after the user download it? For example: I have file name "123.txt" on a server, I copy it to "ABC.txt", then allow uer download the...
3
by: Parag Gaikwad | last post by:
Hi, I need to delete files across the network using web client (IE 6.x) in Win 2003 - IIS 6.0 environment. Can someone please suggest an approach I can use to acheive this. Will using FSO do...
5
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows...
1
by: ABC | last post by:
Is there any best solution automatic delete file within the special time? My web application can generate many pdf files for users. Because there are many users, I need to control the total...
3
by: HMS Surprise | last post by:
If file writing has no return value (http://docs.python.org/lib/bltin- file-objects.html), how do you know if the write was successful? Should one assume that if the open was successful then write...
4
by: id10t error | last post by:
Hello, I am making a program that will not have an user interaction to delete certian files. I found this line of code. My.Computer.FileSystem.DeleteFile("C:\POLLJP.DWN",...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.