Java:文件操作
import java.io.File;
import java.io.IOException;
public class Driver {
public static void main(String[] args) throws IOException {
String pathSeparator = File.pathSeparator;
System.out.println(pathSeparator);//路径分隔符
String separator = File.separator;
System.out.println(separator); //文件分隔符
show01();
show02("d:\\","Driver.java");
show03();
show04();// 获取功能
show05();//判断功能
show06();//遍历
show07();//删除功能操作
}
private static void show07() throws IOException {
File file=new File("C:\\wanson\\demo1"); //一级文件夹
System.out.println(file.mkdir());
/* File file1=new File("C:\\wanson\\aa\\bb\\cc");
System.out.println(file1.mkdirs());*/ // 创建多级文件夹
/* File file2=new File("C:\\wanson\\hello.txt");
System.out.println(file2.createNewFile());*/
File file3=new File("C:\\wanson\\demo1");
System.out.println(file3.delete());
}
private static void show06() {
File file=new File("D:\\UPUPW_ANK_W64");
//遍历方法1
// File file=new File("D:\\UPUPW_ANK_W641");
// File file=new File("D:\\UPUPW_ANK_W64\\UPUPW.exe");
String[] name=file.list();
System.out.println("*****************************************");
for (String s : name) {
System.out.println(s);
}
//遍历方法2
System.out.println("*****************************************");
File[]files=file.listFiles();
for (File file1 : files) {
System.out.println(file1);
}
}
private static void show05() {
//判断功能
File file = new File("D:\\java_project\\HandleFile\\src\\Driver.java");
File file1 = new File("f:\\java_project\\HandleFile\\src\\Driver.java");
System.out.println(file.exists());
System.out.println(file1.exists());
System.out.println(file.isFile());//判断是否是文件
System.out.println(file1.isFile());
System.out.println(file.isDirectory());
System.out.println(file1.isDirectory());
}
private static void show04() {
// 获取功能操作
File file=new File("D:\\java_project\\HandleFile\\src\\Driver.java");
File file1=new File("\\src\\Driver.java");
System.out.println(file.getAbsolutePath());
System.out.println(file1);
System.out.println(file.getPath());
System.out.println(file1.getPath());
System.out.println(file.getName());
System.out.println(file1.getName());
System.out.println(file.length()); //文件大小 有大小 单位字节
System.out.println(file1.length());//文件夹大小 没有大小
}
private static void show03() {
File parent=new File("D:\\java_project\\HandleFile\\src\\");
File file=new File(parent.getName(),"Driver.java");
System.out.println(file);
}
private static void show02(String parent,String child) {
File file=new File(parent,child);
System.out.println(file);
}
private static void show01(){
File file01=new File("D:\\java_project\\HandleFile");
System.out.println(file01);
File file02=new File("D:\\java_project\\HandleFile\\src\\Driver.java");
System.out.println(file02);
File file03=new File("Driver.java");
System.out.println(file03);
}
}
posted on 2019-05-17 00:16 Indian_Mysore 阅读(112) 评论(0) 编辑 收藏 举报