File类

import java.io.File;
import java.io.IOException;
import text6.Staticvariaty;

//File类的构造方法    三种重载形式
public class FileDemo01 {
    public static void main(String[] args) throws IOException {
        function_13();
    }
    
    //传递路径名:可以写到文件夹,可以写到一个文件
    //将路径封装为File类型对象
    public static void function() throws IOException{
        File file=new File("d:\\eclipse");
        //File创建文件的功能  boolean createNewFile()  (文件已经存在了,不再创建)
        boolean b=file.createNewFile();
        System.out.println(b);
    }
    
    public static void function_1(){
        File file=new File("c:","windows");
        System.out.println(file);
    }
    
    public static void function_2(){
        File parent=new File("d:");
        File file=new File(parent,"eclipse");
        System.out.println(file);
    }
    
    //File创建文件夹功能 boolean mkdir()  (文件夹已经存在了,不再创建)
    public static void function_3(){
        File file=new File("d:\\abc");
        boolean b=file.mkdir();
        System.out.println(b);
    }
    //File创建多级目录的文件夹
    public static void function_4(){
        File file=new File("d:\\abc\\c");
        boolean b=file.mkdirs();
        System.out.println(b);
    }
    
    //File类的删除功能 boolean delete()  (删除的文件或文件夹,在File构造方法中给出;删除成功返回true,失败返回false)
    //删除方法,不走回收站,直接从硬盘中删除
    public static void function_5(){
        File file=new File("d:\\abc");
        boolean b=file.delete();
        System.out.println(b);
    }
    
    //File类的获取功能---String getName():返回由此抽象路径名表示的文件或目录的名称
    //获取路径中的最后部分的名字
    public static void function_6(){
        File file=new File("e:\\Wechat\\WeChat.exe");
        String nameString=file.getName();
        System.out.println(nameString);
    }
    
    //File类的获取功能---long length():返回路径中表示的文件大小的字节数
    public static void function_7(){
        File file=new File("e:\\Wechat\\WeChat.exe");
        long length=file.length();
        System.out.println(length);
    }
    
    //File类的获取功能---String getAbsolutePath():获取绝对路径,返回String对象
    //            ---String getAbsoluteFile():获取绝对路径,返回File对象  (常用此)
    public static void function_8(){
        File file=new File("src");
        File absolute=file.getAbsoluteFile();
        System.out.println(absolute);
    }
    
    //File类的获取功能---String getParent():获取父路径,返回String对象
    //                 String getParentFile():获取父路径,返回File对象
    public static void function_9(){
        File file=new File("e:\\Wechat\\Wechat.exe");
        File parent=file.getParentFile().getParentFile();
        System.out.println(parent);
    }
    
    //File判断功能---boolean exists():判断File构造方法中封装路径是否存在
    public static void function_10(){
        File file=new File("src");
        boolean b=file.exists();
        System.out.println(b);
    }
    
    //File判断功能---boolean isDirectory():判断是不是文件夹
    public static void function_11(){
        File file=new File("src");
        boolean b=file.isDirectory();
        System.out.println(b);
    }
    
    //File类的获取功能---String[] list():获取到File构造方法中封装的路径中的文件和文件夹名(遍历一个目录)
    public static void function_12(){
        File file=new File("e:\\Wechat");
        String[] strArr=file.list();
        for(String s:strArr){
            System.out.println(s);
        }
    }
    
    //File类的获取功能---String[] listFiles():获取到File构造方法中封装的路径中的文件和文件夹名(遍历一个目录)
    //返回的是文件或目录的全路径
    public static void function_13(){
        File file=new File("e:\\Wechat");
        File[] fileArr=file.listFiles();
        for(File f:fileArr){
            System.out.println(f);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值