黑马程序员技术交流社区

标题: IO练习 [打印本页]

作者: javaeea    时间: 2015-9-29 22:04
标题: IO练习
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);
                                }
                        }
                       
                }
        }
作者: 王贺明    时间: 2015-9-29 23:13





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2