接着上一篇的 “Java 的 File 类” 的随笔,在File类的基础上,我们就走进Java的IO流吧。

流的概念和作用

  流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

流的分类:

  (1)、按照数据流向的不同分为:输入流(从磁盘、网络等读取到内存,只能读,不能写)、输出流(从内存写出到磁盘、网络,只能写,不能读)

  (2)、按照处理数据的单位不同分为:字节流(以字节为基本操作单位)、字符流(以字符为基本操作单位)

  (3)、按照角色的不同分为:节点流(向某个IO设备/节点(磁盘文件、网络等)直接读写数据,也称为低级流)、处理流(用于包装一个已存在的流,通过这个已存在的流来进行读写操作,并不直接操作IO节点,也称为高级流、包装流)

Java流类结构:

分类

字节输入流

字节输出流

字符输入流

字符输出流

抽象基类

InputStream

OutputStream

Reader

Writer

操作文件

FileInputStream

FileOutputStream

FileReader

FileWriter

操作数组

ByteArrayInputStream

ByteArrayOutputStream

CharArrayReader

CharArrayWriter

操作字符串

   

StringReader

StringWriter

缓冲流

BufferedInputStream

BufferedOutputStream

BufferedReader

BufferedWriter

转换流

   

InputStreamReader

OutputStreamWriter

对象流(用于序列化)

ObjectInputStream

ObjectOutputStream

   

抽象基类(用于过滤)

FilterInputStream

FilterOutputStream

FilterReader

FilterWriter

打印流(输出功能极其大)

 

PrintStream

 

PrintWriter

java的IO流的用法

InputStream是字节输入流的顶级父类,常用方法:

  • int  read()     //读取一个字节,返回该字节数据的Unicode码值
  • int  read(byte[]  buff)    //最多读取buff.length个字节,将读取的数据放在buff数组中,返回实际读取的字节数
  • int  read(byte[]  buff, int off, int length)    //最多读取length个字节,放在buff数组中,从数组的off位置开始放置数据,返回实际读取的字节数。off一般设置为0,length一般设置为buff的长度。
        //1、创建一个File类的对象
File file = new File("hello.txt");
//2、创建一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
//3、调用FileInputStream的方法,实现file文件的读取
int b = fis.read();
while(b != -1){
System.out.print((char)b+" ");
b = fis.read();
}
fis.close();

OutputStream是字节输出流的顶级父类,常用方法:

  • void  write(int  i)    \\输出一个字节,i是码值,即read()得到的码值,输出i对应的字节
  • void  write(byte[]  buff)   //输出整个字节数组的内容
  • void  write(byte[]  buff, int  off, int  length)    //把字节数组从off位置开始,输出长度为length字节的内容
        File file = new File("hello1.txt");
FileOutputStream fos = null;
try{
fos = new FileOutputStream(file);
fos.write(new String("\nI Love CYT!").getBytes());
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Reader时字符输入流的顶级父类,常用方法:

  • int  read()     //读取一个字符,返回该字符的Unicode码值,注意并不是返回该字符。
  • int  read(char[]  buff)   //最多读取buff.length个字符,放在buff数组中,返回实际读取的字符数
  • int  read(char[]  buff, int  off, int  length) //最多读取length个字节,放在buff数组中,从数组的off位置开始放置数据,返回实际读取的字符数。off一般设置为0,length一般设置为buff的长度
            File file = new File("1.txt");
    FileReader fr = null;
    try{
    fr = new FileReader(file);
    char[] c = new char[24];
    int len;
    while((len = fr.read(c)) != -1){
    for(int i=0;i<len;i++){
    System.out.print(c[i]);
    }
    }
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    if(fr != null){
    try {
    fr.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

Writer是字符输出流的顶级父类,常用方法:

  • void  write(int  i)    //输出一个字符,i是码值,输出的是i对应的字符
  • void  write(char[]  buff)    //输出整个char[]的内容
  • void  write(char[]  buff,  int  off,  int  length)      //把char[]从off位置开始,输出长度为length字符的内容

可以用String代替char[],所以Writer还具有以下2个方法:

  • void  write(String str)
  • void  write(String str, int off, int length)
        FileWriter fw = null;
try{
fw = new FileWriter(new File("1.txt"));
String str = "我喜欢Java,我要成为Java工程师。";
fw.write(str);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

缓冲流是和4级顶级父类对应的:加前缀Buffered

  InputStream   BufferedInputStream   字节输入缓冲流,可作为所有字节输入流类的缓冲流

  OutputStream   BufferedOutputStream    字节输出缓冲流

  Reader     BufferedReader    字符输入缓冲流

  Writer  BufferedWriter    字符输出缓冲流

//用 BufferedReader 把文件读进来, 再用 BufferedWriter 把内容写出去
File file = new File("hello.txt");
File file1 = new File("hello3.txt");
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
try{
fr = new FileReader(file);
fw = new FileWriter(file1);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
String str;
while((str = br.readLine()) != null){
// System.out.println(str);
bw.write(str);
bw.newLine();
bw.flush();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

其他方法我就不一一说明了。有兴趣的同学可以自己再深入研究。

最后我就提供文件复制的通用方法吧。自己写的通用类。让自己更好的去了解Java的IO流的使用。

package io;
/*
* 实现文件的复制
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class CopyFile { // 字节文件的复制方法
public static void copyFileByte(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);
// 3、实现文件的复制
byte[] b = new byte[20];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (Exception 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();
}
}
}
} /*
* 使用FileReader 、 FileWriter 可以实现文本文件的复制
* 对于非文本文件(视频文件、音频文件、图片),只能使用字节流复制。
*/
public static void copyFileChar(String src, String dest) {
// 1、提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2、提供相应的流
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(file1);
fw = new FileWriter(file2);
// 3、实现文件的复制
char[] c = new char[24];
int len;
while ((len = fr.read(c)) != -1) {
fw.write(c, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} //使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制
public static void copyFileBuffered1(String src, String dest) {
//1、提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2、先创建相应的节点流:FileInputStream、 FileOutputStream
FileInputStream fis = null;
FileOutputStream fos = null;
//3、再创建缓冲流:BufferedInputStream 、 BufferedOutputStream
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
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 {
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

现在自己也准备大四了,所以自己要捉紧时间去学习,博客里面有什么问题的可以跟我说一下,大家一起交流一下学习的经验。

Java 的 IO 流的更多相关文章

  1. java的IO流

    java的IO流继承四大抽象类分别是字节流 inputStream outputStream与字符流 read write.怎么理解记忆很重要. 直接连接读写对象的是结点流,例如对文件读取字节类的名字 ...

  2. Java基础——IO流

    今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬板.内存.键盘等处理 ...

  3. 【Java】IO流简单分辨

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827509.html Java的IO流体系十分庞大,并且体系层次稍复杂,很容易记混或记错.在此,我把平时经常用 ...

  4. Java - 文件(IO流)

    Java - 文件 (IO)   流的分类:     > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter     ...

  5. Java中IO流的总结

    有关Java中IO流总结图 流分类 按方向分 输入流 输出流 按单位分 字节流 字符流 按功能分 节点流 处理流(过滤流) 其他 所有的流继承与这四类流:InputSteam.OutputStream ...

  6. JAVA中IO流总结

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...

  7. 第15章-输入/输出 --- 理解Java的IO流

    (一)理解Java的IO流 JAVA的IO流是实现输入/输出的基础,它可以方便地实现数据的输入/输出操作,在Java中把不同的输入/输出(键盘.文件.网络连接等)抽象表述为"流"( ...

  8. Java基础IO流(二)字节流小案例

    JAVA基础IO流(一)https://www.cnblogs.com/deepSleeping/p/9693601.html ①读取指定文件内容,按照16进制输出到控制台 其中,Integer.to ...

  9. java的Io流学习

    Java中io流的学习(一)File:https://blog.csdn.net/qq_41061437/article/details/81672859 Java中io流的学习(二)FileInpu ...

  10. Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)

    Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...

随机推荐

  1. 起言-----UE4学习方法

    1.bilibili 2.官网教程 3.我觉得以上两个就够了 官方文档链接 https://docs.unrealengine.com/ 官网在线视频链接 https://learn.unrealen ...

  2. Winform中实现向窗体中拖放照片并显示以及拖放文件夹显示树形结构(附代码下载)

    场景 向窗体中拖拽照片并显示效果 向窗体中拖拽文件夹并显示树形结构效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...

  3. 【高可用架构】开发机上部署Deploy项目(一)

    准备 部署项目的细节可以看这个,传送门Centos 7部署Laravel项目 主机IP:192.168.10.17 [高可用架构]系列链接:待部署的架构介绍 演示 部署Deploy composer ...

  4. 更改Android设备System目录的文件的写入权限

    有时候我们需要修改/system目录中文件的权限,比如将该目录下的脚本设置写入权限等,但该目录默认只有read权限,此时应该怎么办? 1.安卓设备请确保root;2.连接安卓设备,确保安卓设备打开了“ ...

  5. linux 定时备份数据库

    说明 检查Crontab是否安装 若没有 需要先安装Crontab定时工具 安装定时工具参考(https://www.cnblogs.com/shaohuixia/p/5577738.html) 需要 ...

  6. CentOS7下部署java+tomcat+mysql项目及遇到的坑

    CentOS 7 下安装部署java+tomcat+mysql 前置:CentOS7安装:https://jingyan.baidu.com/article/b7001fe1d1d8380e7382d ...

  7. STM32基本GPIO操作:按键输入(扫描+外部中断)

    (涉及专有名词较多,难免解释不到位,若有错误还请指出,谢谢!) 硬件连接图如下: 一.扫描 思路是在main函数中通过死循环来扫描端口电平状态检测,以此判断按键是否按下.实现较为简单. 1.初始化(注 ...

  8. ubuntu下安装截图工具

    安装shutter 1.添加安装包软件源 sudo add-apt-repository ppa:shutter/ppa 2.更新软件源并且安装 sudo apt-get update sudo ap ...

  9. Graylog 笔记

    安装 基本上有3种方式,1 yum安装2 rpm安装3 docker安装 yum安装 yum安装,参照官方文档是最好的:http://docs.graylog.org/en/3.0/pages/ins ...

  10. 使用系统定时器SysTick实现精确延时微秒和毫秒函数

    SysTick定时器简介 SysTick定时器是存在于系统内核的一个滴答定时器,只要是ARM Cortex-M0/M3/M4/M7内核的MCU都包含这个定时器,它是一个24位的递减定时器,当计数到 0 ...