数据流的基本概念

数据流的基本概念

1.      输入/输出流

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

2. 缓冲流

为提高数据的传输效率,通常使用缓冲流(buffered stream),即为一个流配有一个缓冲区(buffer),一个缓冲区就是专门用于传送数据的一块内存。   

Java的标准数据流

1.System.in

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

2. System.out

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

3. System.er

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

简单示例:

import java.io.*;

public classSystemTest

{          

      public static void main(String[] args)

      {

           byte[] b = new byte[512];

      int num = 0;

      System.out.println("请输入:");

      try{

           num = System.in.read(b);

 

      }catch(IOException e){

      e.printStackTrace();

      }

      System.out.println(new String(b));

      }

}

 

字节流

¯    InputStream类和OutputStream类

1.字节输入流类InputStream

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

注意:该类中的大多数方法都可能抛出IOException异常,因此调用它们时,应放在try…catch块中,捕获和处理IOException异常。

 

读取数据的方法

int read() throwsIOException ;

int read(byte[] b)throws IOException ;

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

¯    关闭输入流

 public void close() throws IOException;

¯    获取流中可读的字节数

public int available() throws IOException;

¯    移动读取指针

public long skip(long n) throws IOException;

¯    标记流中的位置和重置读取位置

µ   publicboolean markSupported();

µ   publicvoid mark(int readlimit); public void reset();

 

2.字节输出流 OutputStream

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

注意:该类中的大多数方法都可能抛出IOException异常,因此调用它们时,应放在try…catch块中,捕获和处理IOException异常。

字节输出流 OutputStream

¯   输出数据的方法

void write(int b) throws IOException ;

void write(byte[] b) throws IOException ;

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

¯    关闭输出流

public void close() throws IOException;

¯    清空缓冲区

public void flush() throws IOException;

文件字节输入流类FileInputStream

构造方法

publicFileInputStream(string name) throws FileNotFoundException

publicFileInputStream(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类

构造方法

publicFileOutputStream(String name) throws FileNotFoundException

publicFileOutputStream(File file) throws FileNotFoundException

publicFileOutput.Stream (String name,boolean append) throws FileNotFoundException

写入字节的方法

public voidwrite(int b) throws IOException

public voidwrite(byte[] b) throws IOException

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

文件的读写操作

简单示例:

importjava.io.FileInputStream;

importjava.io.FileOutputStream;

import java.io.*;

public classinputOutput {

      public static void main(String[] args) {

           try{

           FileInputStream fis = newFileInputStream("data.txt");

           FileOutputStream fos = newFileOutputStream("result.txt" ,true);

           int n = fis.available();

           byte[] b = new byte[n];

           //fos.write("\n".toArray());

           while(fis.read(b) != -1){

                 System.out.println(newString(b));

                 fos.write(b);

           }

           fis.close();

           fos.close();

           }catch(Exception e){}

           //FileOutputStream fos = newFileOutputStream("resrlt.txt");

     

      }

 

}

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值