java中的io流总结(一)
知识点:基于抽象基类字节流(InputStream和OutputStream)、字符流(Reader和Writer)的特性,处理纯文本文件,优先考虑使用字符流BufferedReader/BufferedWriter,其他的考虑使用字节流(BufferedInputStream/BufferedOutputStream),之所以使用缓冲流读写很快
(一)io结构图

字节流和字符流的区别:
1.字节流处理所有的类型数据,如:图片,视频等等,而字符流只能处理字符数据,如果处理纯文本文件,优先考虑使用字符流,其他的考虑使用字节流;
2.字节流读到一个字节就返回一个字节,字符流使用字节流读到一个或者多个字节,去查编码表,将查到的字符返回(中文对应的字节数为两个)。
(二)示例代码
(1) 节点流 FileInputStream/FileOutStream(字节流)读写文件实现文件的复制
import java.io.*;
/*
IO体系
抽象基类 节点流(文件流) 缓冲流(处理流的一种)
InputStream FileInputStream BufferedInputStream
OutputStream FileOutputStream BufferedOutputStream (flush)
Reader FileReader BufferedReader (readline)
Writer FileWriter BufferedWriter (flush)
*/
public class IOTestInputOutputStream {
//复制文件
@Test
public void TestFileInputOutputStream(){
//TestCopyFile("E:\\test\\io\\test2.txt","E:\\test\\io\\test8.txt"); //复制文本文件
//TestCopyFile("E:\\test\\io\\1.png","E:\\test\\io\\2.png");//图片
long start=System.currentTimeMillis();
TestCopyFile("E:/test/io/1.flv","E:/test/io/2.flv");
long end=System.currentTimeMillis();
System.out.println("时间:"+(end-start));//复制视频48.3M需要6151毫秒
}
public void TestCopyFile(String src,String dest){
File file1=new File(src);
File file2=new File(dest); FileInputStream fis=null;
FileOutputStream fos=null;
try {
int len;
byte[] b=new byte[50];
fis=new FileInputStream(file1);
fos=new FileOutputStream(file2); while (((len=fis.read(b))!=-1)) {
fos.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(2) 节点流 FileReader/FileWriter(缓冲流)读写文件实现文件的复制
import java.io.*;
/*
FileReader、FileWriter 可以实现文本文件复制
对于非文本文件(视频文件、音频文件、图片),只能使用字节流
*/
public class FileReaderFileWriter {
//复制文本文件
@Test
public void FileReaderAndFileWriter(){
CopyFileRederAndWriter("E:/test/io/3.txt","E:/test/io/4.txt");
//CopyFileRederAndWriter("E:/test/io/1.png","E:/test/io/3.png"); //文件复制出错
}
public void CopyFileRederAndWriter(String src,String desc){
FileReader fr=null;
FileWriter fw=null;
try {
File file1=new File(src);
File file2=new File(desc);
fr=new FileReader(file1);
fw=new FileWriter(file2);
char[] c=new char[20];
int len;
while ((len=fr.read(c))!=-1){
String str=new String(c);
fw.write(str,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(3) 缓冲流 BufferedInputStream/BufferedInputStream 读写文件实现文件的复制
使用缓冲流较文件读写熟读更快,如下例子中读写48.4M的视频文件,文件需要6151毫秒,而缓冲只需要170毫秒,实际应用中使用缓冲流,将文件作为参数传入缓冲中
@Test
public void testBufferedInputOutputStream(){
long start=System.currentTimeMillis();
CopyBufferedInputOutputStream("E:/test/io/1.flv","E:/test/io/2.flv");
long end=System.currentTimeMillis();
System.out.println("时间:"+(end-start));//复制视频48.3M需要170毫秒,较节点流速度快
} public void CopyBufferedInputOutputStream(String src,String desc){
BufferedInputStream bis=null;
BufferedOutputStream bos=null; try {
File file1=new File(src);
File file2=new File(desc); FileInputStream fis=new FileInputStream(file1);
FileOutputStream fos=new FileOutputStream(file2); bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
byte[] b=new byte[50];
int len;
while ((len=bis.read(b))!=-1){
bos.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//先关闭输出流,后关闭输入流
if (bos!=null){
try {
bos.flush();//清空缓冲区内的数据,最后一次
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(4) 缓冲流 BufferedReader/BufferedWriter读写文件实现文件的复制
@Test
public void testBufferedReaderAndWriter(){
BufferedReader br=null;
BufferedWriter bw=null; try {
File file1=new File("E:/test/io/3.txt");
File file2=new File("E:/test/io/5.txt");
FileReader fr=new FileReader(file1);
FileWriter fw=new FileWriter(file2);
br=new BufferedReader(fr);
bw=new BufferedWriter(fw); String str;
while ((str=br.readLine())!=null){//按行读
// System.out.println(str+"\n");
bw.write(str+"\n");//换行写
// bw.newLine();//换行写
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bw!=null){
try {
bw.flush();//清空缓冲区内的数据,最后一次
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}
java中的io流总结(一)的更多相关文章
- java中的IO流
Java中的IO流 在之前的时候我已经接触过C#中的IO流,也就是说集中数据固化的方式之一,那么我们今天来说一下java中的IO流. 首先,我们学习IO流就是要对文件或目录进行一系列的操作,那么怎样操 ...
- Java中的IO流总结
Java中的IO流总结 1. 流的继承关系,以及字节流和字符流. 2. 节点流FileOutputStream和FileInputStream和处理流BufferedInputStream和Buffe ...
- Java中的IO流大体介绍
由于Java中的IO流是在是知识点繁多,所以我大约花了1周的时间将其整理起来.但是整理起来后并不是将完事了,我还是要分字节流和字符流来讲述.然后字节流和字符流中还有是否带有缓冲流. 讲述完IO流后我将 ...
- Java中的IO流,Input和Output的用法,字节流和字符流的区别
Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...
- Java中的IO流(五)
上一篇<Java中的IO流(四)>记录了一下Properties类,此类不属于IO流,它属于集合框架.接下来说一下IO流中的其它流 一,打印流PrintStream PrintStream ...
- Java中的IO流(六)
上一篇<Java中的IO流(五)>把流中的打印流PrintStream,PrintWriter,序列流SequenceInputStream以及结合之前所记录的知识点完成了文件的切割与文件 ...
- JAVA 中的IO流
Java中的IO流是用来处理设备与设备之前的数据传输,在java中以流的形式传输.流分为两类:字节流和字符流. 字节流:InputStream,OutPutSteam.(计算机内的数据都是以字节存储的 ...
- Java中的IO流(四)
上一篇<Java中的IO流(三)>把IO流中的文件及目录操作的对象File类记录了一下,本篇把本不属性IO流但又和IO流有关系的一个对象作一下记录,此对象本属于集合框架里的一个子集,即Pr ...
- Java中的IO流(三)
上一篇<Java中的IO流(二)>把学习Java的字符流以及转换流作了一下记录,从本篇开始将把IO流中对文件或文件夹操作的对象File类的学习进行一下记录. 一,File类的构造函数及字段 ...
- Java中的IO流(二)
上一篇<Java中的IO流(一)>把学习IO流的字符流作了一下记录,本篇把字节流记录一下. 一,Java中的字节流 Java中的字节流的操作方式与字符流的操作方式大致相同,连方法名都是类似 ...
随机推荐
- ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法
这个是非常常用的案例,ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法/** * 单文件上传 * name:表单上传文件的名字 * ext: 文件允许的后缀,字符串形式 * path:文件保 ...
- 【计算机视觉】BING: Binarized Normed Gradients for Objectness Estimation at 300fps
BING: Binarized Normed Gradients for Objectness Estimation at 300fps Ming-Ming Cheng, Ziming Zhang, ...
- angular入门 - 环境安装及项目创建
1.安装node.js 下载,安装,在终端测试安装是否成功:node -v(查看nodejs版本) npm -v(查看npm版本) 下载地址:https://nodejs.org/en/downloa ...
- 学习Elasticsearch原理笔记
Elasticsearch是一个分布式可拓展的实时搜索和分析引擎 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索 实时分析的分布式搜索引擎 可以拓展到上百台服务器,处理PB级别的结构化或 ...
- 学习Python类的心得
类的注意事项 1)命名规则 需要注意的是,在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的, 是特殊变量,特殊变量是可以直接访问的,不是private变量, ...
- go if 便捷语句
之前使用java C#没这么用过. 绝对新技能 if v := math.Pow(x, n); v < lim { 跟 for 一样,`if` 语句可以在条件之前执行一个简单的语句. 由这个语 ...
- asp.net core-16.EF Core Migration
左边的是基于visual studio code 右边的是基于visual studio 如果想要在数据库的AspNetUsers表里添加一列 然后可以发现 在Data下的Migrations文件夹下 ...
- prometheus+grafana监控nginx
被监控机器环境搭建&配置 nginx-module-vts下载: https://github.com/vozlt/nginx-module-vts nginx-module-vts安装 un ...
- 解决django的后台管理界面添加中文内容乱码问题
在使用django migrate功能时,默认数据库的字符集不是utf8. 是latin 1,然后在后台管理model时,不允许有中文字符插入 解决方案: 在使用migrate建库之前先把数据库建立起 ...
- office2016激活码 最新各个版本 激活
office2016专业版激活密钥 Microsoft Office 2016 Pro Plus Retail 零售版序列号密钥: BHXN7-MQB36-MTHQ4-8MHKV-CYT97 Micr ...