本地D盘:
package com.demo.other;
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String args[]) throws IOException {
java.io.File file = new java.io.File("D:\\root", "b.txt");
System.out.println("判断文件或目录是否存在:"+file.exists());
System.out.println("判断是否是文件:"+file.isFile());
System.out.println("判断是否是目录:"+file.isDirectory());
System.out.println("返回文件名或目录名:"+file.getName());
System.out.println("返回文件或目录的路径:"+file.getPath());
System.out.println("获取文件的长度:"+file.length());
java.io.File root = new java.io.File("D:\\root");
String[] list = root.list();
System.out.println("将目录中所有文件名保存在字符串数组中返回:");
for (String str:list){
System.out.println(str);
}
File dir = new File("D:\\root1");
if (!dir.exists()){
//创建目录
dir.mkdir( );
}
java.io.File aa = new java.io.File("D:\\root", "aa.txt");
if (!aa.exists()){
//创建文件
aa.createNewFile( );
}
}
}
打印结果: