从 new File() 中创建一个文件夹
make out of new File() a folder
我必须制作我目前不知道它们是否存在的文件对象。使用实际文件没问题:
File file = new File("path+filename"); //File does not get generated which is fine.
file.isDirectory() //is false :)
那么我该如何创建一个文件对象呢?
File file = new File("path+foldername");
file.isDirectory = true; //doesn't work oviously :(
使用的方法是.mdkir()
(或.mkdirs()
)。
但是您需要检查 return 代码,因为它们 return boolean
s...
但由于这是 2015 年,我假设您使用的是 Java 7+。因此,放弃 File
,忘记它,改用 java.nio.file:
final Path path = Paths.get("elements", "of", "path", "here");
Files.createDirectory(path);
Files.createDirectories(path);
让我们考虑以下代码。在这里,最初我们没有文件或目录。这就是为什么 exits(),isFile() 和 isDirectory() returns false .但是稍后当我们使用 mkdir() 创建目录时,isDirectory returns true 因为我们已经成功创建了一个目录。
File file = new File("d:\abc"); //This just creates a file object
System.out.println(file.exists()); //This will return false
System.out.println(file.isFile()); //This will return false
System.out.println(file.isDirectory()); //This will return false
file.mkdir(); //This will create a directory called abc
System.out.println(file.exists()); //This will return true because a directory exists
System.out.println(file.isFile()); //This will return false because we have created a directory called abc not a file
System.out.println(file.isDirectory());//This will return true because we have just created a directory called abc
编辑:
file.isDirectory()
只会return true存在文件夹(目录)时。因此,举例来说,如果您在以下位置 d:\sample 已经有一个名为 sample 的文件夹。现在,创建一个名为:
的文件对象
File file = new File("d:\sample");
如果你现在打电话给
file.isDirectory() //Returns true
它将 return 为真。因为文件对象指向一个有效且存在的文件夹。
我必须制作我目前不知道它们是否存在的文件对象。使用实际文件没问题:
File file = new File("path+filename"); //File does not get generated which is fine.
file.isDirectory() //is false :)
那么我该如何创建一个文件对象呢?
File file = new File("path+foldername");
file.isDirectory = true; //doesn't work oviously :(
使用的方法是.mdkir()
(或.mkdirs()
)。
但是您需要检查 return 代码,因为它们 return boolean
s...
但由于这是 2015 年,我假设您使用的是 Java 7+。因此,放弃 File
,忘记它,改用 java.nio.file:
final Path path = Paths.get("elements", "of", "path", "here");
Files.createDirectory(path);
Files.createDirectories(path);
让我们考虑以下代码。在这里,最初我们没有文件或目录。这就是为什么 exits(),isFile() 和 isDirectory() returns false .但是稍后当我们使用 mkdir() 创建目录时,isDirectory returns true 因为我们已经成功创建了一个目录。
File file = new File("d:\abc"); //This just creates a file object
System.out.println(file.exists()); //This will return false
System.out.println(file.isFile()); //This will return false
System.out.println(file.isDirectory()); //This will return false
file.mkdir(); //This will create a directory called abc
System.out.println(file.exists()); //This will return true because a directory exists
System.out.println(file.isFile()); //This will return false because we have created a directory called abc not a file
System.out.println(file.isDirectory());//This will return true because we have just created a directory called abc
编辑:
file.isDirectory()
只会return true存在文件夹(目录)时。因此,举例来说,如果您在以下位置 d:\sample 已经有一个名为 sample 的文件夹。现在,创建一个名为:
的文件对象File file = new File("d:\sample");
如果你现在打电话给
file.isDirectory() //Returns true
它将 return 为真。因为文件对象指向一个有效且存在的文件夹。