How to read file from relative path in Java projec

2019-01-02 15:01发布

I have a project with 2 packages:

  1. tkorg.idrs.core.searchengines
  2. tkorg.idrs.core.searchengines

In package (2) I have a text file ListStopWords.txt, in package (1) I have a class FileLoadder. Here is code in FileLoader:

File file = new File("properties\\files\\ListStopWords.txt");

But have this error:

The system cannot find the path specified

Can you give a solution to fix it? Thanks.

标签: java file
9条回答
余生无你
2楼-- · 2019-01-02 15:12

If you are trying to call getClass() from Static method or static block the you can do the following way.

You can call getClass() on the Properties object you are loading into.

public static Properties pathProperties = null;

static { 
    pathProperties = new Properties();
    String pathPropertiesFile = "/file.xml;
    InputStream paths = pathProperties.getClass().getResourceAsStream(pathPropertiesFile);
}
查看更多
姐姐魅力值爆表
3楼-- · 2019-01-02 15:17
    InputStream in = FileLoader.class.getResourceAsStream("<relative path from this class to the file to be read>");
    try {
        BufferedReader reader=new BufferedReader(new InputStreamReader(in));
        String line=null;
            while((line=reader.readLine())!=null){
                System.out.println(line);
            }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
查看更多
荒废的爱情
4楼-- · 2019-01-02 15:19

I wanted to parse 'command.json' inside src/main//js/Simulator.java. For that I copied json file in src folder and gave the absolute path like this :

Object obj  = parser.parse(new FileReader("./src/command.json"));
查看更多
其实,你不懂
5楼-- · 2019-01-02 15:21

The following line can be used if we want to specify the relative path of the file.

File file = new File("./properties/files/ListStopWords.txt");  
查看更多
流年柔荑漫光年
6楼-- · 2019-01-02 15:21

While the answer provided by BalusC works for this case, it will break when the file path contains spaces because in a URL, these are being converted to %20 which is not a valid file name. If you construct the File object using a URI rather than a String, whitespaces will be handled correctly:

URL url = getClass().getResource("ListStopWords.txt");
File file = new File(url.toURI());
查看更多
姐姐魅力值爆表
7楼-- · 2019-01-02 15:29

The relative path works in in java using the . operator.

  • . means same folder as the currently running context.
  • .. means the parent folder of the currently running context.

So the question is how do you know the path where the java is currently looking?

do a small experiment

   File directory = new File("./");
   System.out.println(directory.getAbsolutePath());

Observe the output , you will come to know the current directory where java is looking . From there , simply use the ./ operator to locate your file.

for example if the output is

G:\JAVA8Ws\MyProject\content.

and your file is present in the folder MyProject simply use

File resourceFile = new File("../myFile.txt");

Hope this helps

查看更多
登录 后发表回答