个人觉得你最好把文件夹通过系统属性传入:
java -Dfolder=C:\PDFMalwareDataAnalyser\Txt\ myapp
这个你可以这样使用:
public File folderTxt = new File(System.getProperty("folderTxt"));
或者通过程序参数传递文件夹:
java myapp C:\PDFMalwareDataAnalyser\Txt\
然后像这样使用它:
public File folderTxt = new File(args[1]);
但如果你坚持在你的代码中使用常量,你可以这样使用它:
public File folderTxt = new File(String.join(File.separator,"C:","PDFMalwareDataAnalyser","Txt"));
或者更新版本是:
public File folderTxt = new File(Paths.get("C:","PDFMalwareDataAnalyser","Txt").toString());
或者您也可以读取一个属性文件并将路径存储在该文件中。这适用于不同的操作系统,您只需在之前创建属性文件:
String propertyFileName = String.format("folderSettings-%s.properties", System.getProperty("os.name").replaceAll(" ","_"));
Properties p = new Properties();
try (InputStream is = getClass().getResourceAsStream(propertyFileName))
{
p.load(is);
String folderName = p.getProperty("folderName");
folderTxt = new File(folderName);
}
catch (Exception e)
{
// log if error occurs...
e.printStackTrace();
}