一个关于输入、输出流的小题目(2)
2008-03-06 13:55
369 查看
编写应用程序,读出“my file.txt”的内容,并将读出的内容输出到屏幕上,读文件结束后,显示“Finished reading,programe ended.”.
这是我的程序,如有更好的,请赐教。
package huicer;
import java.io.*;
public class ReadInFile {
/**
* @param args
*/
public static void main(String[] args) {
File aFile = new File("c:/test.txt");
FileReader fr = null;
char[] b = new char[200];
if (!aFile.exists()) {
System.out.println("File not Found!");
aFile = null;
System.exit(0);
} else {
try {
fr = new FileReader(aFile);
int frLength = 0;
while ((frLength = fr.read(b)) != -1) {
System.out.print(new String(b, 0, frLength));
}
System.out.println();
System.out.println("Finished reading,programe ended.");
fr.close();
fr = null;
} catch (FileNotFoundException e) {
System.err.println("File not Found!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是我的程序,如有更好的,请赐教。
package huicer;
import java.io.*;
public class ReadInFile {
/**
* @param args
*/
public static void main(String[] args) {
File aFile = new File("c:/test.txt");
FileReader fr = null;
char[] b = new char[200];
if (!aFile.exists()) {
System.out.println("File not Found!");
aFile = null;
System.exit(0);
} else {
try {
fr = new FileReader(aFile);
int frLength = 0;
while ((frLength = fr.read(b)) != -1) {
System.out.print(new String(b, 0, frLength));
}
System.out.println();
System.out.println("Finished reading,programe ended.");
fr.close();
fr = null;
} catch (FileNotFoundException e) {
System.err.println("File not Found!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
相关文章推荐
- 一个关于输入、输出流的小题目(1)
- 关于Java的二进制输入输出流的一点总结(有错误请指出)
- java 输入输出流 关于InputStream 和 OutputSteam 实现文本一行一行读入和 文本一行一行输出
- 调整数组使奇数全部都位于偶数前面。 题目: 输入一个整数数组,实现一个函数,来调整该数组中数字的顺序使得数组中所有的奇数位于数组的前半部分,所有偶数位于数组的后半部分。
- 第14 题:题目:输入一个已经按升序排序过的数组和一个数字
- 第十四周oj题目输入一个数插入到有序数组中
- 关于输入输出流的讨论
- c#关于输入一个整数是否为质数的代码
- 5.题目:输入一个32位的整数a,使用按位异或^运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果 提示:0 ^ 0 = 0; 1 ^ 1 = 0;
- java中字符输入输出流在输出结果的结尾多一个乱码字符:'?'
- 关于输入一个ID后,该ID的2个类别展示的问题
- 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
- 题目:输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。
- 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。(c语言)
- 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
- 一个关于java继承的题目
- 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
- 关于FileReader和FileWriter 输入输出流的总结
- 一个关于文件行操作的题目