Can I point to project files this way?

Asked

Viewed 229 times

1

Working with PHP is very easy to set up a schematic path for some file, but in Java the scheme may be different?

I can point to a file starting, for example, by src File.separator?

Example:

private static final String MODELS_PATH = "src" + File.separator + "models" + File.separator;

I can retrieve the path absolute, but only if my way of pointing to a file above is correct.

Example:

File file = new File(".", MyClass.MODELS_PATH);

String absolutePath = file.getAbsolutePath();

The way to point to a file as in MODELS_PATH is correct?

  • 1

    I don’t know if I understand what you want. Do you want Eclipse to run this code to know how to build the full project? If this is it, I can tell you that Java is a serious language that does not allow PHP to do the same.Application code is application code, information about building the project is something totally isolated and is not part of the code.But if you are only talking about picking up any file during the execution of your application, then the way is more or less this.Clarify.

  • @Bigown Eclipse is just an IDE, I never think about doing anything for him but for a run time. Knowing how this handles the file path is my main concern. The only thing I want is to learn how to properly point to a file, thank you.

  • If the question is not related to the IDE, you should not mention it.

  • @bigown I mentioned why it was Eclipse who created the project’s folder system.

1 answer

1


I think this is what you want:

import java.io.*;

class Main {
    public static void main(String[] args) {
        File path = new File("src", "models");
        System.out.println(path.getPath());
        System.out.println(path.getAbsolutePath());
        File novoPath = new File(".", path.getPath());
        System.out.println(novoPath.getAbsolutePath());
        System.out.println(novoPath.getPath());
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thanks for the example, it changes the game!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.