Java实现文件复制的四种方式

摘要:
背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制。=-1){8fo.write;9}10fi.close();11fo.close();12}实现方式二:使用BufferedInputStream/BufferedOutputStream高效字节流进行复制文件1privatestaticvoidbufferedStreamCopyFilethrowsIOException{2//使用缓冲字节流进行文件复制3BufferedInputStreambis=newBufferedInputStream;4BufferedOutputStreambos=newBufferedOutputStream;5byte[]b=newbyte[1024];6Integerlen=0;7//一次读取1024字节的数据8while((len=bis.read(b))!=-1){8fw.write;9}1011fr.close();12fw.close();13}实现方式四:使用BufferedReader/BufferedWriter高效字符流进行文件复制1privatestaticvoidbufferedReaderWriterCopyFilethrowsIOException{2//使用带缓冲区的高效字符流进行文件复制3BufferedReaderbr=newBufferedReader;4BufferedWriterbw=newBufferedWriter;56char[]c=newchar[1024];7Integerlen=0;8while((len=br.read(c))!=null){15bw.write;16bw.newLine();17}*/1819br.close();20bw.close();21}以上便是Java中分别使用字节流、高效字节流、字符流、高效字符流四种方式实现文件复制的方法!

背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制。

实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操作

1 private static void streamCopyFile(File srcFile, File desFile) throwsIOException {
2         //使用字节流进行文件复制
3         FileInputStream fi = newFileInputStream(srcFile);
4         FileOutputStream fo = newFileOutputStream(desFile);
5         Integer by = 0;
6         //一次读取一个字节
7         while((by = fi.read()) != -1) {
8 fo.write(by);
9 }
10 fi.close();
11 fo.close();
12     }

实现方式二:使用BufferedInputStream/BufferedOutputStream高效字节流进行复制文件

1 private static void bufferedStreamCopyFile(File srcFile, File desFile) throwsIOException {
2         //使用缓冲字节流进行文件复制
3         BufferedInputStream bis = new BufferedInputStream(newFileInputStream(srcFile));
4         BufferedOutputStream bos = new BufferedOutputStream(newFileOutputStream(desFile));
5         byte[] b = new byte[1024];
6         Integer len = 0;
7         //一次读取1024字节的数据
8         while((len = bis.read(b)) != -1) {
9             bos.write(b, 0, len);
10 }
11 bis.close();
12 bos.close();
13     }

实现方式三:使用FileReader/FileWriter字符流进行文件复制。(注意这种方式只能复制只包含字符的文件,也就意味着你用记事本打开该文件你能够读懂)

1 private static void readerWriterCopyFile(File srcFile, File desFile) throwsIOException  {
2         //使用字符流进行文件复制,注意:字符流只能复制只含有汉字的文件
3         FileReader fr = newFileReader(srcFile);
4         FileWriter fw = newFileWriter(desFile);
5         
6         Integer by = 0;
7         while((by = fr.read()) != -1) {
8 fw.write(by);
9 }
10         
11 fr.close();
12 fw.close();
13     }

实现方式四:使用BufferedReader/BufferedWriter高效字符流进行文件复制(注意这种方式只能复制只包含字符的文件,也就意味着你用记事本打开该文件你能够读懂)

1 private static void bufferedReaderWriterCopyFile(File srcFile, File desFile)  throwsIOException {
2         //使用带缓冲区的高效字符流进行文件复制
3         BufferedReader br = new BufferedReader(newFileReader(srcFile));
4         BufferedWriter bw = new BufferedWriter(newFileWriter(desFile));
5         
6         char[] c = new char[1024];
7         Integer len = 0;
8         while((len = br.read(c)) != -1) {
9             bw.write(c, 0, len);
10 }
11         
12         //方式二
13         /*String s = null;
14 while((s = br.readLine()) != null) {
15 bw.write(s);
16 bw.newLine();
17 }*/
18         
19 br.close();
20 bw.close();
21     }

以上便是Java中分别使用字节流、高效字节流、字符流、高效字符流四种方式实现文件复制的方法!

免责声明:文章转载自《Java实现文件复制的四种方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MSF使用之payload模块Ubuntu中NetBeans C/C++配置、编译下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Lua 字符串

字符串或串(String)是由数字、字母、下划线组成的一串字符。 Lua 语言中字符串可以使用以下三种方式来表示: 单引号间的一串字符。 双引号间的一串字符。 [[和]]间的一串字符。 实例 string1 = "Lua" print(""字符串 1 是"",string1) string2 = 'runoob.com' print("字符串 2...

MS SQLSERVER SELECT FOR XML 中字符的限制问题

采用sqlcommand返回单行的值这种方式查询的xml长度不能超过8000个字符。 ssql = "select * from " + tablename + " FOR XML AUTO,ELEMENTS";                      SqlCommand command = new SqlCommand(ssql, connect...

java去除字符串中的特殊符号或指定的字符

方法一 String regEx="[ `~!@#$%^&*()+=|{}':;',\[\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?]"; //可以在中括号内加上任何想要替换的字符,实际上是一个正则表达式 String aa = " ";//这里是将特殊字符换为aa字符串," "代表直接去掉  P...

3、Python字符编码区分utf-8和utf-8-sig

Python 读取文件首行多了"ufeff"字符串 python读取B.txt文件时,控制台打印首行正常,但是若是用首行内容打开文本的话,就会报错: Traceback (most recent call last): A File "E:/python project/multiProcess/test.py", line 32, in <mo...

SQL模糊查询语句和Escape转义字符

通配符 描述 示例 % 包含零个或更多字符的任意字符串。 WHERE title LIKE '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名。 _(下划线) 任何单个字符。 WHERE au_fname LIKE '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean...

【转帖】C++编译原理 资料

转自:http://blog.csdn.net/shiwenbin333/article/details/5157797 首先是预编译,这一步可以粗略的认为只做了一件事情,那就是“宏展开”,也就是对那些#***的命令的一种展开。       例如define MAX 1000就是建立起MAX和1000之间的对等关系,好在编译阶段进行替换。       例如...