import java.io.File;
import java.io.IOException;
public class FileD {
public static void main(String[] args) throws IOException {
fun();
System.out.println();
fun1();
System.out.println();
fun2();
System.out.println();
fun3();
System.out.println();
fun4();
System.out.println();
fun5();
System.out.println();
fun6();
System.out.println();
fun9();
System.out.println();
fun10();
System.out.println();
fun11();
System.out.println();
fun12();
System.out.println();
fun13();
}
// file类的构造方式 三种重载形式
// 1传递路径名
public static void fun(){
File file=new File("d:\\java");
System.out.println(file);
}
// 2、传递字符串 父路径
public static void fun1(){
File file=new File("d:","java");
System.out.println(file);
}
// 3、 传递父路径 字符串路径
public static void fun2(){
File parent =new File("D:");
File file=new File(parent,"*.File");
System.out.println(file);
}
// 获得 getParent() 获取父路径
public static void fun3(){
File file=new File("d://新建文件夹\\CallName");
String parent = file.getParent();
System.out.println(parent);
}
// 获取绝对路径 绝对位置的工程根目录
public static void fun4(){
File file=new File("src");
File Abs=file.getAbsoluteFile();
System.out.println(Abs);
}
// length() 获得文件的字节数
public static void fun5() {
File file=new File("D:\\java\\新建文件夹");
long length=file.length();
System.out.println(length);
}
// 获取功能 放回路经中表示的文件或者文件名 获得路径中的最后部 的名字
public static void fun6(){
File file=new File("D:\\java\\新建文件夹");
String name=file.getPath();
String name1=file.getName();
System.out.println(name);
System.out.println(name1);
}
// 判断封装的是不是文件夹
public static void fun7(){
File file=new File("d:\\java");
if(file.exists()){
boolean b=file.isDirectory();
System.out.println(b);
}
}
// 判断文件是否存在
public static void fun8(){
File f=new File("src");
boolean es=f.exists();
System.out.println(es);
}
// 创建文件功能 如果已经存在就不创建
// 创建文件
public static void fun9() throws IOException{
File file=new File ("d:\\XJWJ");
boolean mkd=file.mkdir();
System.out.println(mkd);
File fiele =new File("d:\\XJWJ\\a.text");
boolean s=fiele.createNewFile();
System.out.println(s);
}
// 删除文件
public static void fun10(){
File file =new File ("d:\\XJWJ\\a.text");
File file2=new File("D:\\XJWJ");
boolean b=file.delete();
boolean delete = file2.delete();
System.out.println(b);
System.out.println(delete);
}
public static void fun11() throws IOException{
// 创建D盘中的路径
File fileM =new File("d:\\aa");
File file =new File ("d:\\aa\\bbb.txt");
File file1=new File("d:\\aa\\aac1.txt");
File file2=new File("d:\\aa\\ccc.txt");
File file3=new File("D:\\aa\\ss.txt");
File file4=new File("d:\\aa\\ss");
// 打印D盘的路径
System.out.println(fileM);
System.out.println(file);
System.out.println(file1);
System.out.println(file2);
System.out.println(file3);
System.out.println(file4);
System.out.println();
System.out.println("++++++++++++++++++++++++++++++++");
System.out.println();
// 创建文件夹和文件
boolean mkd = fileM.mkdir();
boolean NewFile = file.createNewFile();
boolean NewFile1 = file1.createNewFile();
boolean NewFile2 = file2.createNewFile();
boolean NewFile3= file3.createNewFile();
boolean NewFile4 = file4.mkdir();
//
// 打印
System.out.println(mkd);
System.out.println(NewFile);
System.out.println(NewFile1);
System.out.println(NewFile2);
System.out.println(NewFile3);
System.out.println(NewFile4);
System.out.println();
System.out.println("++++++++++++++++++++++++++++++++");
System.out.println();
// 遍历文件夹的内容
File[] list = fileM.listFiles();
for(File fil:list){
System.out.println(fil);
}
System.out.println();
System.out.println("========================");
System.out.println(list.length);
System.out.println();
System.out.println("========================");
// 删除文件和文件夹
boolean delete=file.delete();
boolean delete1 = file1.delete();
boolean delete2 = file2.delete();
boolean delete3 = file3.delete();
boolean delete4 = file4.delete();
boolean delete5 = fileM.delete();
// 打印
System.out.println(delete1);
System.out.println(delete2);
System.out.println(delete3);
System.out.println(delete4);
System.out.println(delete);
System.out.println(delete5);
}
public static void fun12(){
//遍历d盘文件夹的内容
System.out.println("============================");
File f =new File("D:\\java\\新建文件夹");
File[] li = f.listFiles(new MyFile());
for(File f1:li){
System.out.println(f1);
}
}
public static void fun13(){
// 打印; \
System.out.println("++++++++++++++++++++++++");
String s=File.pathSeparator;
System.out.print(s);
String se=File.separator;
System.out.println(se);
}
}
原文地址:https://www.cnblogs.com/hph1728390/p/10585236.html