Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

how can i copy a file from one location to another location then delete............

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
(My requirement is copy a file from one location to another location then delete the file from original location)...when i use the following mechanism
String objDelFileFROM = SourceFileName;
String objDelFileTO= DestinationFileName
FileInputStream from = new FileInputStream(objDelFileFROM );
FileOutputStream to = new FileOutputStream(objDelFileTO);
byte[] buffer = new byte[4096];
int bytes_read;


while((bytes_read = from.read(buffer)) != -1)
{ // Read bytes until EOF
to.write(buffer, 0, bytes_read);// write bytes
}
**File is copied by using above mechanism **
if(objDelFileFROM.exists())
{
objDelFileFROM.delete();
}
objDelFileFROM.exists()is returning false...please any one let me know why bjDelFileFROM.exists() is returning false.
Thanks
Sravan
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you have some code that is not posted ... you must be making a File object to test exists()? Maybe it's really ...
File objDelFileFROM = new File( SourceFileName );
As it stands, I don't see why it would read successfully but not show up as existing. Just for sanity, you might want to test exists() before trying to copy?
 
sravan reddy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James,
thanks for your reply..
please have a look at the following code....
String strDelFilePath = "\\10.111.110.11\\temp_out\\Delete\\00000032.txt"
File objDelFileFROM = new File(strDelFilePath );
if(objDelFileFROM.exists())
{
String strCopyFilePath = "\\10.111.110.11\\temp_out\\temp\\00000032.txt"
File objDelFileTO = new File(strCopyFilePath );

FileInputStream from = new FileInputStream(objDelFileFROM );
FileOutputStream to = new FileOutputStream(objDelFileTO);
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = from.read(buffer)) != -1)
{ // Read bytes until EOF
to.write(buffer, 0, bytes_read);// write bytes
}
objDelFileFROM.delete();
}
}
******************************
objDelFileFROM.delete();//This operation is nor performing..means file is not deleting from the original location
..
Thanks
sravan
[ July 30, 2003: Message edited by: sravan reddy ]
[ July 30, 2003: Message edited by: sravan reddy ]
[ July 30, 2003: Message edited by: sravan reddy ]
 
sravan reddy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got the solution..thanks
i have to close all the references before deleting the file.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it's a moot point now, but you could try using File's renameTo() method. It's not apparent from the method name, but you can often "rename" a file to a different directory. So it's roughly equivalent to mv in Unix or "move" in Windows. The only catch is, the method may fail (return false) in some situations, e.g. if you're trying to move across a network drive (depending how the network is set up). If the renameTo() fails, you can then try the read-write-delete approach above, which is a little more robust (it works in some situations where renameTo() does not) but it also takes longer. If the files are big, this can easily be a notable performance issue.
Also worth noting - if youve got JDK 1.4, then FileChannel has transferTo() and transferFrom() which are generally much, much faster than copying bytes using streams. (They take better advantage of native code and hardware optimizations, and bypass the JVM for a lot of the work.) But still not as fast as renameTo(), so try that first.
 
sravan reddy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic