陈华江(HuaChiang Chen) 陈泉冰专栏

未老之前携你远行...

FileInputStream(文件输入流) 示例

//Demonstrate FileInputStream
import java.io.*;
class FileInputStreamDemo{
 public static void main(String[] args) throws Exception{
  int intSize;
  InputStream f = new FileInputStream("FileInputStreamDemo.java");
  System.out.println("Total Available Bytes: " + (intSize = f.available()));

  int n = intSize / 40 ;
  System.out.println("First " + n + " bytes of  the file one read() at a time.");
  for(int i=0;i<n;i++){
   System.out.print((char)f.read());
  }
  System.out.println("/nStill Available :" +  f.available());
  System.out.println("Reading the next " + n + " with one read(b[]) ");

  byte b[] = new byte[n];
  if(f.read(b) !=n){
   System.out.println("couldn't read " + n +"bytes.");
  }
  System.out.println(new String(b,0,n));
  System.out.println("/nStill Available :" +  (intSize = f.available()));
  System.out.println("Skipping half of remaining bytes with skip()");
  f.skip(intSize/2);
  System.out.println("Still Available :" +  f.available());
  System.out.println("Reading  " +  n/2 + " into the end of array.");
  if(f.read(b,n/2,n/2) !=n/2){
   System.out.println("couldn't read " + n/2 + "bytes.");
  }
  System.out.println(new String(b,0,b.length));
  System.out.println("/nStill Available :" +  f.available());
  f.close();
 }
}

阅读更多
个人分类: Java
上一篇FilenameFilter接口 示例
下一篇FileOutputStream(文件输出流) 示例
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭