Get file from project folder java

后端 未结 9 641
庸人自扰
庸人自扰 2020-12-24 06:56

I want to get File from project folder by using \"File class\" , How I can Do that ?

File file=new File(\"x1.txt\");
相关标签:
9条回答
  • 2020-12-24 07:29

    If you don't specify any path and put just the file (Just like you did), the default directory is always the one of your project (It's not inside the "src" folder. It's just inside the folder of your project).

    0 讨论(0)
  • 2020-12-24 07:30

    These lines worked in my case,

    ClassLoader classLoader = getClass().getClassLoader();
    File fi = new File(classLoader.getResource("test.txt").getFile());
    

    provided test.txt file inside src

    0 讨论(0)
  • 2020-12-24 07:36
    String path = System.getProperty("user.dir")+"/config.xml";
    File f=new File(path);
    
    0 讨论(0)
提交回复
热议问题