FileReader读取文件的三种方式
Posted qmfsun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FileReader读取文件的三种方式相关的知识,希望对你有一定的参考价值。
package com.agoly.test; //import java.io.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileTest { public static void main(String[] args) { // 读取文件的方式一:逐个字符来读取文本文件 FileReader fr = null; try { fr = new FileReader("Agoly.txt"); int ch = fr.read(); while (ch != -1) { System.out.print((char) ch); ch = fr.read(); } System.out.println(); } catch (IOException e) { System.out.println("异常:" + e.toString()); } finally { try { if (fr != null) fr.close(); } catch (IOException e) { System.out.println("异常:" + e.toString()); } } //读取文件方式二:使用数组来读取文本文件 FileReader fr1 = null; try { fr1 = new FileReader("Agoly.txt"); char [] buf = new char[1024]; int num = 0; while((num = fr1.read(buf))!=-1) { System.out.println(new String(buf,0,num)); } }catch(IOException e) { System.out.println("异常:" + e.toString()); }finally { try { if(fr1!=null) fr1.close(); }catch(IOException e) { System.out.println("异常:" + e.toString()); } } //方法三:用缓冲区读取文本文件 //通过查源码得知方法三内部实现时是使用数组形式来缓冲字符数据的 FileReader fr2 = null; BufferedReader bufr = null; try { fr2 = new FileReader("Agoly.txt"); bufr = new BufferedReader(fr2); String line = null; //BufferedReader提供了按行读取文本文件的方法readLine() //readLine()返回行有效数据,不包含换行符,未读取到数据返回null while((line = bufr.readLine())!=null) { System.out.println(line); } }catch(IOException e) { System.out.println("异常:" + e.toString()); }finally { try { if(bufr!=null) bufr.close(); }catch(IOException e) { System.out.println("异常:" + e.toString()); } } } }
FileReader可以通过一次读取文件中一个字符,一次读取一个字符数组或使用缓冲区这三种方式来读取文件
备注:使用缓冲区的方式的内部实现是借助数组完成
方法2:封装成函数:
package com.agoly.test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; public class FileTest { /** * * <p>TODO(按行读文件)</p> * @author 276283 * @date 2016-1-26 上午9:42:48 * @param fileName * @see */ public void readFile(String fileName){ BufferedReader reader = null; String content = null; try { reader = new BufferedReader(new FileReader(fileName)); while(null != (content = reader.readLine() )){ System.out.println(content); } }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ if(reader != null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * * <p>TODO(按字节读文件)</p> * @author 276283 * @date 2016-1-26 上午9:43:02 * @param pathName * @see */ public void readFileByBytes(String fileName){ InputStream is = null; File file = new File(fileName); int tempByte; try { is = new FileInputStream(file); while((tempByte = is.read()) != -1){ System.out.write(tempByte); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * * <p>TODO(按字符读取文件)</p> * @author 276283 * @date 2016-1-26 上午9:43:46 * @see */ public void readFileByChar(String fileName){ File file = new File(fileName); Reader reader = null; int tempChar; try { reader = new InputStreamReader(new FileInputStream(file)); while(-1 != (tempChar = reader.read())){ System.out.print((char)tempChar); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(reader != null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { FileTest readJavaFile = new FileTest(); System.out.println("--------------------------- 按照行读文件开始(BufferedReader) --------------------------"); readJavaFile.readFile("Agoly.txt"); System.out.println("--------------------------- 按照行读文件结束(BufferedReader) --------------------------"); System.out.println(" "); System.out.println("--------------------------- 按字节读文件开始(FileInputStream) ------------------------"); readJavaFile.readFileByBytes("Agoly.txt"); System.out.println(" --------------------------- 按字节读文件结束(FileInputStream) ------------------------"); System.out.println(" "); System.out.println("--------------------------- 按字符读文件开始(InputStreamReader) ------------------------"); readJavaFile.readFileByChar("Agoly.txt"); System.out.println(" --------------------------- 按字符读文件结束(InputStreamReader) ------------------------"); } }
以上是关于FileReader读取文件的三种方式的主要内容,如果未能解决你的问题,请参考以下文章