2019-3-6

package com.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileDome {
    
    public static void fuz (String s,String b) {
        String a = "";
        int du = -1;
        FileInputStream  file = null;
        FileOutputStream fio = null;
        try {
              file = new FileInputStream(s);
            System.out.println("这个文件的字节是:"+file.available());

            while((du=file.read())!=-1) {    
                a+=(char)du;
            }
            System.out.println(a);
            System.out.println("读取完成,准备复制");
             fio = new FileOutputStream(b);
            byte fuz[] = a.getBytes();
            fio.write(fuz, 0, fuz.length);
            System.out.println("复制完成");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {    
            if(file!=null) {
                try {
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fio!=null) {
                try {
                    fio.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }        
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要文件的地址");
        String shuru = sc.next();
        File  f = new File (shuru);
        if(f.exists()) {
            System.out.println("请输入要要复制的地址");
            String fuz = sc.next();
            fuz(shuru,fuz);
        }else {
            System.out.println("文件不存在");
        }
        
    }

}

输出结果图:

猜你喜欢

转载自www.cnblogs.com/cxlbzdcom/p/10485450.html