Java io流完成复制粘贴功能
JAVA 中io字节输入输出流 完成复制粘贴功能:
public static void main(String[] args) throws Exception{
// 创建输入流要读取的文件
FileInputStream fis = new FileInputStream("f:yxx.txt");
//创建要输出的文件路径
FileOutputStream fos = new FileOutputStream("f:Yao/y.txt");
//创建变量 用于接收 字节流读取返回字节
int len = 0;
while((len = fis.read())!=-1){
fos.write(len);
}
//关闭数据流
fis.close();
fos.close();
}
使用IO字节数组输入输出流 完成复制粘贴功能: 这个方法比较快
// 创建输入流要读取的文件
FileInputStream fis = new FileInputStream("f:s.exe");
//创建要输出的文件路径
FileOutputStream fos = new FileOutputStream("f:Yao/s.exe");
//创建变量 用于接收 字节流读取返回字节
int len = 0;
byte[] by = new byte[1024];
while((len = fis.read(by))!=-1){
fos.write(by,0,len);
}
//关闭数据流
fis.close();
fos.close();
}
使用io流 字符流来复制 注意只能复制文本!!
/*
* 字符流的复制功能 只能复制 文本文件
* 循环一定要刷新缓冲区
* */
public static void main(String[] args)throws IOException {
FileReader fr = new FileReader("E:gu.txt");
FileWriter fw = new FileWriter("e:gubin/1.txt");
char[] cbuff = new char[1024];
int len = 0;
while ((len = fr.read(cbuff))!=-1) {
fw.write(cbuff);
fw.flush();
}
fr.close();
fw.close();
}
=====================
/*
* 文件复制方式,字节流,一共4个方式
* 1. 字节流读写单个字节 125250 毫秒
* 2. 字节流读写字节数组 193 毫秒 OK
* 3. 字节流缓冲区流读写单个字节 1210 毫秒
* 4. 字节流缓冲区流读写字节数组 73 毫秒 OK
*/
public class Copy {
public static void main(String[] args)throws IOException {
long s = System.currentTimeMillis();
copy_4(new File("c:\\q.exe"), new File("d:\\q.exe"));
long e = System.currentTimeMillis();
System.out.println(e-s);
}
/*
* 方法,实现文件复制
* 4. 字节流缓冲区流读写字节数组
*/
public static void copy_4(File src,File desc)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
/*
* 方法,实现文件复制
* 3. 字节流缓冲区流读写单个字节
*/
public static void copy_3(File src,File desc)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0 ;
while((len = bis.read())!=-1){
bos.write(len);
}
bos.close();
bis.close();
}
/*
* 方法,实现文件复制
* 2. 字节流读写字节数组
*/
public static void copy_2(File src,File desc)throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
/*
* 方法,实现文件复制
* 1. 字节流读写单个字节
*/
public static void copy_1(File src,File desc)throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0 ;
while((len = fis.read())!=-1){
fos.write(len);
}
fos.close();
fis.close();
}
}
Java io流完成复制粘贴功能的更多相关文章
- 在本机上用IO流实现复制粘贴功能
/** * 复制文件夹 * @param sourcePath * @param targetPath * @throws IOException */ public void copyFolder( ...
- Java IO流文件复制/解压的几种方法总结
引言 在JavaWeb项目开发过程,涉及到IO文件的读写操作以及文件的复制copy操作是作为一个程序员不可获取的知识,那接下来就总结一些copy文件的一些方法,与大家通过学习,如果还有其他更好的方法, ...
- java io流(字节流)复制文件
java io流(字节流) 复制文件 //复制文件 //使用字节流 //复制文本文件用字符流,复制其它格式文件用字节流 import java.io.*; public class Index{ pu ...
- java IO流详解
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- Java IO流题库
一. 填空题 Java IO流可以分为 节点流 和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行. 输入流的唯一目的是提供通往数据的通道,程序可以通过这个通道读取数 ...
- Java IO流详尽解析
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream
Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...
- Java IO 流总结篇
1. 写在前面的话 I/O ,I 是 Input (输入)的缩写,O是Output (输出) 的缩写,众所周知,人与人之间想要沟通交流,就需要讲彼此都能听懂的语言,比如大家都统一说英语. 人类如果想和 ...
- Java IO流学习总结一:输入输出流
Java IO流学习总结一:输入输出流 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/54292148 本文出自[赵彦军的博客] J ...
随机推荐
- LUA全总结
------------------------------------------------------------------------------ --2018.7.21 do --开启或关 ...
- protobuf's extension
[protobuf's extension] extension允许第三方扩展协议,开发方需要像下面这样定义: 扩展方需要像下面这样扩展: 使用的时候必须用SetExtension方法: 参考:htt ...
- codeforces:Michael and Charging Stations分析和实现
题目大意 迈克尔接下来n天里分别需要支付C[1], C[2], ... , C[n]费用,但是每次支付费用可以选择使用优惠或不使用优惠,每次使用价值X的优惠那么迈克尔所能使用的优惠余量将减少X并且当天 ...
- Linux进程之Fork函数
Fork()函数 1.所需头文件: #include <unistd.h> #include<sys/types.h> 2.函数定义 pid_t fork( void ); p ...
- 27.Remove Element(Array)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- react native 导航路由组件react-navigation的使用
navigation的几个难点和问题: 1.底部tab是否可以加上中间的大按钮? 如果加上,如何触发事件? js文件放哪? 2.navigation的登录注册页面.成功后应该不能返回刚刚的登录页面?清 ...
- JAVA - 守护线程(Daemon Thread)
转载自:http://www.cnblogs.com/luochengor/archive/2011/08/11/2134818.html 在Java中有两类线程:用户线程 (User Thread) ...
- 使用 ipmitool 实现 Linux 系统下对服务器的 ipmi 管理
http://www.ibm.com/developerworks/cn/linux/l-ipmi/ 1.简介 IPMI(Intelligent Platform Management Interfa ...
- Qt Customize QVariant
Customize QVariant #include <QCoreApplication> #include <QVariant> #include <QDebug&g ...
- Linux automake命令
1)automake 使用教程 http://loftor.com/archives/automake.html 2)configure.in Makefile.am解析 http://blog.cs ...