关闭

FileInputStream(文件输入流) 示例

1293人阅读 评论(0) 收藏 举报

//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();
 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:691364次
    • 积分:7953
    • 等级:
    • 排名:第2693名
    • 原创:75篇
    • 转载:247篇
    • 译文:0篇
    • 评论:64条
    文章分类
    最新评论