package excise;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class PrintMyself {
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("src/excise/PrintMyself.java");
char[] chars = new char[100];
int redCount=0;
while ((redCount = fr.read(chars)) != -1){
System.out.print(new String(chars,0,redCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
打印结果

该代码示例展示了如何使用Java的FileReader类读取并打印一个名为'PrintMyself.java'的文件内容。程序首先创建FileReader对象,然后通过read方法逐块读取文件,将读取到的内容输出到控制台。在读取过程中,捕获了可能抛出的FileNotFoundException和IOException,并在最后确保文件被正确关闭。
206

被折叠的 条评论
为什么被折叠?



