wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Help with Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Help with Files" Watch "Help with Files" New topic
Author

Help with Files

Sree Potluri
Ranch Hand

Joined: Aug 12, 2009
Posts: 62
HI,

I have a file Structure C:\Files\java. In java I have 3 folders F1, F2, F3 and some files . In each of the folders there are zip files a.zip, b.zip, c.zip. I have to go into each folder and move the file to different place. Ex:C:\Moved.

Initial file Structure C:\Files\java\F1\a.zip, C:\Files\java\F2\b.zip,C:\Files\java\F3\c.zip
After moving: C:\Moved\a.zip, C:\Moved\b.zip, C:\Moved\c.zip

how do I achieve this?

Thanks in Advance.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hint: look at java.io.File.renameTo().


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Sree Potluri
Ranch Hand

Joined: Aug 12, 2009
Posts: 62
HI,

I do not want to rename the files, but move them from one place to another.

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4245
    
    7

Sree Potluri wrote:I do not want to rename the files, but move them from one place to another.

And the difference is?
Sree Potluri
Ranch Hand

Joined: Aug 12, 2009
Posts: 62
What do you mean by "And the difference is?" I am not understanding your question. I want to move files from one directory, where in if the path is the directory then go into the directory until there are no more directories, then take the file and put it in another location.

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Sree Potluri wrote:What do you mean by "And the difference is?" I am not understanding your question. I want to move files from one directory, where in if the path is the directory then go into the directory until there are no more directories, then take the file and put it in another location.
Take a look at the java.io.File.renameTo() documentation.
Sree Potluri
Ranch Hand

Joined: Aug 12, 2009
Posts: 62
I want to know how many files are there in F1 directory. If there are F11, F12.. inside F1, then it should go to each individual directory and move the file to C:\Moved folder


Vib Mator
Greenhorn

Joined: Oct 21, 2010
Posts: 6
make use of list method
it returns an array
so you can always check the length parameter

int num_of_file=new File(<directory path>).list().length ;
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

Sree Potluri wrote: I want to know how many files are there in F1 directory. If there are F11, F12.. inside F1, then it should go to each individual directory and move the file to C:\Moved folder



So, in this particular example you want to "rename" the original file from C:\Files\java\F1\a.zip to C:\Moved\a.zip. That isn't what you coded. Instead you tried to rename the original file from C:\Files\Java\F1\a.zip to C:\Moved, which is going to fail if that directory already exists.
Vib Mator
Greenhorn

Joined: Oct 21, 2010
Posts: 6



<File ref for file_to_be_moved>.renameTo(new File(<dest_folder File ref>,<File ref for file_to_be_moved >.getName()));

is what would be the right thing to do.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4245
    
    7

Sree Potluri wrote:What do you mean by "And the difference is?" I am not understanding your question.

I mean that conceptually, renaming a file and moving a file are the same thing. In each case you're changing the absolute path of the file. Compare Unix commands - whether you want to move or rename a file you still use the mv command.
Sree Potluri
Ranch Hand

Joined: Aug 12, 2009
Posts: 62
Mattew,

There can be more than one file in each directory that I need to move. So first I have to scan and check how many files are present before moving the files.

Thanks
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Just check the File documentation. It's all there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Files
 
Similar Threads
Files
OCP book (K & B) - Exam2 - Num 50 - GC question
What does a "one to many" relationship mean
Issue with float to binary
doubt in kathy sierra question?