java⽂件读取与写⼊
package com.myjava;import java.io.*;
import java.util.ArrayList;import java.util.Collections;import java.util.List;
/**
* Created by 14061371 on 2017/5/12. */
public class FileUtils { //获取当前路径
/** 按字节读取 *
* @param fileName */
public static void readFileByBytes(String fileName){ File file=new File(fileName); if(!file.exists()){ return; }
InputStream in = null; try {
in = new FileInputStream(file); byte[] tempbytes = new byte[100]; int byteread = 0;
in = new FileInputStream(fileName);
// 读⼊多个字节到字节数组中,byteread为⼀次读⼊的字节数 while ((byteread = in.read(tempbytes)) != -1) { System.out.write(tempbytes, 0, byteread); }
} catch (IOException e) { e.printStackTrace(); }finally {
if (in != null) { try {
in.close();
} catch (IOException e1) { } } } }
/**
* 按字符读取
* @param fileName */
public static void readFileByChars(String fileName){ File file=new File(fileName); if(!file.exists()){ return; }
Reader reader=null; try {
reader=new InputStreamReader(new FileInputStream(file)); int tempChart;
char[] tempchars = new char[30];
while((tempChart=reader.read(tempchars))!=-1){ System.out.print(tempchars); }
} catch (IOException e) { e.printStackTrace(); }finally{
if(reader!=null){ try{
reader.close();
}catch (IOException e){ } } } }
/**
* 按⾏读取
* @param fileName */
public static void readFileByLines(String fileName){ File file=new File(fileName);
if(!file.exists()){ return; }
BufferedReader reader=null; try {
reader=new BufferedReader(new FileReader(file)); String tempString=null; int line=1;
while((tempString=reader.readLine())!=null){ System.out.println(line+tempString); line++; }
reader.close();
} catch (IOException e) { e.printStackTrace(); }finally{
if(reader!=null){ try{
reader.close();
}catch(IOException e){ } } } }
/**
* random读取⽂件 * @param fileName */
public static void readFileByRandomAccess(String fileName){ RandomAccessFile randomFile=null; try {
randomFile=new RandomAccessFile(fileName,\"r\"); long fileLength=randomFile.length(); randomFile.seek(0);
byte[] bytes=new byte[10]; int byteread=0;
while((byteread=randomFile.read(bytes))!=-1){ System.out.write(bytes,0,byteread); }
} catch (IOException e) { e.printStackTrace(); }finally {
if (randomFile != null) { try {
randomFile.close(); } catch (IOException e) { } } } }
/**
* 读取⽂件最后N⾏ *
* 根据换⾏符判断当前的⾏数, * 使⽤统计来判断当前读取第N⾏ *
* PS:输出的List是倒叙,需要对List反转输出 *
* @param file 待⽂件
* @param numRead 读取的⾏数 * @return List */public static List readLastNLine(File file, long numRead) { // 定义结果集List result = new ArrayList(); //⾏数统计long count = 0;
// 排除不可读状态
if (!file.exists() || file.isDirectory() || !file.canRead()) { return null; }
// 使⽤随机读取
RandomAccessFile fileRead = null; try {
//使⽤读模式
fileRead = new RandomAccessFile(file, \"r\"); //读取⽂件长度
long length = fileRead.length();
//如果是0,代表是空⽂件,直接返回空结果 if (length == 0L) { return result; } else {
//初始化游标
long pos = length - 1; while (pos > 0) { pos--;
//开始读取
fileRead.seek(pos);
//如果读取到\\n代表是读取到⼀⾏ if (fileRead.readByte() == '\\n') { //使⽤readLine获取当前⾏
String line = fileRead.readLine(); //保存结果
result.add(line); //打印当前⾏
// System.out.println(line);
//⾏数统计,如果到达了numRead指定的⾏数,就跳出循环 count++;
if (count == numRead) { break; } } }
if (pos == 0) {
fileRead.seek(0);
result.add(fileRead.readLine()); } }
} catch (IOException e) { e.printStackTrace(); } finally {
if (fileRead != null) { try {
//关闭资源
fileRead.close(); } catch (Exception e) { } } }
Collections.reverse(result); return result; }
/**
* 使⽤RandomAccessFile追加⽂件 * @param fileName */
public static void appendFileByAccess(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 追加⽂件内容 * @param fileName * @param content */
public static void appendFile(String fileName,String content){ try {
FileWriter writer=new FileWriter(fileName,true); writer.write(content); writer.close();
} catch (IOException e) { e.printStackTrace(); } }
/**
* 往⽂件头插⼊数据 * @param filename * @param pos
* @param insertContent */
public static void insert(String filename, int pos, String insertContent){ RandomAccessFile raf=null;
FileOutputStream tmpOut=null; FileInputStream tmpIn=null; try{
File tmp = File.createTempFile(\"tmp\null); tmp.deleteOnExit();
raf = new RandomAccessFile(filename, \"rw\"); tmpOut = new FileOutputStream(tmp); tmpIn = new FileInputStream(tmp); raf.seek(pos);//⾸先的话是0 byte[] buf = new byte[]; int hasRead = 0;
while ((hasRead = raf.read(buf)) > 0) { //把原有内容读⼊临时⽂件
tmpOut.write(buf, 0, hasRead); }
raf.seek(pos);
raf.write(insertContent.getBytes()); //追加临时⽂件的内容
while ((hasRead = tmpIn.read(buf)) > 0) { raf.write(buf, 0, hasRead); }
}catch (IOException e) { e.printStackTrace(); }finally{ try {
if (raf != null) { raf.close(); }
if (tmpOut != null) { tmpOut.close(); }
if (tmpIn != null) { tmpIn.close(); }
} catch (IOException e) { } } }}