1.流的分类

 按照数据流向的不同:输入流和输出流

 按照处理数据的单位不同:字节流((非文本文件)视频、音频、图像)、字符流(文本文件)

 按照角色的不同:节点流和处理流

2.IO体系

抽象基类        节点流        缓冲流(处理流的一种,可以提高文本操作的效率)

InputStream      FileInputStream    BufferedInputStream

OutputStream      FileOutputStream    BufferedOutputStream

Reader         FileReader       BufferedReader

Writer         FileWriter         BufferedWriter

public class FileInputOutputStream {

  // 从硬盘中存在的文件,读取内容加载到程序中,FileInputStream
  // 要读取的文件一定要存在,否则抛一个异常FileNotFoundException
  @Test
  public void testFileInputStream1() throws Exception{
    // 1、创建一个File类的对象
    File file = new File("hello.txt");
    // 2、创建一个FileInputStream类的对象
    FileInputStream fis = new FileInputStream(file);
    // 3、调用FileInputStream中的方法,实现file文件的读取
    // read()方法:可以读取文件中的一个字节 当执行到文件结尾时,返回-1
    /* int b = fis.read();
    // 判断有没有读取到文件结尾
    while(b!=-1){
      System.out.println((char)b);
      b = fis.read();
    }*/

    int b;
    while((b=fis.read())!=-1){
      System.out.println((char)b);
    }

    // 4、关闭相应的流
    fis.close();
    }

   // 使用try-catch-finally处理异常:保证流的关闭一定可以执行
   @Test
   public void testFileIntputStream2(){
    File file = new File("hello.txt");
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(file);
      int b;
      while((b=fis.read())!=-1){
        System.out.println((char)b);
      }
     } catch (IOException e) {
      e.printStackTrace();
     }finally {
        // 不管是否发生异常,finally中的代码一定会执行
        try {
          fis.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
     }
  }

  // 将读取到的数据填充到字节数组中
  @Test
  public void testFileInputStream3(){
    FileInputStream fis = null;
    try {
      File file = new File("hello.txt");
      // 将file对象作为FileInputStream的形参传进来
      fis = new FileInputStream(file);
      // 读取的数据要写入的数组
      byte[] b = new byte[5];
      // 每次读入到byte中字节的长度
      int len;
      while ((len=fis.read(b))!=-1) {
        for (int i = 0; i < len; i++) {
          System.out.print((char)b[i]);
        }
      }
     } catch (Exception e) {
        e.printStackTrace();
     }finally {
        try {
          fis.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
     }
  }

// FileOutputStream
@Test
public void testFileOutputStream(){
// 1、创建一个File对象,指定要写入的文件位置
// 输出的物理文件也可以不存在,当执行过程中,如果不存在,则自动创建,若存在,则将原来的文件覆盖
File file = new File("hello2.txt");
// 2、创建一个FileOutputStream对象,将file对象作为形参传递给FileOutputStream的构造器。
FileOutputStream fos = null;
try {
fos=new FileOutputStream(file);
// 3、执行写入操作
fos.write(new String("hello").getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
// 4、关闭输出流(释放资源)
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

// 从硬盘读取一个文件并写入到另一个位置(文件的复制)
@Test
public void testFileInputOutputStream(){
// 1、提供读入、写出的文件
File file1 = new File("hello.txt");
File file2 = new File("hello3.txt");
// 2、提供相应的输入流和输出流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[20];
int len;
// 3、实现文件的复制
while ((len=fis.read(b))!=-1) {
System.out.println(len);
// 从头开始写 写的长度是len
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

// 实现文件复制的方法
public static void copyFile(String src,String dest){
// 1、提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2、提供相应的输入流和输出流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[20];
int len;
// 3、实现文件的复制
while ((len=fis.read(b))!=-1) {
// 从头开始写 写的长度是len
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

@Test
public void testCopyFile(){
copyFile("1.jpg", "2.jpg");
}
}

public class TestBuffered {
// 使用BufferedInputStream和BufferedOutputStream实现非文本复制
@Test
public void testBufferedInputOutputStream(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.提供读写的文件
File file1 = new File("1.jpg");
File file2 = new File("3.jpg");
// 2.创建响应的节点流 FileInputStream FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
// 3.将创建的节点流对象作为形参传递给缓冲流的构造器
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 4.具体实现文件复制的操作
byte[] b = new byte[1024];
int len;
while ((len=bis.read(b))!=-1) {
// 写数据
bos.write(b, 0, len);
// 清除缓存
bos.flush();
}

} catch (Exception e) {
e.printStackTrace();
}finally {
// 5.释放资源
if (bis!=null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos!=null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

@Test
public void testBufferReader(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("oop.txt");
File file1 = new File("oop2.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);

/*char[] c = new char[1024];
int len;
while ((len=br.read(c))!=-1) {
String str = new String(c, 0, len);
bw.write(str);
bw.flush();
}*/

String str;
while ((str=br.readLine())!=null) {
bw.write(str);
bw.newLine();// 自动换行
bw.flush();
}

} catch (Exception e) {
e.printStackTrace();
}finally {
if (bw!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

IO学习二(节点流)的更多相关文章

  1. Java IO学习--(二)文件

    在Java应用程序中,文件是一种常用的数据源或者存储数据的媒介.所以这一小节将会对Java中文件的使用做一个简短的概述.这篇文章不会对每一个技术细节都做出解释,而是会针对文件存取的方法提供给你一些必要 ...

  2. java io 节点流和处理流

    JAVA IO操作总结:节点流和处理流 JAVA IO操作总结--节点流和处理流  按照流是否直接与特定的地方(如磁盘.内存.设备等)相连,分为节点流和处理流两类. 节点流:可以从或向一个特定的地方( ...

  3. Java IO学习笔记(二)缓冲流

    处理流:包在别的流上的流,可以对被包的流进行处理或者提供被包的流不具备的方法. 一.缓冲流:套接在相应的节点流之上,带有缓冲区,对读写的数据提供了缓冲的功能,提高读写效率,同时增加一些新的方法.可以减 ...

  4. JAVA IO分析二:字节数组流、基本数据&对象类型的数据流、打印流

    上一节,我们分析了常见的节点流(FileInputStream/FileOutputStream  FileReader/FileWrite)和常见的处理流(BufferedInputStream/B ...

  5. Java IO 节点流 ByteArrayInput/OutputStream

    Java IO 节点流 ByteArrayInput/OutputStream @author ixenos ByteArrayInputStream 包含一个内部缓冲区(字节数组byte[]),该缓 ...

  6. Java IO学习笔记二

    Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...

  7. Java IO学习笔记(三)转换流、数据流、字节数组流

    转换流 1.转换流:将字节流转换成字符流,转换之后就可以一个字符一个字符的往程序写内容了,并且可以调用字符节点流的write(String s)方法,还可以在外面套用BufferedReader()和 ...

  8. Java基础—IO小结(一)概述与节点流

    一.File类的使用  由于file类是一个基础类,所以我们从file类开始了解.(SE有完善的中文文档,建议阅读) 构造器: 常用方法:——完整方法请参见API API API!!! File做的是 ...

  9. JAVA里面的IO流(一)分类2(节点流和处理流及构造方法概要)

    IO流根据处理对象的不同分为节点流和处理流. 直接对文件进行处理的流为节点流: 对流进行包装从而实现对文件的优化处理的流为处理流. 节点流类型: 可以看出,节点流主要分这几大类: 文件流 文件流构造方 ...

随机推荐

  1. Javascript高级编程学习笔记(31)—— BOM(5)screen、history对象

    screen对象 screen对象应该是BOM对象中最不常用的对象了 其主要用于提供客户端的显示能力信息 包括浏览器外部显示的信息,和像素的宽高等 这个对象的主要用于检测客户端能力,一般不会影响功能 ...

  2. 细说MySQL数据库操作

    目录 基本语法: 字符集和校验规则 字符集 校验规则 校验规则的影响 数据库操作相关指令 查询数据库版本 显示数据库语句 显示数据库创建语句 数据库删除语句 查看当前数据库有多少个用户在操作 基本语法 ...

  3. 一句话的事儿,Head first 设计模式

    head first 设计模式,是比较有趣的一本设计模式的书. 在学校里看书和在工作时看书,意义是不一样的.在学校时是为读书而读书,我们可以从0到1,我们有的是时间.但是工作后就不一样. 我觉得这时的 ...

  4. 如何实现一个基于 jupyter 的 microservices

    零.背景: 现有基于 Node.js 的项目,但需要整合 Data Science 同事的基于 python(jupyter) 的代码部分,以实现额外的数据分析功能.于是设想实现一个 microser ...

  5. DevOps - 参考信息

    DevOps DevOps(Development+Operations)强调共同对业务目标负责,以实现用户价值作为唯一的评判标准:保证产品功能及时实现.成功部署和稳定使用: 是一种重视软件开发人员( ...

  6. 协议—IIC

    I2C总线支持任何IC生产过程NMOS CMOS双极性,两线――串行数据 SDA 和串行时钟SCL线在连接到总线的器件间传递信息,每个器件都有一个唯一的地址识别,无论是微控制器.LCD 驱动器.存储器 ...

  7. c# 导入导出excel方法封装

    在很多项目中,都会使用到文件的上传下载等,为了方便,封装了一个帮助类,每次直接拿过来使用就可以了,下面是封装的类和使用方法. using Common.AttributeHelper; using N ...

  8. 【WebAPI】从零开始学会使用.NET Core WebAPI

    介绍 以后会慢慢总结在项目使用中或者学习到的webAPI相关的知识,在这里做记录. 我会从最开始的如何创建WebAPI项目到项目的后续知识一点一点的开始讲述记录. 通过简单有效的方式,让我们能够快速的 ...

  9. C# 算法之选择排序

    1.简介 选择排序是排序中比较简单的一种,实现的大致思路如下:首先我们拿到一个需要排序的数组,假设该数组的第一个元素是最小的,然后将数组中剩下的元素,于最小的元素进行比较,如果中间有比第一个元素的小的 ...

  10. 如何正确且高效实现OSSIM中文化的解决方案(图文详解)

    前言   对于玩OSSIM的初学者或者中级水平的从业人员来说,都有一定必要性从中文看起,当然,最终还是英文的目标迈进,只是说,为了让自己更快速上手! 虽然系统说明支持中文,实际上,只是台湾的繁体中文而 ...