zoukankan      html  css  js  c++  java
  • Java-IO流-file类的几个方法

    package cn.bruce.file;
    
    import java.io.File;
    import java.io.IOException;
    
    public class FileDemo2 {
        public static void main(String[] args) throws IOException {
            fun1();
            fun2();
            fun3();
            fun4();
            fun5();
            fun6();
            fun6_1();
        }
    
        public static void fun1() throws IOException {
            File file = new File("E:\A.TXT");
            Boolean B = file.createNewFile();// 当不存在此名称文件时创建文件
            System.out.println(B);
            Boolean B1 = file.delete();// 删除此名称文件
            System.out.println(B1);
        }
    
        public static void fun2() throws IOException {
            File file = new File("E:\A\b\c");
            Boolean B = file.mkdirs();// 当不存在此名称文件时创建多层次文件夹
            System.out.println(B);
            File file1 = new File("E:\b");
            Boolean B1 = file1.delete();// 删除此名称单层文件夹
            System.out.println(B1);
        }
    
        public static void fun3() throws IOException {
            File file1 = new File("E:\b\A.txt");
            Long length = file1.length();// 取得文件大小字节数
            System.out.println(length);
        }
    
        public static void fun4() throws IOException {
            File file1 = new File("E:\b\A.txt");
            Boolean B1 = file1.exists();// 判断路径存不存在
            System.out.println(B1);
        }
    
        public static void fun5() throws IOException {
            File file1 = new File("E:\b");
            if (file1.exists())// 路径存在才去判断
            {
                Boolean B1 = file1.isDirectory();// 判断是不是目录
                System.out.println(B1);
            }
        }
    
        // 遍历目录1
        public static void fun6() throws IOException {
            File file1 = new File("E:\b");
            if (file1.exists())// 路径存在才去判断
            {
                String[] s = file1.list();// 判断是不是目录
                for (String string : s)
                {
                    System.out.println(string);
                }
            }
        }
        // 遍历目录2-全名 推荐
        public static void fun6_1() throws IOException {
            File file1 = new File("E:\b");
            if (file1.exists())// 路径存在才去判断
            {
                File[] s = file1.listFiles();// 判断是不是目录
                for (File File : s)
                {
                    System.out.println(File);
                }
            }
        }
    }

  • 相关阅读:
    MQTT 连接服务端失败,报错客户机未连接(32104)
    引入其他类定义的静态变量
    Linux 中文乱码问题
    MQTT 简介
    mybatis xml 特殊字符转义
    如何科学的高效率的选择创建线程数
    【安卓自定义控件系列】自绘控件打造界面超炫功能超强的圆形进度条
    Eclipse简介和使用技巧快捷方式
    MyEclipse如何全局搜索
    JAVA面向对象-----访问修饰符
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13530493.html
Copyright © 2011-2022 走看看