您的位置:首页 > 编程语言 > Java开发

学习JAVA的标准数据流和输入输出流

2011-08-29 22:38 309 查看
 

学习JAVA的标准数据流和输入\输出流
学习笔记    宋鹤贤 2011-8-29
数据流分为输入流(InputStream)和输出流(OutputScream)两大类。

采取数据流的目的是,使程序的输入\输出操作独立于相关设备。因为每个设备的实现细节由系统执行完成,所以程序中不需要关心这些细节问题,使得一个程序能够用于多种输入\输出设备,不需要对源代码甚至目标代码做任何修改,从而增强程序的可移植性。

Java的标准数据流

1、标准输入System.in

Public int read() throws IOException:返回读入的一个字节,如果到达留的末尾,则返回-1。

2、标准输出System.out

Public int read(byte[] i) throws IOException:返回读入缓冲区的字节总字节数,如果因为已经到达流末尾而不再有数据可用,则返回-1。

使用read()方法发生IO错误时,抛出IOException异常。

3、标准的错误输出 System.err

字节流

1.字节输入流InputStream类

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

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

读取数据的方法

int read() throws IOException ; 

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;

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

public boolean markSupported();

public void mark(int readlimit);

public void reset();

2.字节输出流 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

例题:打开文件

import java.io.*;

public class ReadFileTest

{

   public static void main(String[] args) throws IOException

   {

  try

  { 

 //创建文件输入流对象

 FileInputStream fis =new FileInputStream("ReadFileTest.java");       

         int n=fis.available();        

         byte b[]=new byte
;        

         //读取输入流         

         while((fis.read(b,0,n))!=-1)    

         {                  

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

         }

         System.out.println();

         //关闭输入流

         fis.close();    

      }

      catch(IOException ioe)

      {

         System.out.println(ioe.getMessage());

      }

      catch(Exception e)

      {

         System.out.println(e.getMessage());

      }

}

}

用available()方法得到输入流的可读取字节数,再用read(byte[]b)方法,从源程序文件ReadFileTest.java中读取文件储存到字节数组b中,然后将以b中的值构造字符串new String(b) 显示在屏幕上。程序运行时,将源程序文件ReadFileTest.java的内容显示在屏幕上。

文件字节输出流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

例题12.3 写入文件

import java.io.*;

public class WriteFileTest

{

public static void main(String[] arg)

    {

       try

       {

         System.out.println("please input:");

         int count;

         byte b[]=new byte[512];

         count=System.in.read(b);    

         // 读取标准输入流

         FileOutputStream fos=new FileOutputStream("data.txt");

         //建文件输出流对象

         fos.write(b,0,count);        

         //写入文件

         fos.close();   

         //关闭输出流

         System.out.println("保存文件成功!");

       }

       catch(IOException ioe)

       {

          System.out.println(ioe.getMessage());

       }

   }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java byte string io