文件常见操作属性

package Demo1;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;

public class Demo {

    public static void main(String[] args) throws IOException {
    
        method_6();
    }
    public static void method_6() {
        File file=new File("c:\\");
        String[] names=file.list();//得到文件目录

        for(String name:names)
        {
            sop(name);
        }        
    }
    private static void method_5() {
        File file=new File("D:\\");
        sop(file.getFreeSpace());//文件剩余空间
        sop(file.getTotalSpace());//文件已用空间
        sop(file.getUsableSpace());
/*        File[] files=File.listRoots();
        for(File file:files)
        {
            System.out.print(file);
        }*/
        
    }
    public static void method_4()throws IOException
    {
        File f1=new File("b.txt");
        File f2=new File("c.txt");
        boolean b=f1.renameTo(f2);//修改文件名

        sop(b);
    }
    public static void method_3()throws IOException
    {
        File f1=new File("mywww\\awtDemo\\fdssd.txt");
        boolean c=f1.mkdirs();//创建多级目录
        f1.delete();//删除
    }
            //路径分隔符
        public static void method_2()
        {
            File f1=new File("a.txt");
            String path=f1.getAbsolutePath();
            long len=f1.length();
            sop(len);
            sop(f1.canExecute());//可执行
            sop(f1.canRead());//可读
            sop(f1.canWrite());//可写
            sop(f1.getName());//文件名

            long time=f1.lastModified();//文件最后修改时间
            Date date=new Date(time);
            DateFormat dateFormat=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
            String str_time=dateFormat.format(date);
            sop(str_time);
            System.out.println(path);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    

}
原文地址:https://www.cnblogs.com/kedoudejingshen/p/2731305.html