Filef=newFile("d:/aaa");
if(f.isDirectory()){//判断是不是目录
File[]F1=f.listFiles();//获取目录下文件列表
for(Filef2:F1){//循环文件列表
if(f2.isDirectory()){//判断是不是目录
}else{//是文件
}
}
}
‘贰’ java怎么判断文件夹里面是否还有文件夹
File file =new File("C:\\Users\\QPING\\Desktop\\JavaScript");
//如果文件夹不存在则创建
if (!file .exists() && !file .isDirectory())
{
System.out.println("//不存在");
file .mkdir();
} else
{
System.out.println("//目录存在");
}
‘叁’ java判断目录下是否有文件夹
File file =new File("C:\\Users\\QPING\\Desktop\\JavaScript");
//如果文件夹不存在则创建
if (!file .exists() && !file .isDirectory())
{
System.out.println("//不存在");
file .mkdir();
} else
{
System.out.println("//目录存在");
}
‘肆’ java 验证文件夹是否存在
用file.exists()就行吧
import java.io.*;
import java.util.*;
public class TestStream {
public static boolean isexists(String s){
File f = new File(s);
if(f.exists())
return true;
else
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入要查询的文件夹路径:");
String str = in.nextLine();
if(isexists(str))
System.out.println("此文件夹存在");
else
System.out.println("此文件夹不存在");
}
}
如:
输入:E:\资料
输出:此文件夹存在
‘伍’ java判断文件是否存在
java判断文件是否存在:
1、判断文件是否存在,不存在创建文件
Filefile=newFile("C:\Users\QPING\Desktop\JavaScript\2.htm");
if(!file.exists())
{
try{
file.createNewFile();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
2、判断文件夹是否存在,不存在创建文件夹
Filefile=newFile("C:\Users\QPING\Desktop\JavaScript");
//如果文件夹不存在则创建
if(!file.exists()&&!file.isDirectory())
{
System.out.println("//不存在");
file.mkdir();
}else
{
System.out.println("//目录存在");
}
‘陆’ Java如何判断文件夹是否存在
方法如下:
public static void judeDirExists(File file)
if (file.exists()) if (file.isDirectory())
System.out.println("dir exists"); }
else System.out.println("the same name file exists, can not create dir"); }41
else System.out.println("dir not exists, create it ..."); 、
file.mkdir();
‘柒’ Java判断文件夹是否存在,不存在就创建
用File类中的.exists()方法判断是否存在
mkdirs创建目录
createNewFile()创建文件
多看看API文档
boolean
exists()
测试此抽象路径名表示的文件或目录是否存在。
createNewFile()
当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
boolean
mkdirs()
创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
‘捌’ Java中怎样根据文件的路径去判断该文件夹中是否存在该文件
1.File testFile = new File(testFilePath);
if(!testFile .exists()){
testFile.mkdirs();
System.out.println("测试文件夹不存在");
}
2.File testFile = new File(testFilePath);
if(!testFile .exists()){
testFile.createNewFile();
System.out.println("测试文件不存在");
}
java中File类自带一个检测方法exists可以判断文件或文件夹是否存在,一般与mkdirs方法(该方法相较于mkdir可以创建包括父级路径,推荐使用该方法)或者createNewFile方法合作使用。
1,如果路径不存在,就创建该路径
2,如果文件不存在,就新建该文件
‘玖’ java判断目录是否存在
public static File checkExist(String filepath) throws Exception{
File file=new File(filepath);
if (file.exists()) {//判断文件目录的存在
System.out.println("文件夹存在!");
if(file.isDirectory()){//判断文件的存在性
System.out.println("文件存在!");
}else{
file.createNewFile();//创建文件
System.out.println("文件不存在,创建文件成功!" );
}
}else {
System.out.println("文件夹不存在!");
File file2=new File(file.getParent());
file2.mkdirs();
System.out.println("创建文件夹成功!");
if(file.isDirectory()){
System.out.println("文件存在!");
}else{
file.createNewFile();//创建文件
System.out.println("文件不存在,创建文件成功!" );
}
}
return file;
}