繁体   English   中英

在Ubuntu中使用Java创建文件夹和文件

[英]Create a folder and a file with Java in Ubuntu

这是我想做的:

  1. 检查文件夹是否存在
  2. 如果不存在,请创建文件夹
  3. 如果不存在,则不执行任何操作
  4. 最后在该文件夹中创建一个文件

在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 

您的目录(D:\\ temp)在Linux上不适用。

请考虑使用linux File System和File.SEPARATOR常量:

static String OS = System.getProperty("OS.name").toLowerCase();
String root = "/tmp";

if (OS.indexOf("win") >= 0) {
    root="D:\\temp";
} else {
    root="/";
}

File folder = new File(ROOT + "dir1" + File.SEPARATOR + "dir2");

if (folder.exists() && folder.isDirectory()) {
} else {
    folder.mkdir();
}

没有尝试过,但是谁可以工作。

D:\\temp在linux系统中不存在(我的意思是它将它解释为好像是其他任何文件夹名称一样)

在Linux系统中,文件分隔符是/而不是\\ (对于Windows)

所以解决方案是:

File folder = new File("/tmp"); 

代替

File folder = new File("D:\\temp");

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);

在类似Unix的系统上,没有逻辑磁盘。 您可以尝试在/tmp/home创建以下代码,以在您的主目录中创建temp目录:

String myPathCandidate = System.getProperty("os.name").equals("Linux")? System.getProperty("user.home"):"D:\\";
  System.out.println(myPathCandidate);
  //Check write permissions
  File folder = new File(myPathCandidate);
  if (folder.exists() && folder.isDirectory() && folder.canWrite()) {
      System.out.println("Create directory here");
  } else {System.out.println("Wrong path");}

或者,对于/tmp tmp-系统临时目录。 大多数用户可以在这里写:

String myPathCandidate = System.getProperty("os.name").equals("Linux")? System.getProperty("java.io.tmpdir"):"D:\\";

在Java 7之前,File API可以利用操作系统配置(例如RAM磁盘上的临时文件)创建临时文件。 由于Java 7使用实用程序函数类Files

使用System类的getProperty静态方法考虑两种解决方案。

String os = System.getProperty("os.name");

if(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") > 0 ) // Unix
    File folder = new File("/home/tmp"); 
else if(os.indexOf("win") >= 0) // Windows
    File folder = new File("D:\\temp");
else
    throw Exception("your message");

从Java 7开始,您可以将Files实用程序类与新的Path类一起使用。 请注意,以下示例中省略了异常处理。

// uses os separator for path/to/folder.
Path file = Paths.get("path","to","file");

// this creates directories in case they don't exist
Files.createDirectories(file.getParent());

if (!Files.exists(file)) {
    Files.createFile(file);
}

StreamResult result = new StreamResult(file.toFile());
transformer.transform(source, result);

这涵盖了一般情况,如果不存在则创建一个文件夹,并在该文件夹上创建一个文件。


如果您确实要创建一个临时文件(如示例中所示),则只需执行以下操作:

// this create a temporary file on the system's default temp folder.
Path tempFile = Files.createTempFile("xxx", "xml");

StreamResult result = new StreamResult(Files.newOutputStream(file, CREATE, APPEND, DELETE_ON_CLOSE));
transformer.transform(source, result);

请注意,使用这种方法,文件名将与您使用的前缀不完全对应(在本例中为xxx )。

尽管如此,由于它是一个临时文件,所以这根本不重要。 DELETE_ON_CLOSE保证关闭文件时将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM