- /**
- *
- */
- package io;
- import java.io.FileReader;
- /**
- * @author mshi
- *
- */
- public class FileReaderDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- //使用read()读取单个字符流的方式读取文件
- /*FileReader fileReader = null;
- try {
- fileReader = new FileReader("D:\\HaxLogs1.txt");
- int i;
- char ch;
- while((i = fileReader.read())!= -1)
- { ch = (char)i;
- System.out.print(ch);}
- } catch (Exception e) {
- // TODO: handle exception
- }finally{
- try {
- if(fileReader!=null)
- fileReader.close();
- } catch (Exception e2) {
- // TODO: handle exception
- }*/
- //使用read(char[])读取字符流并存入字符数组的方式读取文件
- FileReader fileReader = null;
- try {
- fileReader = new FileReader("D:\\HaxLogs1.txt");
- int len;
- char[] ch = new char[1024];
- while((len= fileReader.read(ch))!= -1)
- { System.out.print(new String(ch, 0, len));
- ch =null;}
- } catch (Exception e) {
- // TODO: handle exception
- }finally{
- try {
- if(fileReader!=null)
- fileReader.close();
- } catch (Exception e2) {
- // TODO: handle exception
- }
- }
- }
- }
复制代码
代码如上,请大家验证一下 |
|