📄 chap8-2.txt
字号:
// 程序8-2
import java.io.*;
class showFile{ // 若要打开的文件不存在就会产生异常
void showInfo( ) throws Exception{ // 必须采用throws指明异常类型
int size=0;
FileInputStream fin=new FileInputStream("d:/abc.txt"); // 可能会产生异常
try{
size=fin.available( ); // 判断可读取的字节数
System.out.println("file size = "+size);
System.out.println("Read the first 1/4");
byte b[ ]=new byte[size/4];
fin.read(b); // 读取文件1/4的内容
String str=new String(b);
System.out.println("The first 1/4 is: "+str);
System.out.println("Skip the next 1/2 of the file");
fin.skip(size/2); // 跳过文件1/2的内容
System.out.println("Still available is :"+fin.available( ));
}catch(FileNotFoundException e) {
System.out.println("File not found : "+e);
throw e; // 将异常对象提交给main进行再次处理
}finally{
fin.close( ); // 关闭输入流
}
}
}
public class fileReadTest {
public static void main(String args[ ]){
showFile obj=new showFile( );
try{
obj.showInfo( );
}catch(Exception e) {
System.out.println("File not found : "+e);
e.printStackTrace( );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -