妖碧落

导航

File类应用例子

public class test1 {
    /*
     * 从键盘录入一个文件夹, 统计该文件夹大小
     */
    public static void main(String[] args) {
        File dir = getFile();
        System.out.println(getFileLength(dir));
    }
    private static long getFileLength(File dir) {
        long len = 0;
        File[] farr = dir.listFiles();
        for (File subFile : farr) {
            if (subFile.isFile()) {
                len = len + subFile.length();     // 字节长度就是文件大小
            } else {
                len = len + getFileLength(subFile);   //  遇到文件夹就递归一下
            }
        }
        return len;
    }

    public static File getFile() {    // 获取键盘输入
        Scanner s1 = new Scanner(System.in);
        System.out.println("请输入一个文件夹路径");
        
        while(true){
            String path = s1.nextLine();
            File f1 = new File(path);
            if (f1.exists()) {
                if (f1.isFile()) {
                    System.out.println("你输入的是一个文件路径,请重新输入一个文件夹路径");
                }else{
                    return f1;
                }
            }else {
                System.out.println("你输入的路径不存在,请重新输入一个文件夹路径");
            }
        }
    }
}

 

 

import java.io.File;

public class test2 {
    
    public static void main(String[] args) {
        File dir = test1.getFile();
        deleteFile(dir);
    }

    private static void deleteFile(File dir) {  //  递归删除文件夹
        File[] subFiles = dir.listFiles();
        for (File subFile : subFiles) {
            if (subFile.isFile()) {
                subFile.delete();
            } else {
                deleteFile(subFile);
            }
        }
        dir.delete();
    }

}

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.text.DefaultEditorKit.CopyAction;

public class test3 {
    /*
     * 从键盘接收2个文件夹路径,把其中一个文件夹拷贝到另一个文件夹中
     */
    public static void main(String[] args) throws IOException {
        File src = test1.getFile();
        File dest = test1.getFile();
        copy(src,dest);
    }

    private static void copy(File src, File dest) throws IOException {
        File newDir = new File(dest,src.getName());
        newDir.mkdir();
        File[] subFiles = src.listFiles();
        for (File subfile : subFiles) {
            if(subfile.isFile()){
                BufferedInputStream b1 = new BufferedInputStream(new FileInputStream(subfile));
                BufferedOutputStream b2 = new BufferedOutputStream(
                        new FileOutputStream(new File(newDir,subfile.getName())));
                int  b;
                while ((b = b1.read()) != -1) {
                    b2.write(b);
                }
                b1.close();
                b2.close();
                
            }else{
                copy(subfile, newDir);
            }
        }
    }

 

import java.io.File;
import java.io.PrintStream;

public class test4 {
    /*
     * 从键盘获取一个文件夹,按照层级打印文件和文件夹
     */
    public static void main(String[] args) {
        File dir = test1.getFile();
        PrintLev(dir,0);
    }

    public static void PrintLev(File dir,int lev) {
        File[] subFiles = dir.listFiles();
        for (File subfile : subFiles) {
            for (int i = 0; i <= lev; i++) {
                System.out.print("\t");
            }
            System.out.println(subfile);
            if (subfile.isDirectory()) {
                PrintLev(subfile,lev + 1);
            }
        }
    }

}

 

 

import java.io.FileInputStream;

public class test5 {
    /*
     * 斐波那契数列
     */
    public static void main(String[] args) {
        int x = fei(12);
        System.out.println(x);
        int[] arr = new int[12];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = fei(i+1);
            System.out.println(arr[i]);
        }
        
    }

    private static int fei(int n) {
        
        if (n==1 || n==2) {
            return 1;
        } 
        else {
            return fei(n-1) + fei(n-2);
        }
    }

}

 

import java.math.BigInteger;

public class test6 {
    /*
     * 1000的阶乘  所有零和尾部零的个数
     */
    public static void main(String[] args) {
        //demo1();    //获取所有0
        demo2();     // 获取尾部0 
    }

    public static void demo2() {
        BigInteger b1 = new BigInteger("1");
        for (int i = 1; i <= 1000; i++) {
            BigInteger b2 = new BigInteger(i+"");
            b1 = b1.multiply(b2);
        }
        //System.out.println(b1);
        String str = b1.toString();
        StringBuilder sb1 = new StringBuilder(str);
        str = sb1.reverse().toString();
        int count = 0 ;
        for (int i = 0; i < str.length(); i++) {
            if ('0'!=str.charAt(i)) {
                break;
            } else {
                count++;
            }
            
            
        }
        System.out.println(count);
    }

    public static void demo1() {
        BigInteger b1 = new BigInteger("1");
        for (int i = 1; i <= 1000; i++) {
            BigInteger b2 = new BigInteger(i+"");
            b1 = b1.multiply(b2);
        }
        //System.out.println(b1);
        String str = b1.toString();
        int count = 0 ;
        for (int i = 0; i < str.length(); i++) {
            if ('0'==str.charAt(i)) {
                count= count +1 ;
            }
            
        }
        System.out.println(count);
    }

}

 

posted on 2019-08-08 23:07  妖碧落  阅读(221)  评论(0编辑  收藏  举报