Java读取文件


package com.yfli.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

/**
 * [Java]读取文件方法大全 
 * 1、按字节读取文件内容
 * 2、按字符读取文件内容 
 * 3、按行读取文件内容 
 * 4、随机读取文件内容
 * 
 * @author yfli
 * 
 */
public class ReadFile {
	/**
	 * 1、以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
	 */
	public static void readFileByBytes(String fileName) {
		File file = new File(fileName);
		try {
			byte[] tempbytes = new byte[100];
			int i = 0;
			InputStream in = new FileInputStream(file);
			while ((i = in.read(tempbytes)) != -1) {
				System.out.write(tempbytes, 0, i);
			}
			in.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} 
	}

	/**
	 * 2、以字符为单位读取文件,常用于读文本,数字等类型的文件
	 */
	public static void readFileByChars(String fileName) {
		File file = new File(fileName);
		try {
			InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
			
			//1、一次读取 一个字节
			/*int tempchar =0 ;
			while ((tempchar = reader.read()) != -1) {
				if (((char) tempchar) != '\r') {
					System.out.print((char) tempchar);
				}
			}*/
			
			//2、一次读取多个字节
			char[] tempchars = new char[1024];
			int charread = 0;
			while ((charread = reader.read(tempchars)) != -1) {
				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]);
						}
					}
				}
			}
			reader.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} 
	}

	/**
	 * 3、以行为单位读取文件,常用于读面向行的格式化文件
	 */
	public static void readFileByLines(String fileName) {
		File file = new File(fileName);
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 0;
			while ((tempString = reader.readLine()) != null) {
				System.out.println(line + " :" + tempString);
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	/**
	 * 4、随机读取文件内容
	 */
	public static void readFileByRandomAccess(String fileName) {
		try {
			RandomAccessFile randomFile  = new RandomAccessFile(fileName, "r");// 打开一个随机访问文件流,按只读方式
			long fileLength = randomFile.length();// 文件长度,字节数
			int beginIndex = (fileLength > 4) ? 4 : 0;// 读文件的起始位置
			randomFile.seek(beginIndex);// 将读文件的开始位置移到beginIndex位置。
			byte[] bytes = new byte[10];
			int byteread = 0;
			while ((byteread = randomFile.read(bytes)) != -1) {
				System.out.write(bytes, 0, byteread);
			}
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	/**
	 * 追加文件:使用RandomAccessFile
	 */
	public static void appendMethodA(String fileName, String content) {
		try {
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");// 打开一个随机访问文件流,按读写方式
			long fileLength = randomFile.length();// 文件长度,字节数
			randomFile.seek(fileLength);// 将写文件指针移到文件尾。
			randomFile.writeBytes(content);
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 追加文件:使用FileWriter
	 */
	public static void appendMethodB(String fileName, String content) {
		try {
			// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
			FileWriter writer = new FileWriter(fileName, true);
			writer.write(content);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值