R
R
roman38472014-04-20 14:01:03
Java
roman3847, 2014-04-20 14:01:03

How to specify the path to a file?

I am creating a project. In the project folder I create a folder resources. In this folder I throw the file test. How can I access this file without specifying the full path in the root system, but starting only from the project folder?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kharitonov, 2014-04-20
@geakstr

The construction
will give the full path to the current class file. You can use something like

public class Main {
  void run() {
    String testFilePath = getResourcePath("test");
    System.out.println(testFilePath);
  }

  String getResourcePath(String file) {
    return getClass().getResource("/").getPath() + "../resourses/" + file;
  }

  public static void main(String[] args) {
    new Main().run();
  }

}

Or just use
getResourceAsStream()

R
rustelecom, 2014-04-20
@rustelecom

What are you applying from? From a script, program. O/S what?

C
CloudMonster, 2014-04-20
@CloudMonster

You can do this:
in index.php

define ('RESOURSES', 'path/to/');
require_once(RESOURSES. "test.php");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question