• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question: Moving folder(s)  RSS feed

 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.
I'm currently in the process of trying to write my very first Java program. I will try and explain as much as I can, but not entirely to what the program will do as it's pretty irrelevant. A basic way to say what the program will do is that it will take a batch of folders, cut it, and paste it based on the directory that the user selected. Obviously there are some more features it will do but this is just what I'm currently stuck on. I'm currently lost on trying to figure out how to simply cut folders and all contents within them using Java. I've been searching the internet and all I found was how to create a new folder, but it's not exactly what I was looking for. Here is the code I'm working on within the method:



FoldersToMoveList is a file array that is filled within another method. You can ignore line 3 as it was just something else I was testing/messing around with. The full path names for the folders I want to copy are in an ArrayList called foldersToMoveList. The folder name is in the foldersToMovePane itself. I'm not entirely sure how it's done but the program so far can identify the folders to cut, where it wants to cut it too, now all I need to figure out is just how to cut them. This is my first time actually posting on this forum so I apologize if I'm missing information I may have overlooked. I have just started using Java not that long ago so I'm definitely no expert, just trying to learn as much as I can. Please and thank you for the help! :)
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of ways (order of best-choice-first):

1) The java.nio.file.Files class has some utilities that could help. for example the move() method.
2) Failing that, you could use the java.io.File renameTo() method
3) Finally, you could create the new directories, make new files, read in the old files, copy bytes to the new files, and delete the old files.

 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, the move() method does sound exactly what I'm looking for, but how do I implement it exactly? I tried to import it with the following:



But I get the error that package java.nio.file.Files does not exist.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read the API for java.nio.file.Files, what does it tell you about when it was implemented? What version of Java are you using?
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, this is all so new to me. Where can I read the API for java.nio.file.Files? I can't implement the import statement because I get this error that it doesn't exist. If I run the file it says it's compiled with errors, with the only error being the import statement doesn't exist. According to my Programs list, it says: Java(TM) 6 Update 31 - Version 6.0.310, Java(TM) SE Development Kit 5 Update 31 - Version 1.6.0.310. Hope that helps.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darnell Day wrote:I'm sorry, this is all so new to me. Where can I read the API for java.nio.file.Files?


You see how the name java.nio.file.Files is underlined? That is one of the cool features of this forum - it is a link to the API. just click on it and you will go to the API Also, for future reference, you should have this link permanently stored in you browser bookmarks: http://docs.oracle.com/javase/7/docs/api/ (or for Java 6: http://docs.oracle.com/javase/6/docs/api/). The API is an invaluable resource.


I can't implement the import statement because I get this error that it doesn't exist. If I run the file it says it's compiled with errors, with the only error being the import statement doesn't exist. According to my Programs list, it says: Java(TM) 6 Update 31 - Version 6.0.310, Java(TM) SE Development Kit 5 Update 31 - Version 1.6.0.310. Hope that helps.


It does. I could answer but I want to give you some time to look at the API. Even for something like this it is a valuable exercise.
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This all does look very interesting, but judging that there is a Standard Ed 6 & 7, I'm guessing my Java is severely outdated and I need to upgrade it. Which is why I'm getting the "Package not found" error. As I'm browsing through the Java 7 API, I see a CopyOption Method from the java.nio.file package. Can I use that as well? Or is it the same thing? Also I would now like to upgrade my Java to Java 7, is there anything I should know before doing so? As in, should I uninstall Java first, and also uninstall NetBeans as well before upgrading? The last time I believe I tried to upgrade my Java, my Firefox was having some capability issues to the point it wouldn't even load. That's why I'm a bit reluctant to do the upgrade so hastily without ensuring that I'm actually doing it the proper way.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darnell Day wrote:This all does look very interesting, but judging that there is a Standard Ed 6 & 7, I'm guessing my Java is severely outdated
Outdated, yeah. Severely so? Not really (at least not in my mind).
and I need to upgrade it. Which is why I'm getting the "Package not found" error. As I'm browsing through the Java 7 API, I see a CopyOption Method from the java.nio.file package. Can I use that as well? Or is it the same thing?
As you can see from the API: CopyOption is an interface and implementations of the interface can be passed to the Files.move() method to define options for the move. So, the CopyOption isn't an action to use instead of Files.move() is is a configuration to use with Files.move().
Also I would now like to upgrade my Java to Java 7, is there anything I should know before doing so? As in, should I uninstall Java first, and also uninstall NetBeans as well before upgrading? The last time I believe I tried to upgrade my Java, my Firefox was having some capability issues to the point it wouldn't even load. That's why I'm a bit reluctant to do the upgrade so hastily without ensuring that I'm actually doing it the proper way.
Read up on the release notes for Java 7 to see if anything strikes your fancy. Perhaps someone else here can provide info about NetBeans and Firefox. I haven't used either in a long time.
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, now I upgraded to the new Java7. One thing I had to do was to restart after installing, uninstall Java 6, restart again just to be sure and thankfully everything was still working fine. I switched my project to run on JDK7 now and the import statement for java.nio.file.Files is no longer giving me problems. Now I don't mean to be a pickle, but I'm still having trouble trying to access the method :P I have some code like this:



I go through the list after the "." and I don't see any move(). I try selecting different things like selecting .toString() to see what I can select after that, but still no move(). In fact I couldn't find any of the methods that are listed from the API.

EDIT: Nevermind I found them I didn't realize I had to press "CTRL + Space" to see all items. I'll see what I can do with this method.

EDIT: Just to update, I do have the move() method working (not as much as I want it to do so I still have some coding left to figure out) but nonetheless I basically found what I was looking for. Which was just a way to move a folder from one destination to another. Here's the code I used for it:



But before the thread is closed I just have one more question if you don't mind. I'm almost complete with the program I'm working on, might be finished in another two days or so. But one thing I'm concerned with is when I finish my program, how do I actually turn it into a fully functional ".exe" application program? I've been looking for like what I might need to use to turn this project into a real program in Netbeans, but I have no idea where I'm supposed to go.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!