I am playing a bit with the new Java 7 IO features, actually I trying to receive all the xml files of a folder. But this throws an exception when the folder does not exist, how can I check if the folder exists with the new IO?
我正在使用新的Java 7 IO特性,实际上我尝试接收一个文件夹的所有xml文件。但是当文件夹不存在时,这会抛出异常,我如何检查这个文件夹是否与新的IO一起存在?
public UpdateHandler(String release) {
log.info("searching for configuration files in folder " + release);
Path releaseFolder = Paths.get(release);
try(DirectoryStream<Path> stream = Files.newDirectoryStream(releaseFolder, "*.xml")){
for (Path entry: stream){
log.info("working on file " + entry.getFileName());
}
}
catch (IOException e){
log.error("error while retrieving update configuration files " + e.getMessage());
}
}
9 个解决方案
#1
174
Using java.nio.file.Files
:
使用java.nio.file.Files:
Path path = ...;
if (Files.exists(path)) {
// ...
}
You can optionally pass this method LinkOption
values:
您可以选择传递此方法LinkOption值:
if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
There's also a method notExists
:
还有一种方法不存在:
if (Files.notExists(path)) {
#2
157
Quite simple:
很简单:
new File("/Path/To/File/or/Directory").exists();
And if you want to be certain it is a directory:
如果你想确定它是一个目录:
File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
...
}
#3
39
To check if a directory exists with the new IO:
检查一个目录是否存在新的IO:
if (Files.isDirectory(Paths.get("directory"))) {
...
}
isDirectory
returns true
if the file is a directory; false
if the file does not exist, is not a directory, or it cannot be determined if the file is a directory or not.
如果文件是目录,则isDirectory返回true;如果该文件不存在,不是目录,或者无法确定该文件是否为目录,则为false。
See: documentation.
看:文档。
#4
4
You need to transform your Path into a File
and test for existence:
您需要将路径转换为文件并测试是否存在:
for(Path entry: stream){
if(entry.toFile().exists()){
log.info("working on file " + entry.getFileName());
}
}
#5
3
Generate a file from the string of your folder directory
从文件夹目录的字符串生成一个文件
String path="Folder directory";
File file = new File(path);
and use method exist.
If you want to generate the folder you sould use mkdir()
和使用方法存在。如果您想要生成文件夹,您可以使用mkdir()
if (!file.exists()) {
System.out.print("No Folder");
file.mkdir();
System.out.print("Folder created");
}
#6
2
File sourceLoc=new File("/a/b/c/folderName");
boolean isFolderExisted=false;
sourceLoc.exists()==true?sourceLoc.isDirectory()==true?isFolderExisted=true:isFolderExisted=false:isFolderExisted=false;
#7
2
There is no need to separately call the exists()
method, as isDirectory()
implicitly checks whether the directory exists or not.
不需要单独调用exist()方法,因为isDirectory()隐式地检查目录是否存在。
#8
0
We can check files and thire Folders.
我们可以检查文件和文件夹。
import java.io.*;
public class fileCheck
{
public static void main(String arg[])
{
File f = new File("C:/AMD");
if (f.exists() && f.isDirectory()) {
System.out.println("Exists");
//if the file is present then it will show the msg
}
else{
System.out.println("NOT Exists");
//if the file is Not present then it will show the msg
}
}
}
#9
0
import java.io.File;
import java.nio.file.Paths;
public class Test
{
public static void main(String[] args)
{
File file = new File("C:\\Temp");
System.out.println("File Folder Exist" + isFileDirectoryExists(file));
System.out.println("Directory Exists" + isDirectoryExists("C:\\Temp"));
}
public static boolean isFileDirectoryExists(File file)
{
if (file.exists())
{
return true;
}
return false;
}
public static boolean isDirectoryExists(String directoryPath)
{
if (!Paths.get(directoryPath).toFile().isDirectory())
{
return false;
}
return true;
}
}
#1
174
Using java.nio.file.Files
:
使用java.nio.file.Files:
Path path = ...;
if (Files.exists(path)) {
// ...
}
You can optionally pass this method LinkOption
values:
您可以选择传递此方法LinkOption值:
if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
There's also a method notExists
:
还有一种方法不存在:
if (Files.notExists(path)) {
#2
157
Quite simple:
很简单:
new File("/Path/To/File/or/Directory").exists();
And if you want to be certain it is a directory:
如果你想确定它是一个目录:
File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
...
}
#3
39
To check if a directory exists with the new IO:
检查一个目录是否存在新的IO:
if (Files.isDirectory(Paths.get("directory"))) {
...
}
isDirectory
returns true
if the file is a directory; false
if the file does not exist, is not a directory, or it cannot be determined if the file is a directory or not.
如果文件是目录,则isDirectory返回true;如果该文件不存在,不是目录,或者无法确定该文件是否为目录,则为false。
See: documentation.
看:文档。
#4
4
You need to transform your Path into a File
and test for existence:
您需要将路径转换为文件并测试是否存在:
for(Path entry: stream){
if(entry.toFile().exists()){
log.info("working on file " + entry.getFileName());
}
}
#5
3
Generate a file from the string of your folder directory
从文件夹目录的字符串生成一个文件
String path="Folder directory";
File file = new File(path);
and use method exist.
If you want to generate the folder you sould use mkdir()
和使用方法存在。如果您想要生成文件夹,您可以使用mkdir()
if (!file.exists()) {
System.out.print("No Folder");
file.mkdir();
System.out.print("Folder created");
}
#6
2
File sourceLoc=new File("/a/b/c/folderName");
boolean isFolderExisted=false;
sourceLoc.exists()==true?sourceLoc.isDirectory()==true?isFolderExisted=true:isFolderExisted=false:isFolderExisted=false;
#7
2
There is no need to separately call the exists()
method, as isDirectory()
implicitly checks whether the directory exists or not.
不需要单独调用exist()方法,因为isDirectory()隐式地检查目录是否存在。
#8
0
We can check files and thire Folders.
我们可以检查文件和文件夹。
import java.io.*;
public class fileCheck
{
public static void main(String arg[])
{
File f = new File("C:/AMD");
if (f.exists() && f.isDirectory()) {
System.out.println("Exists");
//if the file is present then it will show the msg
}
else{
System.out.println("NOT Exists");
//if the file is Not present then it will show the msg
}
}
}
#9
0
import java.io.File;
import java.nio.file.Paths;
public class Test
{
public static void main(String[] args)
{
File file = new File("C:\\Temp");
System.out.println("File Folder Exist" + isFileDirectoryExists(file));
System.out.println("Directory Exists" + isDirectoryExists("C:\\Temp"));
}
public static boolean isFileDirectoryExists(File file)
{
if (file.exists())
{
return true;
}
return false;
}
public static boolean isDirectoryExists(String directoryPath)
{
if (!Paths.get(directoryPath).toFile().isDirectory())
{
return false;
}
return true;
}
}