1

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

2
  • Start with Basic I/O tutorial. Commented Oct 30, 2013 at 18:15
  • 4
    Please don't give negative votes to person who has shown what he did till now. Commented Oct 30, 2013 at 18:19

4 Answers 4

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Here's my new code: public class mainClass { File appdata = new File(System.getenv("APPDATA")); File minecraft = new File(appdata, ".minecraft"); if (minecraft.exists()) { System.out.println("Success"); } } But it is giving my this error: **Error: Main method not found in class mainClass, please define the main method as: public static void main(String[] args) **
1

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());
}

Comments

0

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.

Comments

0

%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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.