A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© javaeea 中级黑马   /  2015-9-29 22:04  /  519 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.heima.gaoshuai;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Test10 {
       
        public static void main(String[] args) {
                print(new File("D:\\我的文档\\Tencent Files\\1435431902\\FileRecv\\day21补充"),0);
        }
       
/**
* 从键盘接收一个文件夹路径,统计该文件夹大小
* @return
*/
        public static long getSize(){
                Scanner sc = new Scanner(System.in);
                File file = new File(sc.nextLine());
                long a = 0;
                if(file.isDirectory()){
                         a = file.length();
                       
                }
                return a ;
        }
        /**
         * 从键盘接收一个文件夹路径,删除该文件夹
         */
        public static boolean delDir(){
                Scanner sc = new Scanner(System.in);
                File file = new File(sc.nextLine());
                if(file.exists()){
                        file.delete();
                        return true;
                }else{
                        return false;
                }
        }
        /**
         * 从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
         * @param fileName
         * @throws IOException
         */
        public static void copyFile(String fileName) throws IOException{
                Scanner sc = new Scanner(System.in);
                File file = new File(sc.nextLine());
                //String fileName ="";
                if(file.exists()){
                       
                        if (file.isDirectory()) {
                                file.mkdir();
                                fileName = file.getAbsolutePath();
                                File[] files = file.listFiles();
                                for (int i = 0; i < files.length; i++) {
                                       
                                        copyFile(fileName);
                                }
                        } else if (file.isFile()) {
                                FileInputStream fis = new FileInputStream(file.getAbsolutePath());
                                FileOutputStream fos = new FileOutputStream("d:\\文档"+file.getName());
                                int len;
                                while ((len = fis.read()) != -1) {
                                        fos.write(len);
                                }
                        }
                       
                }
        }

1 个回复

倒序浏览
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马