* 复制文本文件。
 *
 * 数据源:从哪里来
 * a.txt -- 读取数据 -- FileInputStream 
 *
 * 目的地:到哪里去
 * b.txt -- 写数据  -- FileOutputStream
 *
 * java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
 *
 * 这一次复制中文没有出现任何问题,为什么呢?
 * 上一次我们出现问题的原因在于我们每次获取到一个字节数据,就把该字节数据转换为了字符数据,然后输出到控制台。
 * 而这一次呢?确实通过IO流读取数据,写到文本文件,你读取一个字节,我就写入一个字节,你没有做任何的转换。
 * 它会自己做转换。

 public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("a.txt");
// 封装目的地
FileOutputStream fos = new FileOutputStream("b.txt"); int by = ;
while ((by = fis.read()) != -) {
fos.write(by);
} // 释放资源(先关谁都行)
fos.close();
fis.close();
}
}

* 通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非常好的。
 * 既然是这样的话,那么,java开始在设计的时候,它也考虑到了这个问题,就专门提供了带缓冲区的字节类。
 * 这种类被称为:缓冲区类(高效类)
 * 写数据:BufferedOutputStream
 * 读数据:BufferedInputStream
 *
 * 构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。
 *
 * 为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
 * 原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。

 public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedOutputStream(OutputStream out)
// FileOutputStream fos = new FileOutputStream("bos.txt");
// BufferedOutputStream bos = new BufferedOutputStream(fos);
// 简单写法
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("bos.txt")); // 写数据
bos.write("hello".getBytes()); // 释放资源
bos.close();
}
}

* 字节流四种方式复制文件:
 * 基本字节流一次读写一个字节: 共耗时:117235毫秒
 * 基本字节流一次读写一个字节数组: 共耗时:156毫秒
 * 高效字节流一次读写一个字节: 共耗时:1141毫秒
 * 高效字节流一次读写一个字节数组: 共耗时:47毫秒

 public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// method1("e:\\哥有老婆.mp4", "copy1.mp4");
// method2("e:\\哥有老婆.mp4", "copy2.mp4");
// method3("e:\\哥有老婆.mp4", "copy3.mp4");
method4("e:\\哥有老婆.mp4", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
} // 高效字节流一次读写一个字节数组:
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString)); byte[] bys = new byte[];
int len = ;
while ((len = bis.read(bys)) != -) {
bos.write(bys, , len);
} bos.close();
bis.close();
} // 高效字节流一次读写一个字节:
public static void method3(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString)); int by = ;
while ((by = bis.read()) != -) {
bos.write(by); } bos.close();
bis.close();
} // 基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString); byte[] bys = new byte[];
int len = ;
while ((len = fis.read(bys)) != -) {
fos.write(bys, , len);
} fos.close();
fis.close();
} // 基本字节流一次读写一个字节
public static void method1(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString); int by = ;
while ((by = fis.read()) != -) {
fos.write(by);
} fos.close();
fis.close();
}
}

java笔记之IO4文本复制的更多相关文章

  1. 用FileInputStream实现文本复制

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 用f ...

  2. java文件和文件夹复制、删除、移动操作

    java文件和文件夹复制.删除.移动操作 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...

  3. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  4. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

  5. Effective java笔记(二),所有对象的通用方法

    Object类的所有非final方法(equals.hashCode.toString.clone.finalize)都要遵守通用约定(general contract),否则其它依赖于这些约定的类( ...

  6. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  7. java 7中文件的复制移动

    在java 7 中文件移动复制简单方法 文件移动 Path source=Paths.get("D:\\sa\\123.txt"); Path target=Paths.get(& ...

  8. 转载---Java集合对象的深度复制与普通复制

    原博文:http://blog.csdn.net/qq_29329775/article/details/49516247 最近在做算法作业时出现了错误,原因是没有弄清楚java集合的深度复制和浅度复 ...

  9. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

随机推荐

  1. codeforces Rockethon 2015 C Second price auction [想法]

    传送门 C. Second price auction time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. HDU 5695 Gym Class

    拓扑排序. #include<cstdio> #include <iostream> #include<cstring> #include<cmath> ...

  3. 关于MySQL的boolean和tinyint(1)

    原文:http://blog.csdn.net/woshixuye/article/details/7089508 MySQL保存boolean值时用1代表TRUE,0代表FALSE.boolean在 ...

  4. android手机rootROM下载地址

    https://download.mokeedev.com/ https://download.lineageos.org/

  5. 【APUE】信号量、互斥体和自旋锁

    http://www.cnblogs.com/biyeymyhjob/archive/2012/07/21/2602015.html http://blog.chinaunix.net/uid-205 ...

  6. 运维平台之CMDB系统建设

    CMDB是运维的基础核心系统,所有的元数据和共享数据管理源,类似于业务中的账号平台的作用.本篇文章,我将从概念篇.模型篇.到实现与实施篇具体的进行阐述. CMDB也称配置管理,配置管理一直被认为是 I ...

  7. vue - 官方 - 上手

    Vue和其它框架一样,有用CDN或本地JavaScript框架,国内我推荐 bootstrap cdn. 为什么很多人选择CDN呢? CDN:内容分发网络(不同区域不同服务器,更快),减少本地服务器压 ...

  8. 关于对FLASH开发,starling、starling feathers、starling MVC框架的理解

    说在前头:楼主之前没有不论什么flash开发经验,仅仅是从一次尝试中总结自己的理解和经验而已.假设有写的不正确的地方,欢迎大家指正. 前一段时间尝试想用flash(as3)又一次制作一下之前做的一个游 ...

  9. java notify notifyAll

    notify()和notifyAll()都是Object对象用于通知处在等待该对象的线程的方法. void notify(): 唤醒一个正在等待该对象的线程.void notifyAll(): 唤醒所 ...

  10. CodeForces 404 Marathon ( 浮点数取模 -- 模拟 )

    B. Marathon time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...