这是我想做的:
- 检查文件夹是否存在
- 如果不存在,创建文件夹
- 如果它不存在什么都不做
- 最后在该文件夹中创建一个文件
在 Windows 7 中一切正常,但当我在 Ubuntu 中运行应用程序时,它不会创建文件夹,它只是创建文件夹名称的文件,例如:(我的文件名为 xxx.xml 和文件夹是 d:\temp,所以在 Ubuntu 中,文件在 d: 生成,名称为 temp\xxx.xml)。这是我的代码:
File folder = new File("D:\\temp");
if (folder.exists() && folder.isDirectory()) {
} else {
folder.mkdir();
}
String filePath = folder + File.separator;
File file = new File(filePath + "xxx.xml");
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
// more code here
最佳答案
Linux 不使用盘符(如 D:),而是使用正斜杠作为文件分隔符。
你可以这样做:
File folder = new File("/path/name/of/the/folder");
folder.mkdirs(); // this will also create parent directories if necessary
File file = new File(folder, "filename");
StreamResult result = new StreamResult(file);
关于java - 在 Ubuntu 中使用 Java 创建一个文件夹和一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066413/