数据流和文档操作1

流:是同一台计算机或网络中不同计算机之间有序运动的数据序列。

  数据序列:可以是原始的二进制字节数据,也可以是经过特定编码处理的有格式的数据.

数据流分为输入流(InputStream)和输出流(OutputStream)两大类。输入流只能读不能写,而输出流只能写不能读。通常程序中使用输入流读出数据,输出流写入数据,就好像数据流入到程序并从程序中流出。

Java的标准数据流

标准的输入,对象是键盘

标准的输出,对象是显示器屏幕

标准的错误输出,对象也是显示器屏幕

Java通过系统类System实现标准的输入输出功能。System类在java.lang包中,声明为一个final类

Java的标准数据流

System.in

作为字节输入流类InputStream的对象in实现标准的输入,其中有read方法从键盘接收数据。

System.out

作为打印流类PrintStream的对象out实现标准输出。其中有print和println两个方法,这两个方法支持java的任意的基本类型作为参数。

System.err

  与System.out相同,以PrintStream类的对象err实现标准的错误输出。

InputStream类和OutputStream类

字节输入流类InputStream

   InputStream类是抽象类,不能直接生成对象,它是所有字节输入流类的父类。该类提供了输入处理的基本方法,它的子类一般都重写这些方法。

  读取数据的方法

  int read() throws IOException ; 

  int read(byte[] b) throws IOException ;

int read(byte[] b,int off,int len) throws IOException ;

注意:read方法若返回-1,则表明当前读取位置已经到达流的末尾。

例如:int n;

While((n=fis.read())!=-1)

{

       //数据处理

}

字节输出流 OutputStream

   OutputStream类是抽象类,不能直接生成对象,它是所有字节输出流类的父类。该类提供了输出处理的基本方法,它的子类一般都重写这些方法。

  输出数据的方法

  void write(int b) throws IOException ; 

  void write(byte[] b) throws IOException ;

void write(byte[] b,int off,int len) throws IOException ;

关闭输出流

public void close() throws IOException;

清空缓冲区

public void flush() throws IOException;

文件数据流类FilelnputStream和FileOutputStream是用于进行文件输入输出的处理,其数据对象都是文件。 

文件字节输入流类FileInputStream

 FileInputStream用于顺序访问本地文件。它从父类InputStream中继承read()、close()等方法对本机上的文件进行操作,但不支持mark()方法和reset()方法。

构造方法

public FileInputStream(string name) throws FileNotFoundException

public FileInputStream(File file) throws FileNotFoundException

读取字节的方法

 public int read() throws IOException

 public int read(byte[] b)throws IOException

 public int read(byte[] b,int off,int len) throws IOException

文件字节输出流FileOutputStream类 

 FileOutputStream类用于向一个文件写数据。它从超类OutputStream中继承write()、close()等方法。

构造方法

public FileOutputStream(String name) throws FileNotFoundException

public FileOutputStream(File file) throws FileNotFoundException

public FileOutput.Stream (String name,boolean append) throws FileNotFoundException

写入字节的方法

public void write(int b) throws IOException

public void write(byte[] b) throws IOException

public void write(byte[] b,int off,int len) throws IOException

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值