java note 6:File文件简单使用

public class MainApp {

    public static void main(String[] args) throws IOException {
        //如何创建一个文件夹?
        File parent = new File("config");

        // 如何用父类文件夹创建一个文件?
        File file = new File(parent,"aa.txt");
        File file1 = new File("idea\\java\\datasource","aa.txt");

        //判断文件/夹存在?
        file.exists();

        //src目录发布后就会消失!

        //怎么获得.class运行时的文件夹信息?当前运行对象目录/当前运行对象的项目目录
        System.out.println(new MainApp().getClass().getResource(""));
        System.out.println(new MainApp().getClass().getResource("/"));

        //把资源转成URI?
        try {
            file = new File(new MainApp().getClass().getResource("MainApp.class").toURI());
            //省略前面的若干文件夹?
            file = new File(new MainApp().getClass().getResource("../aruiea/MainApp.class").toURI());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        System.out.println(file.exists());

        //File常量字段分隔符\?
        String str = "/com" + File.separator+"aruiea";

        //File常见方法
        file.canRead();
        file.canWrite();
        boolean newFile = file.createNewFile();
        file.delete();
        file.deleteOnExit();//虚拟机退出时删除
        file.getAbsolutePath();//得到绝对路径,包含所有名称
        file.getPath();//得到文件目录
        file.getName();//得到文件名
        file.isDirectory();//是目录?
        file.isFile();//是文件?
        file.mkdir();//创建目录,只创建单层
        file.mkdirs();//创建多层目录,包括文件
        file.renameTo(new File("new dest"));
        file.listFiles();//返回目录下文件数组
        file.getParentFile();//返回文件的所有父类文件夹结构
        
        //创建一个绝对路径文件
        File file2 = new File("file/upload/text/aaa.txt");
        if (!file2.exists()) {
            file2.mkdirs();
        }
//        System.out.println(file2.getAbsolutePath());

        //得到文件夹下的所有文件
        File[] files = new File("new dest").listFiles();
        for (File file3 : files) {
            String name = file3.getName();
            System.out.println(name);
        }
        
        //如何获得一个路径下的所有文件夹和所有文件夹下的所有文件?
        //利用getInfo(){}得到所有文件夹的信息,递归调用!

        //虚拟机关闭时删除
        file.deleteOnExit();
        file1.deleteOnExit();
        file2.deleteOnExit();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值