java中的输入输出流(字节流 字符流 节点流 过滤流)

输入输出流有关的类在java.io包下,jdk1.4起加了java.nio包,jdk1.7对其做了改进,称为nio2。


按流中处理的数据是以字节(8位)为单位还是以字符(16位)为单位分为字节流和字符流

字节流: InputStream和OutputStream


InputStream

InputStream是抽象类,不能直接用new InputStream()构造实例。 FileInputStream是InputStream的子类,可以生成实例。 FileInputStream有三个构造方法,最常用的构造方法如下:

Public FileInputStream(String fileName) throws FileNotFoundException
Public FileInputStream(File file) throws FileNotFoundException
//fileName用来指定输入文件名及其路径,file是一个File对象

字节流的层次结构

InputStream类的read()方法:逐字节以二进制的原始方式读取数据

public int read();//读入一个字节,-1表示无
public int read(byte b[]);//返回读入的字节数
public int read(byte[] b,int off.int len);//返回从某个位置开始制定长度的字节数


OutputStream

常用方法:

//写一个字节
void write( int ) throws IOException 
//关闭输出流
void close( ) throws IOException 
//强行将缓冲区的数据写到目的地。
void flush( ) throws IOException 
//写一个字节数组
void write(byte[ ] b) throws IOException 
    void write(byte[ ] b, int offset, int length ) throws IOException 

OutputStream是抽象类,不能直接用new OutputStream()构造实例。 FileOutputStream是OutputStream的子类,可以生成实例。 FileOutputStream最常用的构造方法如下:

Public FileOutputStream(String name) throws FileNotFoundException
Public FileOutputStream(String name,boolean append) throws FileNotFoundException

//Name用来指定输入文件名及其路径,append为true时数据将添加到文件已有内容的末尾



利用字节流复制文件

import java.io.*;
class CopyAFile 
{
    public static void main(String[] args) 
    {
        InputStream in;
        OutputStream out;
        try
        {
            in=new FileInputStream("test.txt");
            out=new FileOutputStream("copyResult.txt");
//          out=new FileOutputStream("copyResult.txt",true);
            int aByte;
            aByte=in.read();
            while (aByte!=-1)
            {
                out.write(aByte);
                aByte=in.read();
            }
            in.close();
            out.close();
            System.out.println("文件复制完毕。test.txt已经复制到copyResult.txt中。");
        }
        catch(FileNotFoundException e)
        {
            System.out.println("当前目录下文件test.txt不存在!");
        }
        catch(IOException e)
        {
            System.out.println("发生输入输出错误!");
        }
    }
}




字符流: Reader和Writer

字符流不一定对应两个字节,要考虑到编码问题。

字符流层次结构

Reader类读取的是字符,而不是字节。
Reader类的重要方法read():

public int read();//需要将int转为char
public int read(char b[]);
public int read(char[] b,int off,int len)




节点流和过滤流

节点流(Node Stream) :直接与原始数据存在的特定介质(如磁盘文件或其他外部设备、内存某区域或其他程序)打交道的流,在流的序列中离程序最远。
过滤流 (Filter Stream):使用其它的流作为输入源或输出目的地,对流中的数据提供进一步处理的流。其他的流可以是节点流,也可以是另一种过滤流。过滤流不能单独使用

一个输入流链或输出流链中一定有且只有一个节点流;可以没有,也可以有多个过滤流。

过滤流和节点流的套接:

InputStream in;
in=new BufferedInputStream(new FileInputStream("test.txt"));

BufferedReader in = new BufferedReader(new FileReader(file));

BufferedReader in2 = new BufferedReader(new inputStreamReader(new FileInputStream(file),"utf-8"));
s = in2.readLine();

流的套接

标准输入输出流和错误流

1.System.out: -PrintStream类型
把输出送到缺省的显示(通常是显示器)

2.System.in - InputStream类型
从标准输入获取输入(通常是键盘)

3.System.err - PrintStream类型
out的用法大家已熟知了,err的用法与out一样

System是final类,in,out,err是System的静态成员变量,因此可以用System.in等形式直接使用。

由于System,.in是InputStream类型,因此需要包装 :

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();

注:
1.输入/输出流的方法会抛出异常,因此必须进行异常处理
2.标准输入/输出/错误流对象System.in, System.out、 System.err 始终存在 ,不需要实例化,也 不需要关闭

最后附上两张图
1、常用节点流:
常用节点流
2.常用过滤流:
常用过滤流

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值