【问题标题】:Telling .jar file to make a file on the Desktop instead of within itself (java) (mac)告诉 .jar 文件在桌面上而不是在其自身内创建文件 (java) (mac)
【发布时间】:2016-07-10 03:20:46
【问题描述】:

我有一个包含以下代码行的程序:

//String s = fileName;
File location = new File(s);

然后我有

//String contents = whatIWantToPrint;
OutputStream print = new FileOutputStream(location);
    print.write(contents.getBytes());
    print.close();

告诉程序将字符串内容打印到文件 s。但是,该文件出现在程序所在的文件夹中,而不是程序所在的目录中。例如,如果程序在个人桌面上,我希望它在他们的桌面上制作文件。

我在终端中使用以下行将程序制作为 jar 文件(mac):

jar -cvmf manifest.txt Generate.jar *.class

【问题讨论】:

    标签: java file path outputstream


    【解决方案1】:

    您应该考虑使用user.dir Java 系统属性。其含义解释为here。所以你的代码看起来像:

    String s = System.getProperty('user.dir');
    File location = new File(s);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多