/**
* 第七题: 定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5)。
*
* @author
*
*
*/
public class Test7 {
public static void main(String[] args) {
FileInputStream fr = null;
try {
fr = new FileInputStream("d:\\exercise.txt");
byte[] buf = new byte[5];
int len = 0;
while ((len = fr.read(buf)) != -1) {
for (int i = 0; i < len; i++) {
System.out.print((char) buf[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
|
|