JAVA I/O之文件复制
有没有大佬告诉我这个不要了的代码插入区(就现在这句话的区域)怎么删掉。。。。。。。
//一个字节一个字节的复制
public static void fun() throws IOException {
FileInputStream fis = new FileInputStream("F:/abc.txt");
FileOutputStream fos = new FileOutputStream("F:/字节流复制(一个字节一个字节).txt");
int by = 0;
while ((by=fis.read()) != -1) {
fos.write(by);
}
fis.close();
fos.close();
}
//1024字节数组复制(加入数组缓冲区)
public static void fun1() throws IOException {
FileInputStream fis = new FileInputStream("F:/abc.txt");
FileOutputStream fos = new FileOutputStream("F:/字节流复制(1024字节数组).txt");
int len = 0;
byte[] bytes =new byte[1024];
while ((len=fis.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
// 一个字节一个字节复制并用了缓冲流
public static void fun2() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:/abc.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:/字节缓冲流复制(一个字节一个字节).txt")); int by = 0;
while ((by=bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 1024字节数组复制并用了缓冲流 (加入数组缓冲区)
public static void fun3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:/abc.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:/字节缓冲流复制(1024字节数组).txt"));
int len = 0;
byte[] bytes =new byte[1024];
while ((len=bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
//字符缓冲流复制文(一行一行可保留格式)
public static void fun4() throws IOException {
FileInputStream fileInputStream = new FileInputStream("F:/abc.txt");
//出现乱码问题的原因:文件的编码,系统的编码,java的默认编码有冲突。
//假如我们用FileReader这些类来读取系统文件,它调用的字符编码是java默认的UTF-8,但是一般WINDOW的TXT默认是ANSI,而自己本机的中文编码是GBK
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"GBK");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); FileOutputStream fileOutputStream = new FileOutputStream("F:/字符缓冲流复制文件.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); String line = null;
while (null != (line = bufferedReader.readLine())) {
bufferedWriter.write(line);
bufferedWriter.newLine();
} if (bufferedWriter != null || outputStreamWriter != null || fileOutputStream != null) {
bufferedWriter.close();
outputStreamWriter.close();
fileOutputStream.close();
}
if (bufferedReader != null || inputStreamReader != null || fileInputStream != null) {
bufferedReader.close();
inputStreamReader.close();
fileInputStream.close();
}
} //字节流(ByteArrayInputStream)当你资源不足够用时,选择BufferedOutputStream是最佳的选择, 当你选择快速完成一个作业时,可以选择ByteArrayOutputStream之类的输出流https://www.cnblogs.com/yixiu868/p/8144670.html
public static void fun5() throws IOException {
FileInputStream fileInputStream = new FileInputStream("F:/abc.txt");
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = fileInputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
byte[] bytes = bos.toByteArray();
FileOutputStream fileOutputStream = new FileOutputStream("F:/字节流(ByteArrayInputStream).txt");
fileOutputStream.write(bytes); if (fileOutputStream != null) {
fileOutputStream.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
}
public static void main(String[] arg) throws IOException{ long start,end;
start = System.currentTimeMillis();
fun();
end = System.currentTimeMillis();
System.out.println("一个字节一个字节的复制(字节流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun1();
end = System.currentTimeMillis();
System.out.println("1024字节数组复制(字节流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun2();
end = System.currentTimeMillis();
System.out.println("一个字节一个字节的复制(缓冲流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun3();
end = System.currentTimeMillis();
System.out.println("1024字节数组复制(缓冲流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun4();
end = System.currentTimeMillis();
System.out.println("字符缓冲流复制文件花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun5();
end = System.currentTimeMillis();
System.out.println("字节流(ByteArrayInputStream)花费时间:" + (end - start) + "ms"); }
执行结果:

下图为IO中的字节流和字符流,每一种又分为相应的输入流和输出流。需了解的基础概念:字符流(Reader,Writer),字节流,缓冲流(Buffered);使用时按照各自需求去搜寻对应相关资料即可。

JAVA I/O之文件复制的更多相关文章
- JAVA File方法各类文件复制操作
import java.io.*; public class AllFile { public static void main(String[] args) throws Exception {// ...
- Java实现简单的文件复制
public class FileCopy { public static void main(String[] args) { String path = "d:\\1.txt" ...
- Java中创建操作文件和文件夹的工具类
Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- Java实现文件复制的四种方式
背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...
- Java基础之读文件——使用通道复制文件(FileBackup)
控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileC ...
- java中的IO流之文件复制
O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...
- 黑马程序员——java基础之文件复制
---------------------- ASP.Net+Unity开发..Net培训.期待与您交流!---------------------- <a href="http:// ...
- java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝
接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...
- java学习之实现文件的复制
package com.io; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; /** * 文件 ...
随机推荐
- MySQL-子查询,派生表,通用表达式
MySQL-子查询 MySQL子查询是嵌套在另一个查询中的查询. MySQL子查询还可以嵌套在另一个子查询中. MySQL子查询称为内部查询,而包含子查询的查询称为外部查询. 查询返回在位于美国(US ...
- Mac OS X 10.10下Versions crash的问题
升级完系统.Versions由于兼容性到问题,各种闪退,网上搜索了一下.知乎到一个帖子提到了暂时解决的方法,例如以下: Blackpixel 正在研究此崩溃的修复方案.暂时解决方式例如以下: 在文本编 ...
- (七)Java 变量类型
Java 变量类型 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ...
- javascript总结02
1 如何打开和关闭一个新的窗口? 2 Window对象的哪个属性能返回上一个浏览页面? 3 一次或多次执行一段程序的函数是什么? 定时函数 4 如何查找并访问节点? 5 给表格新增行和单元格的方法分别 ...
- Python Flask Web 框架入门
Python Flask 目录 本文主要借鉴 letiantian 的文章 http://www.letiantian.me/learn-flask/ 一.简介 二.安装 三.初始化Flask 四.获 ...
- js moment.js日期操作类 datetime,日期操作,dayjs
http://momentjs.com/ JS时间处理插件MomentJS https://juejin.im/post/5a2bdc55f265da432b4abf5e Day.js 2kB超轻量时 ...
- bzoj [Usaco2010 Hol]cowpol 奶牛政坛【树链剖分】
意识流虚树 首先考虑只有一个党派,那么可以O(n)求树的直径,步骤是随便指定一个根然后找距离根最远点,然后再找距离这个最远点最远的点,那么最远点和距离这个最远点最远的点之间的距离就是直径 那么考虑多党 ...
- iOS静态库.Framework制作
首先要解释一下什么是库,库(Library)其实就是一段编译好的二进制代码,加上头文件就可以供别人使用,一般会有两种情况要用到库: 某些代码需要给别人使用,但是我们不希望别人看到源码,就需要以库的形式 ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- O - Combinations (组合数学)
Description Computing the exact number of ways that N things can be taken M at a time can be a great ...