Java读取文件

来源:互联网 发布:淘宝客服面试基本问题 编辑:程序博客网 时间:2024/06/13 18:32


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
原创粉丝点击