文件在Eclipse中的一些未知路径下保存

时间:2016-11-07 12:29:24

标签: java eclipse

我正在使用Eclipse并具有以下文件夹结构:

Eclipse Folder Structure

在UserHelper.java中,我有代码

try {
  File file = new File ("vikvik1.JSON");

  if (!file.exists ()) {

    System.out.println ("No file");
    file.createNewFile ();
    temp = true;
  }

  System.out.println (file.getAbsolutePath ());

} catch (IOException ioe) {
  System.out.println ("Exception occurred:");
  ioe.printStackTrace ();
}

但是在创建文件后,输出为C:\Users\a595649\Documents\Vikram Thakur\Soft\eclipse\vikvik1.JSON 这是我的Eclipse Exe文件存储的位置。

如何将此文件保存在我的项目DBnov文件夹下?

2 个答案:

答案 0 :(得分:0)

一种可能的解决方案:

  • 在类路径下创建一个属性文件,比如foo.properties

  • 放置像

    这样的属性
    basePath=/some/path/you/want/to/use
    
  • 阅读属性文件

    String basePath="";
    
    final Properties properties = new Properties();
    try (final InputStream stream =
       this.getClass().getResourceAsStream("foo.properties")) {
    
    properties.load(stream);
    basePath=properties.getProperty("basePath");
    }
    
  • 阅读您的文件

    File file = new File ("basePath","vikvik1.JSON");
    

答案 1 :(得分:0)

您必须在实例化File对象时提供文件的路径,否则java会默认选择它。 请参阅构造函数的javadoc,您可以使用不同的方式启动它,例如将路径作为String:

File file = new File ("your_path", "vikvik1.JSON");