0

Ok so for starters sorry if this question is phrased wrongly, im not quite sure how to put this. The general idea is id like to write a java program that can be given a letter and search for program folders in the windows program files that start with this letter than return them to you as text destinations. (For example, if i enter the letter J id like it to return something like: C:\Program Files\Java) In other words, is there a way to "set a destination" for your program to work in like in this case windows program files? Also, not just specifically for this program but id like to know if this is possible for future programs such as an aimbot for a game or a modification to another program without editing the actual source code. Thanks for helping, its greatly appreciated!

edit 1: To be more specific, im asking how you can actually target some folder in the OS such as program files\Java than have direct access to its contents such as file names, file types, and the ability to open/view these files.

Bob Mc Muffins.
  • 37
  • 2
  • 2
  • 11
  • Java is cross-platform, and `Program Files` is Windows specific. So I think the answer is probably no. – Elliott Frisch Jan 04 '15 at 07:19
  • To a look at the java.io.File class... – MadProgrammer Jan 04 '15 at 07:20
  • Ok, thanks MadProgrammer, didnt even know that existed, this could be the answer. Elliot you do have a good point but id think that they'd at least have some form of operating system specific support for files such as a class you can import or inherit from like the java.io.File class. – Bob Mc Muffins. Jan 04 '15 at 07:24

2 Answers2

1

There are two parts to your question:

  1. Setting the hard disk location: You can do that using using a string and passing that string as an argument to a File.

    String location = "C:\\Program Files";
    File directory = new File(location);
    

The directory can then be used to get the list of all files and folders within the specified location, as described below.

    String[] files = directory.list();
    for(String file : files){
        //Check if the sub-directory/file name starts with J
        if(file.startsWith("J")){
            System.out.println(file);   
            //Do something here
        }

    }
  1. Cross-platform compatibility: If you plan to run your program across platforms, and have a different hard disk location in each platform, then you can check for the OS, and set the value of location accordingly. You can refer to this link on how to do that: How do I programmatically determine operating system in Java?
Community
  • 1
  • 1
GC_25
  • 60
  • 1
  • 7
  • Ok thanks! I dont plan on running this on anything besides windows so i think ill be ok for the 2nd thing. – Bob Mc Muffins. Jan 04 '15 at 16:23
  • Ok i do have one problem with this when i try this, in eclipse it gives me and error and says that it expects an open bracket after directory.list. I think you forgot to import the class or something. If you know could you please tell me how – Bob Mc Muffins. Jan 04 '15 at 16:33
  • You can refer to a working sample below. Do Let me know if it doesn't work. `package gc25; import java.io.File; public class Test { public static void main(String args[]){ String location = "C:\\Program Files"; File directory = new File(location); String[] files = directory.list(); for(String file : files){ if(file.startsWith("J")){ System.out.println(file); //Do something here } } } }` – GC_25 Jan 05 '15 at 15:12
0

You can simpley get the list of directorys in the root of hard drive like this,

suppose the drive is C:

create a file Class as :

File dir=new File("c:/");

then by executing dir.list() method you can get array of strings containing names of the files and directories in the drive C.

finally you can simply manipulate the array to find the desired folder. Take a look for this example

for (int x=0;x<=dir.length,x++) {
if (file.isDirectory()) {
    //if it is directory you can recursively get the list of files inside untile you get your desired directory
  }
}
ambes
  • 5,272
  • 4
  • 26
  • 36