BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStreamReader
每一种流都介绍到了,详细一目了然的详细

下面是字节流常见操作的四种方式:

 import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.IOException; /*
* 四种方式实现大文件数据的读取写入--->复制
* 1.基本字节流一次读取一个字节
* 2.基本字节流一次读取一个字节数组
* 3.高效字节流一次读取一个字节
* 4.高校字节流一次读取一个字节数组
*
*/
public class Test2 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis(); // method1("E:\\b.txt", "c.txt"); //基本字节流一次一个字节
// method2("E:\\b.txt", "c.txt"); //一次一个字节数组
// method3("E:\\b.txt", "c.txt"); //高效字节流一次一个字节
method4("E:\\b.txt", "c.txt"); //高效字节流一次一个字节数组 long end = System.currentTimeMillis();
System.out.println("共耗时: " + (end - start) + "毫秒");
} // 1.基本方法字节流一次读取一个字节
public static void method1(String srcPath, String destPath)
throws IOException {
// 读取数据对象
FileInputStream fis = new FileInputStream(srcPath);
// 写入数据目标文件路径
FileOutputStream fos = new FileOutputStream(destPath);
// 数据读写
// 直接以单字节读取
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 关闭流
fos.close();
fis.close();
} // 2.基本字节读取一次读取一个数组
public static void method2(String srcPath, String destPath)
throws IOException {
// 数据读取的对象封装
FileInputStream fis = new FileInputStream(srcPath);
// 数据写入对象封装
FileOutputStream fos = new FileOutputStream(destPath);
// 数据的读写
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 关闭流
fis.close();
fos.close();
} // 3.高效字节流一次读取一个字节
public static void method3(String srcPath, String destPath)
throws IOException {
// 数据读取对象封装
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcPath));
// 数据写入对象封装
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destPath)); // 数据读写操作
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
} // 关闭流
bos.close();
bis.close();
} // 4.高效字节流读取一个字节数组
public static void method4(String srcPath, String destPath)
throws IOException {
// 数据读取对象封装
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcPath));
// 数据写入对象
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destPath)); // 数据读写操作
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
} // 关闭流
bos.close();
bis.close();
}
}

下面是字符流常见操作的四种方式:

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader; /*
* 需求:复制文本文件
* 分析:如果使用Windows记事本打开之后能读懂就是用字符流,显然此处使用字符流
* 字符流有五种方式:尤其是第五种字符高效流的特殊方式
* 数据源的地址以及目标地址也应该用String封装
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 字符流读取源文件对象
// method1(); // 普通方法的单字节读取
// method2(); //普通的字符数组
       //method3(); //高效的字节数组
method4(); //高效的字节数组
method5(); //按行读取写入 ,这个很常用!!
} private static void method1() throws IOException {
// 源文件对象,读取数据
// InputStreamReader isr = new InputStreamReader(new FileInputStream(
// "c:\\a.txt"));
FileReader fr = new FileReader("c:\\a.txt");
// 目标文件对象,写入数据
FileWriter fw = new FileWriter("dest.txt"); // 数据的复制
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
} // 释放资源
fr.close();
fw.close();
} // 字符数组读取复制文件
private static void method2() throws IOException {
// 数据源文件对象,读取数据
FileReader fr = new FileReader("c:\\a.txt");
// 目标文件对象,写入数据
FileWriter fw = new FileWriter("dest.txt"); // 数据复制
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
fw.flush(); // 字符流在写入数据时一定记得刷新!
} // 释放资源
fr.close();
fw.close();
} // 高效字符数组复制文件--->单字符模式
public static void method3() throws IOException {
// 数据源文件对象,读取数据
BufferedReader br = new BufferedReader(new FileReader("c:\\a.txt"));
// 目标文件对象,写入数据
BufferedWriter bw = new BufferedWriter(new FileWriter("dest1")); // 文件的复制
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
} // 释放资源
bw.close();
br.close();
} // 高效字符数组复制文本文件--->字符数组模式
private static void method4() throws IOException {
// 数据源文件对象,读取数据
BufferedReader br = new BufferedReader(new FileReader("c:\\a.txt"));
// 目标文件对象,写入数据
BufferedWriter bw = new BufferedWriter(new FileWriter("dest1.txt")); // 数据复制
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs);
bw.flush();
} // 释放资源
br.close();
bw.close();
} // 高效字符流的特殊方法--->按行读取写入~!这个是狠狠常用的
private static void method5() throws IOException {
// 数据源文件对象,读取数据
BufferedReader br = new BufferedReader(new FileReader("c:\\a.txt"));
// 目标文件对象,写入数据
BufferedWriter bw = new BufferedWriter(new FileWriter("dest.txt")); // 数据复制操作
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
} // 释放资源
br.close();
bw.close();
}
}

java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr的更多相关文章

  1. JAVA中的字节流与字符流

    字节流与字符流的区别? 字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢? 实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用 ...

  2. Java中的字节流,字符流,字节缓冲区,字符缓冲区复制文件

     一:创建方式 1.建立输入(读)对象,并绑定数据源 2.建立输出(写)对象,并绑定目的地 3.将读到的内容遍历出来,然后在通过字符或者字节写入 4.资源访问过后关闭,先创建的后关闭,后创建的先关闭 ...

  3. Java字节流和字符流,是时候总结一下IO流了

    目录 从接收输入值说起 字节流读取 字符流读取 Scanner 读取 什么是 IO 流 字节流和字符流 字节流 字节输入流 字节输出流 缓冲流的原理 字符流 字符输入流 字符输出流 为什么字符流需要 ...

  4. java中全角半角字符的相互转换的代码

    如下内容是关于java中全角半角字符的相互转换的内容.package com.whatycms.common.util; import org.apache.commons.lang.StringUt ...

  5. 弄清java中的字节与字符

    问题 在java中,一个字符等于多少字节? 或者更详细的问:在java中,一个英文字符等于多少字节?一个中文字符等于多少字节? 答案 Java采用unicode来表示字符,java中的一个char是2 ...

  6. Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式

    Java中多线程使用匿名内部类的方式进行创建3种方式 package cn.edu.ujn.demo; // 匿名内部类的格式: public class ThreadDemo { public st ...

  7. Java 输入/输出——字节流和字符流

    1.流的分类 (1)输入流和输出流(划分输入/输出流时是从程序运行所在内存的角度来考虑的) 输入流:只能从中读取数据,而不能向其写入数据. 输出流:只能向其写入数据,而不能从中读取数据. 输入流主要由 ...

  8. JAVA基础之字节流与字符流

    个人理解: IO流就是将数据进行操作的方式,因为编码的不同,所以对文件的操作就产生两种.最好用字节流,为了方便看汉字等,(已经确定文字的话)可以使用字符流.每个流派也就分为输入和输出,这样就可以产生复 ...

  9. java基础(23):字节流、字符流

    1. 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream Out ...

随机推荐

  1. shell脚本编程-使用结构化命令(if/else)(转)

    11.1 使用if-then语句 格式如下 if语句会执行if行定义的那个命令,如果该命令的退出状态码是0,则then部分的语句就会执行,其他值,则不会   1 2 3 4 if command th ...

  2. 在Visual Studio 2015中运行OPENGL

    Starting an OpenGL project in VS 2015 is really easy, thanks to the NupenGL.Core nuget package. Here ...

  3. Ubuntu 上安装 Freemind 并支持中文

    Ubuntu 上安装 Freemind 并支持中文 JAVA 运行时 Freemind 是一个使用 Java 编写的思维导图工具,在安装时,需要到 Java 运行时(使用 OpenJRE 或 Orac ...

  4. Android --ListView模板

    调整了近一上午的模板 ListView表头 <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  5. python_类

    1. 对象的概念 对象包括特性和方法.特性只是作为对象的一部分的变量,方法则是存储在对象内的函数.对象中的方法和其他函数的区别在于方法总是将对象作为自己的第一个参数,这个参数一般称为self. 2. ...

  6. SWIFT Button的基本用法

    import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: ...

  7. HTML-移动端如何使用css让百分比布局的弹窗水平和垂直方向上居中

    pc端让一个弹窗水平和垂直方向居中,在知道弹窗宽高的情况下很好计算,只需要用如下css即可: #date{ width: 300px; height: 300px; position: absolut ...

  8. SQL Server中使用正则表达式

    SQL Server 2005及以上版本支持用CLR语言(C# .NET.VB.NET)编写过程.触发器和函数,因此使得正则匹配,数据提取能够在SQL中灵活运用,大大提高了SQL处理字符串,文本等内容 ...

  9. leetcode8 String to Integer (atoi)

    题目需求: 输入一个字符串,输出对应的int值 特殊处理: 输入: null  输出:0 输入: "a122"  输出:0 输入: "   1233"  输出: ...

  10. IOS推荐学习网站

    1> 个人博客:技术大牛 唐巧:http://blog.devtang.com/blog/archives/ 王巍:http://www.onevcat.com 破船之家:http://beyo ...