知识点:基于抽象基类字节流(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流总结(一)的更多相关文章

  1. java中的IO流

    Java中的IO流 在之前的时候我已经接触过C#中的IO流,也就是说集中数据固化的方式之一,那么我们今天来说一下java中的IO流. 首先,我们学习IO流就是要对文件或目录进行一系列的操作,那么怎样操 ...

  2. Java中的IO流总结

    Java中的IO流总结 1. 流的继承关系,以及字节流和字符流. 2. 节点流FileOutputStream和FileInputStream和处理流BufferedInputStream和Buffe ...

  3. Java中的IO流大体介绍

    由于Java中的IO流是在是知识点繁多,所以我大约花了1周的时间将其整理起来.但是整理起来后并不是将完事了,我还是要分字节流和字符流来讲述.然后字节流和字符流中还有是否带有缓冲流. 讲述完IO流后我将 ...

  4. Java中的IO流,Input和Output的用法,字节流和字符流的区别

    Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...

  5. Java中的IO流(五)

    上一篇<Java中的IO流(四)>记录了一下Properties类,此类不属于IO流,它属于集合框架.接下来说一下IO流中的其它流 一,打印流PrintStream PrintStream ...

  6. Java中的IO流(六)

    上一篇<Java中的IO流(五)>把流中的打印流PrintStream,PrintWriter,序列流SequenceInputStream以及结合之前所记录的知识点完成了文件的切割与文件 ...

  7. JAVA 中的IO流

    Java中的IO流是用来处理设备与设备之前的数据传输,在java中以流的形式传输.流分为两类:字节流和字符流. 字节流:InputStream,OutPutSteam.(计算机内的数据都是以字节存储的 ...

  8. Java中的IO流(四)

    上一篇<Java中的IO流(三)>把IO流中的文件及目录操作的对象File类记录了一下,本篇把本不属性IO流但又和IO流有关系的一个对象作一下记录,此对象本属于集合框架里的一个子集,即Pr ...

  9. Java中的IO流(三)

    上一篇<Java中的IO流(二)>把学习Java的字符流以及转换流作了一下记录,从本篇开始将把IO流中对文件或文件夹操作的对象File类的学习进行一下记录. 一,File类的构造函数及字段 ...

  10. Java中的IO流(二)

    上一篇<Java中的IO流(一)>把学习IO流的字符流作了一下记录,本篇把字节流记录一下. 一,Java中的字节流 Java中的字节流的操作方式与字符流的操作方式大致相同,连方法名都是类似 ...

随机推荐

  1. csu 1756: Prime

    1756: Prime Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 128 Mb     Submitted: 281    ...

  2. python while循环 - python基础入门(9)

    经过昨天的学习,相信大家已经对 python的条件判断表达式if/else 有一定的了解了,那么我们今天配合昨天的课程讲解一个新概念 – while循环 . 都说程序源于生活,假如有这样一个场景:老师 ...

  3. 泛微E-cology OA /weaver/ 代码执行漏洞

    泛微E-cology OA /weaver/代码执行漏洞 泛微e-cology OA Beanshell组件远程代码执行 分析文章:https://dwz.cn/bYtnsKwa http://127 ...

  4. Mac OS X下把 /etc/sudoers 写错了怎么办?(转载https://blog.csdn.net/robertsong2004/article/details/53725285)

    重要的事情先说一下,首先为了回避这个问题,一定要用 visudo 来改 /etc/sudoers 文件. 问题描述: 1. 用  sudo vi 直接改 /etc/sudoers 并覆盖原文件. 2. ...

  5. [转帖]MMU内存管理单元

    MMU内存管理单元 https://www.cnblogs.com/alantu2018/p/9002309.html 之前对这一块一直不理解 最近学习了点 CPU time slice 以及 con ...

  6. C++:标准C函数(随机数,时间函数)

    介绍 ANSI组织定义了C标准和标准库函数. 使用标准C函数优点: 使用标准C函数在任何平台上都支持,使得同一个源码,在Windows编译运行的结果和Linux上编译运行结果相同,无需更改代码. 随机 ...

  7. php文件操作类

    <?php /** *本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录 * 列出目录里的文件等功能,路径后面别忘了加"/" */ clas ...

  8. python学习-19 字典

    字典dict 1.dic = {key:value,key:value} 字典有{ }括住,字典的value可以是任意值,字典的key的值不包括列表和字典 di = {"age": ...

  9. s5p6818 从SD卡启动程序(制作SD启动卡)

    背景: 最近在学习uboot,其中有一步很重要的任务就是需要实现uboot 的验证,没有办法验证uboot是不是自己做的,那么整个开发就会收到阻碍.另外,从公司现在开发的板子来看,uboot从sd卡启 ...

  10. 在一台服务器上启动多个Broker

    1:把整个conf文件夹复制一份,比如叫做conf22:修改里面的activemq.xml文件(1)里面的brokerName 不能跟原来的重复(2)数据存放的文件名称不能重复,比如:<kaha ...