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);
                }
            }
        }
    }

  • 相关阅读:
    字符串函数之strncat
    1的数目_扩展问题
    关于虚函数(多态)与继承的一道搜狗笔试题
    给定两个正整数(二进制形式表示)A和B,问把A变为B需要改变多少位(bit)?也就是说,整数A和B的二进制表示中有多少位是不同的?
    字符串函数之strcmp
    字符串函数之strchr
    1的数目
    linux scp 远程获取文件
    scala之helloworld
    scala0011
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13530493.html
Copyright © 2011-2022 走看看