java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr

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的更多相关文章
- JAVA中的字节流与字符流
字节流与字符流的区别? 字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢? 实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用 ...
- Java中的字节流,字符流,字节缓冲区,字符缓冲区复制文件
一:创建方式 1.建立输入(读)对象,并绑定数据源 2.建立输出(写)对象,并绑定目的地 3.将读到的内容遍历出来,然后在通过字符或者字节写入 4.资源访问过后关闭,先创建的后关闭,后创建的先关闭 ...
- Java字节流和字符流,是时候总结一下IO流了
目录 从接收输入值说起 字节流读取 字符流读取 Scanner 读取 什么是 IO 流 字节流和字符流 字节流 字节输入流 字节输出流 缓冲流的原理 字符流 字符输入流 字符输出流 为什么字符流需要 ...
- java中全角半角字符的相互转换的代码
如下内容是关于java中全角半角字符的相互转换的内容.package com.whatycms.common.util; import org.apache.commons.lang.StringUt ...
- 弄清java中的字节与字符
问题 在java中,一个字符等于多少字节? 或者更详细的问:在java中,一个英文字符等于多少字节?一个中文字符等于多少字节? 答案 Java采用unicode来表示字符,java中的一个char是2 ...
- Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式
Java中多线程使用匿名内部类的方式进行创建3种方式 package cn.edu.ujn.demo; // 匿名内部类的格式: public class ThreadDemo { public st ...
- Java 输入/输出——字节流和字符流
1.流的分类 (1)输入流和输出流(划分输入/输出流时是从程序运行所在内存的角度来考虑的) 输入流:只能从中读取数据,而不能向其写入数据. 输出流:只能向其写入数据,而不能从中读取数据. 输入流主要由 ...
- JAVA基础之字节流与字符流
个人理解: IO流就是将数据进行操作的方式,因为编码的不同,所以对文件的操作就产生两种.最好用字节流,为了方便看汉字等,(已经确定文字的话)可以使用字符流.每个流派也就分为输入和输出,这样就可以产生复 ...
- java基础(23):字节流、字符流
1. 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream Out ...
随机推荐
- thinkphp扩展 根据前端批量建立字段
/*批量添加字段辅助*/ function add_colum($tabel){ foreach ($_POST as $key=>$value){ $array[] = "add & ...
- SQLServer 统计数据量
做一个项目,第一件事情就是问:“这个数据库多大?” 下面是统计数据库数据量大小的方法 通常我们会使用命令: "sp_helpdb @dbname" 例如,查询数据库"te ...
- T4模版生成多个实体文件时,提示找不到 Host
T4模版生成多个实体文件时,提示找不到 Host 使用以下方法,把hostspecific改为true就可以了 hostspecific:有效值true.false,默认为false.如果将此特性的值 ...
- proguard使用
Proguard用于混淆java代 码,使代码变为由难懂的,无规律的字符命名的各种方法和类,保护自己的劳动成果.个人认为proguard混淆纯java项目比较理想,比如j2me的 MIDLET,如 ...
- C# WebSocket 服务端示例代码 + HTML5客户端示例代码
WebSocket服务端 C#示例代码 using System; using System.Collections.Generic; using System.Linq; using System. ...
- shell中&&和||的使用方法_转
shell中&&和||的使用方法 &&运算符: command1 && command2 &&左边的命令(命令1)返回真(即返 ...
- linux内核编译相关
参考:http://www.arm.linux.org.uk/docs/kerncomp.php 一. 内核编译1) linux 2.4make clean/make mrpropermake dep ...
- iOS添加自动更新的代码
- (void)versionUpdate{ //获得当前发布的版本 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_ ...
- HTTPS and the TLS handshake protocol阅读笔记
目的 为能够透彻理解HTTPS报文交互过程,做此笔记. 本文大部分内容来自 : http://albertx.mx/blog/https-handshake/ http://www.cnblogs.c ...
- css背景图片拉伸 以及100% 满屏显示
如何用css背景图片拉伸 以及100% 满屏显示呢?这个问题听起来似乎很简单.但是很遗憾的告诉大家.不是我们想的那么简单. 比如一个容器(body,div,span)中设定一个背景.这个背景的长宽值在 ...