• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help With File Class  RSS feed

 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList of File objects. I created them using the syntax:
File file = new File(String path, String filename);

I need to create a directory named for each file, and copy each file into the directory named for it. I've tried the API, but its a little vague on mkdir() and renameTo(). Can anybody help me find the code I need to do this?
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code will allow you to create a directory, no need to use the rename directory method: renameTo, since you can set the name of the directory directly.

 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this code will allow you to create a directory, no need to use the rename directory method: renameTo, since you can set the name of the directory directly.


good luck
Bili
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, for the code:



How do I reference the newly created directory? Am I using the renameTo() method correctly?
[ November 18, 2005: Message edited by: Greg Roberts ]
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I'm trying


A main destination directory has already been set ("destination") but I need to create a sub-directory for each file, and drop each file in itw own sub.
[ November 18, 2005: Message edited by: Greg Roberts ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you try to create a file with path: destination + f.getAbsolutePath()
print out that string. It doesn't make sense to use an absolute path like that.
Also, check that mkdir succeeds before you try to populate that directory.
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. I fixed the earlier method that sets the path for each file, now I'm going with:


The call to System.out.println() shows me that the paths for the files are correct. Its still not making any directories, which means its not copying files either.
[ November 18, 2005: Message edited by: Greg Roberts ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. This is a Java Greenhorn thing, but you should not compare strings with ==
or !=, because that is checking if they are the *same* string, versus
being *equal* strings (contains the same list of characters), So:


2. A file is identified by its absolute path. So there can't be two
files with the same absolute path: You can't have two files named
C:/foo/bar.txt, right. But look at this:

Hmmm, are you thinking what I'm thinking? f and dir has the same
absolute path?

3. Remember that both mkdir and renameTo can fail, so unless you don't
care, you should be checking their success. Especially since you are
trying to move a file into that new directory!
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, every file in the destFiles ArrayList has a path associated with it. For each file, I need to create that directory, and move the file to it. I was thinking if I created dir as a path file, I could call mkdir() from it to create the directory, then move "f" to the new directory that was created just for it.



The System.out.println() prints the correct path that the file needs to be copied to. I'm creating the file "dir" for the sole purpose of creating the directory. Do I not need this file? Can I create the directory from "f" itself?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!