package com.io;java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;spa
public class FileInputStreamDemo {.net
/**
* @Title
* @Description
* @param
* @return void
* @throws IOException
* @pages
* @throws
*/ip
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stubrem
int size;
InputStream f = new FileInputStream("D:\\workspace\\bbs\\src\\com\\hygj\\bbs\\bean\\Tip.java");
System.out.println("total available Bytes: "+(size = f.available()));
//第一次讀取n個字節
int n = size/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());
//第二次經過把數據存入緩衝區進行讀取n個字節
System.out.println("Reading the next "+n+"with one read(b[])");
byte b[] = new byte[n];
if(f.read(b)!=n){
System.out.println("could not read"+n+"bytes");
}
//第三次經過跳過讀取
System.out.println(new String(b,0,n));
System.out.println("\nStill available:"+(size=f.available()));
System.out.println("Skipping half of remaining bytes with skip()");
f.skip(size/2);
System.out.println("Still available :"+f.available());
System.out.println("Reading "+n/2+"into the ehd of array");
if(f.read(b,n/2,n/2)!=n/2){
System.out.println("could not read"+n/2+"bytes");
}
System.out.println(new String(b,0,b.length));
System.out.println("\nStill available:"+f.available());
f.close();
}get
}
it