——- android培训、java培训、期待与您交流! ———-
概述
File类用来将文件或者文件夹封装成对象,方便对文件与文件夹的属性信息进行操作。
File对象可以作为参数传递给流的构造函数。
示例
import java.io.File;
public class FileDemo{
public static void main(String[] args){
constructorDemo();
}
public static void constructorDemo(){
//可以将一个已存在的,或者不存在的文件或者目录封装成file对象
//方式一
File f1 = new File("d:\\demo\\a.txt" );
//方式二
File f2 = new File("d:\\demo" ,"a.txt" );
//方式三
File f = new File("d:\\demo" );
File f3 = new File(f,"a.txt" );
//考虑到Windows和Linux系统通用
File f4 = new File("d:" + File.separator + "demo" + File.separator + "a.txt" );
}
}
File.separator是与系统有关的默认名称分隔符。在 UNIX 系统上,此字段的值为 ‘/’;在 Microsoft Windows 系统上,它为 ‘\’。
File对象的常用方法
获取
获取文件名称
获取文件路径
获取文件大小
获取文件修改时间
示例
import java.io.File;
import java.text.DateFormat;
import java.util.Date;
public class FileMethodDemo{
public static void main(String[] args){
getDemo();
}
public static void getDemo(){
File file1 = new File("a.txt" );
File file2 = new File("d:\\demo\\a.txt" );
String name = file2.getName();
String absPath = file2.getAbsolutePath(); //绝对路径
String path1 = file1.getPath();
String path2 = file2.getPath();
long len = file2.length();
long time = file2.lastModified();
//相对路径不同,父目录不同
//如果此路径名没有指定父目录,则返回 null
String parent1 = file1.getParent();
String parent2 = file2.getParent();
Date date = new Date(time);
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String str_time = df.format(date);
System.out.println( "name:" + name);
System.out.println( "absPath:" + absPath);
System.out.println( "path1:" + path1);
System.out.println( "path2:" + path2);
System.out.println( "len:" + len);
System.out.println( "str_time:" + str_time);
System.out.println( "parent1:" + parent1);
System.out.println( "parent2:" + parent2);
}
}
运行结果图
创建和删除
示例
import java.io.File;
import java.io.IOException;
public class FileMethodDemo{
public static void main(String[] args) throws IOException {
createAndDeleteDemo();
}
public static void createAndDeleteDemo() throws IOException {
File file = new File("file.txt" );
//和输出流不一样,如果文件不存在,则创建,如果文件存在,则不创建
boolean b1 = file.createNewFile();
System.out.println( "b1 = " + b1);
//使用delete方法删除文件夹的时候,如果文件夹中有文件,则会删除失败
boolean b2 = file.delete();
System.out.println( "b2 = " + b2);
File dir = new File("abc\\ab" );
//使用mkdir可以创建多级目录
boolean b3 = dir.mkdir();//make directory
System.out.println( "b3 = " + b3);
//最里层目录被干掉,dir代表的是最里层的目录
boolean b4 = dir.delete();
}
}
运行结果图
判断
示例
import java.io.File;
import java.io.IOException;
public class FileMethodDemo{
public static void main(String[] args) throws IOException {
isDemo();
}
public static void isDemo() throws IOException {
File f = new File("aaa.txt" );
boolean b = f.exists();
System.out.println( "b = " + b);
if(!f.exists()){
f.createNewFile();
}
//最好先判断是否存在
if(f.exists()){
System.out.println(f.isFile());
System.out.println(f.isDirectory());
}
f = new File("aa\\bb" );
f.mkdirs();
if(f.exists()){
System.out.println(f.isFile());
System.out.println(f.isDirectory());
}
}
}
运行结果图
重命名
示例
import java.io.File;
import java.io.IOException;
public class FileMethodDemo{
public static void main(String[] args) throws IOException {
renameToDemo();
}
public static void renameToDemo() throws IOException {
File f1 = new File("d:\\code\\day21\\0.mp3" );
File f2 = new File("d:\\code\\day21\\1.mp3" );
boolean b = f1.renameTo(f2);
System.out.println( "b = " + b);
}
}
运行结果图
系统根目录和容量获取
示例
import java.io.File;
import java.io.IOException;
public class FileMethodDemo{
public static void main(String[] args) throws IOException {
listRootsDemo();
}
public static void listRootsDemo() throws IOException {
File[] files = File.listRoots();
for(File file : files){
System.out.println(file);
}
File file = new File("d:\\" );
System.out.println( "getFreeSpace:" + file.getFreeSpace());
System.out.println( "getTotalSpace:" + file.getTotalSpace());
System.out.println( "getUsableSpace:" + file.getUsableSpace());
}
}
运行结果图