有没有大佬告诉我这个不要了的代码插入区(就现在这句话的区域)怎么删掉。。。。。。。
        //一个字节一个字节的复制
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之文件复制的更多相关文章

  1. JAVA File方法各类文件复制操作

    import java.io.*; public class AllFile { public static void main(String[] args) throws Exception {// ...

  2. Java实现简单的文件复制

    public class FileCopy { public static void main(String[] args) { String path = "d:\\1.txt" ...

  3. Java中创建操作文件和文件夹的工具类

    Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...

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

    背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...

  5. Java基础之读文件——使用通道复制文件(FileBackup)

    控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileC ...

  6. java中的IO流之文件复制

    O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...

  7. 黑马程序员——java基础之文件复制

    ---------------------- ASP.Net+Unity开发..Net培训.期待与您交流!---------------------- <a href="http:// ...

  8. java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝

    接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...

  9. java学习之实现文件的复制

    package com.io; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; /** * 文件 ...

随机推荐

  1. Broccoli &amp; Babel使用演示样例

    1 创建项目project文件夹:test 2 在test下运行 npm init 按提示填写package.json文件 3 安装broccoli命令行工具broccoli-cli npm inst ...

  2. 2.eclipse 插件安装烦死人(2)

    安装插件的实际结果是:(烦死人),要不是很多插件找不到,要不就是版本不对,要不就是下载了装上没有效果,要不就是在线安装(速度爆慢),好不容易等到结果了,结果是些错…… 最后我的eclipse 3.5. ...

  3. java SocketChannel and ServerSocketChannel

    1 SocketChannel 1.1 打开一个SocketChannel SocketChannel socketChannel = SocketChannel.open(); socketChan ...

  4. js中createlement和creatTextnode属性

    js中可以使用creatElement方法创造一个新的元素,使用creatTextnode创造一个新的text文本元素. 之后使用appendchild插入到已存在的元素中. ** window.on ...

  5. MPEG2、MPEG4、H264的差异

    iso(国际标准化组织) MPEG系列 ITU-T(国际电联)h.系列 H.264:iso与ITU联合制定,数据压缩比超牛! MPEG-2简介 MPEG-2制定于1994年,设计目标是高级工业标准的图 ...

  6. SemaphoreSlim

    https://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim(v=vs.110).aspx Represents a ...

  7. [Codeforces 496E] Distributing Parts

    [题目链接] https://codeforces.com/contest/496/problem/E [算法] 按右端点排序 , 每个乐曲优先选取的左端点最大的演奏家 用std :: set维护贪心 ...

  8. bzoj2060

    树形dp dp[x][0]表示x点父亲没选,dp[x][1]表示x点父亲选了,然后dp[x][0]=max(sigma(dp[c[x]][0]),sigma(dp[c[x]][1])) dp[x][1 ...

  9. openstack dnsmasq调试

  10. 可视图linux--ubuntu的安装以及tool的安装

    1.下载ubuntu:http://cn.ubuntu.com/download/ 右键然后选择复制链接地址,然后将这个地址粘贴到迅雷,下载速度会比浏览器下载快. 2.创建虚拟机,安装ubuntu-- ...