当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级Java输入输出操作学习教程(2)
发布时间:2011/12/31 11:33:54 来源:城市学习网 编辑:ziteng

9.5 输入流

  InputStream SequenceInputStream FileInputStream PipedInputStream ByteArrayInputStream FileterInputStream StringBufferInputStream

  DataInputStream LineNumberInputStream PushbackInputStream BufferedInputStream 有好几个类是专门用来处理文件输入的。下面是文件输入类的层次结构:

  9.5.1 FileInputStream 对象

  FileInputStream典型地表示一种顺序访问的文本文件。通过使用FileInputStream你可以访问文件的一个字节、几个字节或整个文件。

  9.5.2 打开FileInputStream

  为一个文件打开输入流FileInputStream,你必须将文件名或文件对象传送给结构:

  FileInputStream myFileStream;

  myFileStream = new FileInputStream ( "/etc/motd");

  你还可以象下边这样从FileInputStream里读文件信息:

  File myFile ;

  FileInputSteam myFileStream;

  myFile = new File("/etc/motd");

  myFileStream = new FileInputStream(myFile);

  FileInputStream输入流打开,你就可以从里面读取信息了。read()成员函数有以下几种选项:

  int read(); //reads one byte //return -1 at end of stream

  int read(byte b[]); //fills entire array,if possible //returns number of bytes read //returns -1 if end of stream is reached

  int read(byte b[],int offset, int len)

  //reads len bytes into b starting at b[offset]

  //Returns number of bytes read,

  //or -1 if end of stream is reached.

  9.5.3 关闭FileInputStream

  当你完成一个文件的操作,你可选两种方法关闭它: 显式关闭和隐式关闭,隐式关闭是自动垃圾回收时的功能。

  显式关闭如下:myFileStream.close();

9.6 例程:显示一个文件

  如果文件的访问权限足够,你可以在TextArea对象里显示文件内容。

  下面是显示文件的程序片断:

  FileInputStream fis;

  TextArea ta;

  public vod init(){

  byte b[] = new byte [1024];

  int I; //make it big enough or wait until you //know the size of the file String s;

  try {

  fis = new FileInputStream("/etc/motd");

  }

  catch(FileNotFoundException e) {

  /*do something appropriate */

  }

  try {

  I= fis.read(b);

  }

  catch(IOException e) {

  /* do something appropriate */

  }

  s = new String(b, 0);

  ta = new TextArea(s,5,40); add (ta);

  }

9.7 DataInputStreams

  DataInputStreams与FileInputStreams差不多。Data流可以直接读任意一种变 量类型, 如浮点数,整数和字符等。一般来说,对二进制文件使用DataInputStream流。

  9.7.1 打开和关闭DataInputStreams

  打开和关闭DataInputStreams对象时, 其方法与FileInputStreams相同:

  DataInputStreams myDataStream;

  FileInputStreams myFileStream;

  //get a file handle

  myFileStream = new FileInputStream("/usr/db/stock.dbf");

  //open,or "chain" a data input file

  myDataStream = new DataOutputStream(myFileStream);

  //Now we can use both input streams to access our file

  //j(If we want to...)

  myFileStream.read(b);

  I = myDataStrea.readInt();

  //close the data friel explicityly

  //Always close the "topmost" file stream

  myDataStream.close();

  myFileStream.close();

  9.7.2 读DataInputStreams

  当你从DataInputStreams流里访问文件时,你可以使用与FileInputStream流相同的成员函数 read()。 但你也可以使用其他访问方法来读取不同种类的数据:

  byte readByte(),int readUnsignedByte(),short readShort(),int readUnsighedShort(),char readChar(),int readInt(),long readLong(),float readFloat(),double readDouble(),String readLine() 以上每一个成员函数都读取相应的数据对象。象String readLine()成员函数,你可使用\n,\r,\r\n,或EOF作为字符 结束 符。

  读一个长整型,例如:

  long serialNo;

  serialNo = myDataStream.readLong();

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved