【Java】运用多种方式读取文件内容,包括按字节、字符和按行为单位读取文件内容

来源:互联网 发布:ubuntu 16.04 支持32 编辑:程序博客网 时间:2024/05/19 13:28
主要功能:运用多种方式读取文件内容
 * <p>包括按字节读取文件内容、按字符读取文件内容、按行读取文件内容。
 * <ul>
 * <li> 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
 * <li> 以字符为单位读取文件,常用于读文本、数字等类型的文件。要得到标准输入的字节流,
 * 然后通过桥接转换为字符流,再经过缓冲得到带缓冲的标准输入流。

 * <li> 以行为单位读取文件,常用于读面向行的格式化文件。


package com.zf.s10.io;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;/** * @文件名 TextReadFile.java * @功能 运用多种方式读取文件内容 * <p>包括按字节读取文件内容、按字符读取文件内容、按行读取文件内容。 * <ul> * <li> 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 * <li> 以字符为单位读取文件,常用于读文本、数字等类型的文件。要得到标准输入的字节流, *  然后通过桥接转换为字符流,再经过缓冲得到带缓冲的标准输入流。 * <li> 以行为单位读取文件,常用于读面向行的格式化文件。 *  </ul> * </p> * @作者  * @日期 2010-4-28 * @版本 M1.0 * @描述 Java 工具类 */public class TextReadFile {/** * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 *  * @param fileName 文件绝对路径 */public static void readFileByBytes(String fileName) {File file = new File(fileName);// 创建文件InputStream in = null;try {System.out.println("①.以字节为单位读取文件内容,一次读一个字节:");in = new FileInputStream(file);// 将文件放入文件输入流中int tempbyte;while ((tempbyte = in.read()) != -1) { // 一次读一个字节,循环将内容读出来System.out.write(tempbyte);}in.close();// 关闭文件输入流} catch (IOException e) {// 捕获异常e.printStackTrace();return;}try {System.out.println("②.以字节为单位读取文件内容,一次读多个字节:");// 一次读多个字节byte[] tempbytes = new byte[100];// 声明长度为100的字节数组int byteread = 0;in = new FileInputStream(fileName);// 将文件放入文件输入流中TextReadFile.showAvailableBytes(in);// //显示输入流中还剩的字节数// 读入多个字节到字节数组中,byteread为一次读入的字节数while ((byteread = in.read(tempbytes)) != -1) {// 一次读多个字节,循环将内容读出来System.out.write(tempbytes, 0, byteread);}} catch (Exception e1) {// 捕获异常e1.printStackTrace();} finally {// 内容总执行if (in != null) {try {in.close();// 确保文件输入流关闭} catch (IOException e1) {e1.printStackTrace();}}}}/** * 以字符为单位读取文件,常用于读文本、数字等类型的文件。 *  * @param fileName 文件绝对路径 */public static void readFileByChars(String fileName) {File file = new File(fileName);// 创建文件Reader read = null;try {System.out.println("①.以字符为单位读取文件内容,一次读一个字节:");// 一次读一个字符read = new InputStreamReader(new FileInputStream(file));int tempchar;while ((tempchar = read.read()) != -1) {// 对于windows下,rn这两个字符在一起时,表示一个换行。// 但如果这两个字符分开显示时,会换两次行。// 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。if (((char) tempchar) != 'r') {// 只要是不换行就读取System.out.print((char) tempchar);}}read.close();} catch (Exception e) {e.printStackTrace();}try {System.out.println("②.以字符为单位读取文件内容,一次读多个字节:");char[] tempchars = new char[30];int charread = 0;read = new InputStreamReader(new FileInputStream(fileName));// 创建文件读入流while ((charread = read.read(tempchars)) != -1) {// 一次读多个字符// 同样屏蔽掉r不显示if ((charread == tempchars.length)&& (tempchars[tempchars.length - 1] != 'r')) {System.out.print(tempchars);} else {for (int i = 0; i < charread; i++) {if (tempchars[i] == 'r') {continue;// 停止执行当前的迭代,然后退回循环开始处} else {System.out.print(tempchars[i]);}}}}} catch (Exception e1) {// 捕获异常e1.printStackTrace();} finally {// 内容总执行if (read != null) {try {read.close();// 确保关闭} catch (IOException e1) {}}}}/** * 以行为单位读取文件,常用于读面向行的格式化文件。 *  * @param fileName 文件绝对路径 */public static void readFileByLines(String fileName) {File file = new File(fileName);BufferedReader reader = null;// 创建缓存读取try {System.out.println("①.以行为单位读取文件内容,一次读一整行:");reader = new BufferedReader(new FileReader(file));// 将文件放在缓存读取中String tempString = null;int line = 1;// 一次读入一行,直到读入null为文件结束while ((tempString = reader.readLine()) != null) {// 显示行号System.out.println("line " + line + ": " + tempString);line++;}reader.close();} catch (IOException e) {// 捕获异常e.printStackTrace();} finally {// 内容总执行if (reader != null) {try {reader.close();// 关闭缓存读取} catch (IOException e1) {}}}}/** * 显示输入流中还剩的字节数 *  * @param in  */private static void showAvailableBytes(InputStream in) {try {System.out.println("当前字节输入流中的字节数为:" + in.available());} catch (IOException e) {// 捕获异常e.printStackTrace();}}public static void main(String[] args) {String fileName = "E:/poem.txt";System.out.println("1.按字节为单位读取文件:");readFileByBytes(fileName);System.out.println("2.按字符为单位读取文件:");readFileByChars(fileName);System.out.println("3.按行为单位读取文件:");readFileByLines(fileName);}}


原创粉丝点击