Stragomagus Posted January 11, 2011 Share Posted January 11, 2011 I have this clock that I am making and was wondering if it was possible for java to randomly access any file, and open, that the user types in the pathing for. I am using this for the alarm portion of the clock so that an user can easily make a folder within one folder and then have the program open any music file located within it when the alarm goes off. example: Folder path was c:\desktop\blah - Desktop - blah - hig - file - file - fri - file - file - die - file - file Quote - Revenge is such a nasty thing that only breeds more vengeful souls, but in some situations revenge does not even need to be sought out, but only bided. Link to comment Share on other sites More sharing options...
Ivoos_ Posted January 11, 2011 Share Posted January 11, 2011 package tests; import java.io.*; import java.util.Random; public class Main { Random rg = new Random(); public static void main(String[] args) { File f = new File("C:\\"); Main main = new Main(); main.getRandomFile(f); } public File getRandomFile(File file) { File[] files = file.listFiles(); int number = rg.nextInt(files.length); if(files[number].isDirectory()){ System.out.println(files[number].toString()); return getRandomFile(files[number]); } else{ System.out.println(files[number].toString()); return files[number]; } } } Something I threw together in a few seconds, ignore the System outs :) http://www.tip.it/runescape/?herb^^ A herblore cost/xp calculator. Any feedback would be greatly appreciated. Cleaning herbs has the values switched at the moment, but the fix has been submitted. Link to comment Share on other sites More sharing options...
Stragomagus Posted January 12, 2011 Author Share Posted January 12, 2011 Thanks. This will be very useful when I need to do something like this again. Quote - Revenge is such a nasty thing that only breeds more vengeful souls, but in some situations revenge does not even need to be sought out, but only bided. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now