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; /** * 文件 ...
随机推荐
- CentOS 6.4安装Ganglia
samba 1.这里安装的是3.1.7版本,web前端是最新版本,安装前期环境(yum源用的是本地的) yum -y insatll php php-gd rrdtools apr-devel apr ...
- 容器与容器编排实战系列 1 -- Docker 安装
CentOS7.4 下安装Docker 详细步骤 第一步:安装Docker yum install -y yum-utils device-mapper-persistent-data lvm2 yu ...
- 【bzoj2464】中山市选[2009]小明的游戏
直接转换成最短路 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstr ...
- 修改spring boot 启动logo
修改spring boot 启动logo 在项目添加文件banner.txt,将需要的logo写在里面 效果:
- 8 Range 对象
8.1 引用Range 引用Range的主要方法: Application.ActiveCell Application.Range Application.Selection Worksheet.C ...
- rsync单向同步
系统版本:Centos X64 6.4(最小化安装) 先安装依赖包 [root@localhost ~]# yum install vim wget lsof gcc make cmake makec ...
- libnids TCP数据流重组,显示TCP连接过程的程序总无法捕获数据包解决办法:
法一: 指定可用网卡: nids_params.device="lo"; 法二: nids.h中有这么一段: struct nids_chksum_ctl { u_int ne ...
- Runtime ----- 带你上道
在IOS开发和学习过程中,我们经常会接触到一个词: Runtime .很多开发者对之既熟悉又陌生,基本都是浅尝辄止,达不到灵活使用的水平(话说开发中也确实不经常用..)本文和大家一起研究一下,Run ...
- 深入浅出Android makefile(2)--LOCAL_PATH(转载)
转自:http://nfer-zhuang.iteye.com/blog/1752387 一.说明 上文我们对acp的Android.mk文件做了一个大致的描述,使得大家对Android.mk文件有了 ...
- bzoj 1630: [Usaco2007 Demo]Ant Counting【dp】
满脑子组合数学,根本没想到dp 设f[i][j]为前i只蚂蚁,选出j只的方案数,初始状态为f[0][0]=1 转移为 \[ f[i][j]=\sum_{k=0}^{a[i]}f[i-1][j-k] \ ...