Java I/O 文件复制练习

来源:互联网 发布:raphael.js 类似工具 编辑:程序博客网 时间:2024/06/07 23:09
/*** @author StormWangxhu* @version 创建时间:2017年11月2日 下午4:31:10**/

利用FileInputStream和FileOutputStream字节输入流、输出流实现文件的复制。先来看看代码:

package com.stormwang.inputStreamDemo;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;/*** @author StormWangxhu* @version 创建时间:2017年11月2日 下午4:31:10**//** * 文件的复制 * 思路: * 1、 * */ class FileCopy {    public FileCopy() {        InputStreamReader iStreamReader = new InputStreamReader(System.in);        BufferedReader bReader = new BufferedReader(iStreamReader);        System.out.print("请输入源文件名:");        //源文件名        String fileName1 = null ;        try {            fileName1 = bReader.readLine();            File file = new File(fileName1);//根据源文件名创建文件            if (file.exists()) {                //若存在,则判断文件属性,是否文件,是否可读                //不是文件,目录无法复制                /*                 * isFile()                 * 返回:                 *    当且仅当此抽象路径名表示的文件存在 且 是一个标准文件时,返回 true;否则返回 false                 * */                if (!file.isFile()) {                    System.out.print(file.getName());                    System.out.println("不是文件,目录无法复制!");                    return;                }                //不可读文件无法复制                if (!file.canRead()) {                    System.out.println(file.getName()+"不可读文件无法复制");                    return;                }                System.out.println("请输入目的文件名:");                String fileName2  = bReader.readLine();                File file2 = new File(fileName2);                //判断目的文件是否存在                if (file2.exists()) {                    System.out.println("该文件已经存在,是否覆盖(Y|N):");                    char s = (char)bReader.read();                    if ((s=='y')||(s=='Y')) {                        Copy(file,file2);                    }                }else {                    file2.createNewFile();                    Copy(file, file2);                }            }else {                System.out.println("源文件不存在!");            }        } catch (IOException e) {            e.printStackTrace();        }    }    private void Copy(File file, File file2) {        try {            FileInputStream fileInputStream= new FileInputStream(file);            FileOutputStream fileOutputStream = new FileOutputStream(file2);            int c ;            //字节缓冲区            byte[] buff = new byte[1024];            try {                while ((c= fileInputStream.read(buff, 0, 1024))!=-1) {                    fileOutputStream.write(buff, 0, c);                }                System.out.println("文件复制成功!");            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }finally {                //关闭流资源                if (fileInputStream!= null) {                    fileInputStream.close();                }                if (fileOutputStream!= null) {                    fileOutputStream.close();                }            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {        }    }}

根据面向对象思想,我们再来创建一个测试类:

package com.stormwang.inputStreamDemo;import com.stormwang.inputStreamDemo.FileCopy;/*** @author StormWangxhu* @version 创建时间:2017年11月2日 下午5:15:59**///测试类public class CopyFileTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        FileCopy fileCopy = new FileCopy();    }}

运行:
这里写图片描述

问题: 一直表示不是文件,是一个目录。
难道文件路径错误?
F盘结构:
这里写图片描述

待解决、、、、、

那是目录!!!!!!