• Post Reply Bookmark Topic Watch Topic
  • New Topic

Randomizer-Copier Program  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I started learning Java about a year ago and then I lost my enthusiasm and stopped. So now, because I want to force myself to learn again, I want to create a simple program, to refresh the basics.

What I want to create is a (simple?) program with swing GUI. You would point a program to some folder with more subfolders, and within these subfolder would be music/MP3 files. The program would go through all files, and create a list of MP3s, found inside subfolders. Then you would have "RANDOM" button, that would select random files, but it would only select as much files, as you would want it to. For example, somwhere in program you would select the drive letter of the USB stick that is put in computer, and the program would examine how much free space does this drive have, and set the "SIZE" value to the value of free space. Then you could change this value to something smaller, and the when you would press the RANDOM button, the program would randomly select so much files that total size wouldn't exceed selected SIZE value. Then there would be "COPY" button that would copy selected files to your selected USB drive.

So the first problem apperas, right at the begginig. I don't know how to start. How can I make my program "communicate" with USB drive (to read free space, copy files,...)?

Thanks for your help .

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can't do all that lot at once. You have to divide it into small pieces. Start by working out how to open files. And do it without a GUI.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Majstr Miha wrote:So the first problem apperas, right at the begginig. I don't know how to start. How can I make my program "communicate" with USB drive (to read free space, copy files,...)?

I'm not sure, but I suspect this is a "non-problem". If your Operating system can see it, why wouldn't Java?

Not being a GUI expert, I can't be sure; but my first port of call would be JFileChooser.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a file chooser without a GUI. Works nicely, in fact.
 
Majstr Miha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that is really fast response.

Sure, I know I can't do all that at once. This is just the whole program (more or less) that I want to build, and I am going step by step.

Ok, I am going to try file chooser.

 
Majstr Miha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, after long time. I haven't had a lot of time to do this, but now I came up with this:
- I can select source and destination folders (and I get path in String);
- I can scan a folder for files, but I can't figure out how to scan subfolders, and how to list only .mp3 files for example?
Here is the method for scaning my selected folder:


Help please ;)
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have all the pieces that you need already. You know how to get the list of files in a folder, and you know how to find out whether one of those files is a directory. You have that working already. Now you take the directory you found and apply that same process to it. It might be easier if you wrote a method which did that, rather than just a code fragment. Then you could call the method, passing it a directory as a parameter.

As for how to process only MP3 files, wouldn't that be the same as processing all files whose names end with ".mp3"? You know how to get the name of a file, and there's a method in the String class which lets you find if a String ends with a particular substring -- check out the API documentation and you'll find it explained there.
 
Majstr Miha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, right. I should just run this method for every subfolder I find ;)....doing that right now.

The method is endsWith() I believe?


EDIT:So for now I came up with this:


and this is part of the code that executes when I press the button:



But this doesn't scan subfolders. Where did I go wrong?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Majstr Miha wrote:Oh, right. I should just run this method for every subfolder I find ;)....doing that right now.

The method is endsWith() I believe?

There is a better way to do this. Have a look at the overloaded versions of the File.listFiles() method.
 
Majstr Miha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still haven't figured uot the problem. Any tips?

Joanne Neal wrote:
Majstr Miha wrote:Oh, right. I should just run this method for every subfolder I find ;)....doing that right now.

The method is endsWith() I believe?

There is a better way to do this. Have a look at the overloaded versions of the File.listFiles() method.


Sorry, almost total begginer here,so I would apreciate en example....I can't seem to find anything like that on Google?

Thanks
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne meant you should look here for listFiles, and look at all three versions.
 
Majstr Miha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have looked there but I don't know how that could help me? I already have a way to scan a folder for files and crate a list of MP3s, but I can't figure out how to make the same code run throught all subfolders, that it finds?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Majstr Miha wrote:Yes, I have looked there but I don't know how that could help me? I already have a way to scan a folder for files and crate a list of MP3s

Yes. And while there's nothing inherently wrong with how you've done it, the standard object-oriented way to search for specific files is to use the File.listFiles methods that accept either a FileFilter or FilenameFilter instance as a parameter.

Majstr Miha wrote:but I can't figure out how to make the same code run throught all subfolders, that it finds?

Well one way is to write a method that accepts a directory as a parameter, searches through that directory, procesing any files it finds however it needs to and calls itself recursively for any sub-directories it finds.
 
Majstr Miha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Majstr Miha wrote:Yes, I have looked there but I don't know how that could help me? I already have a way to scan a folder for files and crate a list of MP3s

Yes. And while there's nothing inherently wrong with how you've done it, the standard object-oriented way to search for specific files is to use the File.listFiles methods that accept either a FileFilter or FilenameFilter instance as a parameter.

Ah, ok. Thanks for the advice, but for now I would just like to make the program work, and then do it all over again, while using more standard way ;)

Joanne Neal wrote:
Majstr Miha wrote:but I can't figure out how to make the same code run throught all subfolders, that it finds?

Well one way is to write a method that accepts a directory as a parameter, searches through that directory, procesing any files it finds however it needs to and calls itself recursively for any sub-directories it finds.


But isn't this just what I have done? I have a method called "Iskanje" that accepts directory as parameter, and then with for loop I try to use this method on every subfolder that is found the first time when method executes. But it only gives me a list of mp3's in first folder, and lists a subfolder in that folder, but it doesn't search further through that subfolder?
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Majstr Miha wrote:But isn't this just what I have done? I have a method called "Iskanje" that accepts directory as parameter, and then with for loop I try to use this method on every subfolder that is found the first time when method executes. But it only gives me a list of mp3's in first folder, and lists a subfolder in that folder, but it doesn't search further through that subfolder?


Well, actually your Iskanje method takes a String as its parameter, not a directory. I would suggest changing it to take a File object instead, which would represent the directory much better than a String would.

And then inside the Iskanje method your code encounters some directories; don't you want to search them as well? Right now your code doesn't do that. (By the way Joanne used the word "recursively" -- do you know what that means?)
 
Majstr Miha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, will try to fix that.

Well, not actualy i don't....googling for explanation right now

EDIT: I got it to work....instead of using for loop later...I just called method Iskanje in case if listOfFile.isDirectory().....jupieee now I just have to thing about how exactly does this work
 
Majstr Miha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, now I have a list of MP3s from all folder is ArrayList<File>, but now, I don't know how to pass one of this values to Copy method correctly?

 
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Majstr Miha wrote:
I don't know how to pass one of this values to Copy method correctly?


Do you know what directory you want to copy the File to? If not, you can use a JFileChooser again. The first argument should be the MP3 File that you want to copy to the destination directory. In order to get a File instance of the destination directory for the second argument, you can use new File("path\\to\\destination\\song.mp3").
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!