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盘结构:
待解决、、、、、
那是目录!!!!!!
阅读全文
0 0
- Java I/O 文件复制练习
- java I/O复制文件
- Java I/O 字节流 文件复制
- I/O复制文件
- 标准I/O文件复制,文件I/O文件复制
- java I/O: 以字节形式复制文件
- JAVA--I/O流实现文件的复制
- Java中的I/O流及利用I/O流实现文件复制功能
- Java 文件I/O
- java文件I/O
- 用I/O流复制文件
- 简单实现I/O文件复制
- (练习)I/O文件流处理
- java中关于I/O文件流的操作实现文件复制和打印功能
- java I/O 读取文件
- Java文件I/O操作
- java API 文件,I/O
- Java中的文件I/O
- 时间与时间戳之间的转换
- Mac Python3安装MySQL问题记录
- vue-cli 初始化项目结构
- 网络是怎样连接的学习笔记(四)
- php的命名空间
- Java I/O 文件复制练习
- JavaScript
- python中求两个List的交集、并集和差集
- Netbeans创建JaveWeb项目
- 把java类生成jar
- JDK1.8新特性了解
- 使用keytool 生成证书
- tensonflow DNN实验
- 08:白细胞计数