題目:
6、 定義一個文件輸入流,調用read(byte[] b)方法將exercise.txt文件中的所有內容打印出來(byte數組的大小限制爲5,不考慮中文編碼問題)。
代碼:
package com.itheima;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.junit.Test;
/**
* date : Jun 14, 2013
*
* time : 7:57:19 PM
*
* author : Spole
*
*/
/**
* 題目:
*
* 6、 定義一個文件輸入流,
*
* 調用read(byte[] b)方法將exercise.txt文件中的所有內容打印出來
*
* (byte數組的大小限制爲5,不考慮中文編碼問題)。
*/
public class Test06 {
@Test
public void test6() {
FileInputStream fis = null;
try {
// 創建文件輸入流對象
fis = new FileInputStream(new File(
"E:/exercise.txt"));
byte buffer[] = new byte[5];
// 讀取輸入流
while ((fis.read(buffer, 0, buffer.length) != -1)){
System.out.print(new String(buffer));
}
System.out.println("");
} catch (IOException ioe) {
ioe.getStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
//關閉流
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}