AwesomeGuy
AwesomeGuy

Reputation: 683

I want to find a value from an array

Here's my java code:

import java.io.File;
import java.util.Arrays;



public class mainClass {

    public static void main(String... args) {
        File[] files = new File("%appdata%").listFiles();
        showFiles(files);
        System.out.println( Arrays.toString( files ) );
        if (Arrays.asList(files).contains(".minecraft")) {
            System.out.println("Success!");
        }
    }

    public static void showFiles(File[] files) {
    }

}

I want code above to check if .minecraft folder exists in %appdata%. I am total N00B to Java. I have worked with PHP, but doesn't seem to help me :) Please help, it annoys me.

-Simon

Upvotes: 1

Views: 113

Answers (4)

A4L
A4L

Reputation: 17595

%appdata%is an environment variable and as such it will not be automatically resolved by File. So you need to resolve it before listing it. This is done using System#getenv

@Test
public void dirExistsInAppData() {
    Assert.assertTrue(dirExistsInAppData(".minecraft"));
}

private boolean dirExistsInAppData(final String dirname) {
    File dir = new File(System.getenv("APPDATA"), dirname);
    return dir.exists() && dir.isDirectory();
}

Upvotes: 0

rolfl
rolfl

Reputation: 17707

If you are interested in finding only the ".minecraft" file it would be much easier to:

File appdata = new File("%appdata%");
File minecraft = new File(appdata, ".minecraft");
if (minecraft.exists()) {
    System.out.println("Success");
}

EDIT: Based on comment, (and I'm a linux guy mostly), you need to use the correct %APPDATA% location: How do I get the value of Windows' %APPDATA% location variable in Java?

Upvotes: 2

bedwyr
bedwyr

Reputation: 5874

As rolfl mentioned, there is a better way to look for a single file.

That said, your code isn't performing a proper check. You are creating an array of File objects, converting the array to a List, and then checking the list for a String value. The String value will never match a File object.

If you want to find a single file, use rolfl's answer. If you want to fix your code specifically, here's something to get your started:

  • You need to iterate over the list of files. What do you gain by converting to a List?
  • You need to find a way to match a File's name with a String name. What method might you call on the File object to get its name?
  • You need to do a String comparison between the File's name and ".minecraft". What might that comparison look like?

Please note: reference L.Butz answer as well; I haven't accessed hidden files in Java, so it's possible there's an extra step you need to get access to them.

Upvotes: 0

L.Butz
L.Butz

Reputation: 2616

The problem is that .minecraftis a hidden folder. You need to access the folder like this:

File directory = new File("%appdata%");    
File[] hiddenFiles = directory.listFiles((FileFilter) HiddenFileFilter.HIDDEN);
for (File hiddenFile: hiddenFiles) {
    System.out.println("hidden file: " + hiddenFile.getCanonicalPath());
}

Upvotes: 1

Related Questions