- FileReader fr = null;
- try {
- //fr = new FileReader("Hello World.txt");
-
- /*fr.read() 读取单个字符,返回值为int类型
- * int num = 0;
- while( (num = fr.read()) != -1) {
- char value = (char) num;
- System.out.println(value);
- }*/
-
- /*//fr.read(char[]) 读取多个字符组
- char[] cbuf = new char[1024];
- int num = 0;
- while (( num = fr.read(cbuf)) != -1) {
- System.out.println(new String(cbuf,0,num));
- }*/
-
- //读取一个.java文件,并打印在控制台中
- fr = new FileReader("Test1.java");
- char[] cbuf = new char[1024];
- int num = 0;
- while ((num = fr.read(cbuf)) != -1) {
- System.out.print(new String(cbuf,0,num));
- }
复制代码 Hello World.txt是之前FileWriter时在java project的根目录下创建的,如果想像上述代码那样,调用别的文件夹的文件,必须写绝对路径吗?
|
|