java读取与写入_java文件读取与写入

packagecom.myjava;import java.io.*;importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;/*** Created by 14061371 on 2017/5/12.*/

public classFileUtils {//获取当前路径

/**按字节读取

*

*@paramfileName*/

public static voidreadFileByBytes(String fileName){

File file=newFile(fileName);if(!file.exists()){return;

}

InputStream in= null;try{

in= newFileInputStream(file);byte[] tempbytes = new byte[100];int byteread = 0;

in= newFileInputStream(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) {

}

}

}

}/*** 按字符读取

*@paramfileName*/

public static voidreadFileByChars(String fileName){

File file=newFile(fileName);if(!file.exists()){return;

}

Reader reader=null;try{

reader=new InputStreamReader(newFileInputStream(file));inttempChart;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){

}

}

}

}/*** 按行读取

*@paramfileName*/

public static voidreadFileByLines(String fileName){

File file=newFile(fileName);if(!file.exists()){return;

}

BufferedReader reader=null;try{

reader=new BufferedReader(newFileReader(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读取文件

*@paramfileName*/

public static voidreadFileByRandomAccess(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反转输出

*

*@paramfile 待文件

*@paramnumRead 读取的行数

*@returnList*/

public static List readLastNLine(File file, longnumRead) {//定义结果集

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) {returnresult;

}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);returnresult;

}/*** 使用RandomAccessFile追加文件

*@paramfileName*/

public static voidappendFileByAccess(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 追加文件内容

*@paramfileName

*@paramcontent*/

public static voidappendFile(String fileName,String content){try{

FileWriter writer=new FileWriter(fileName,true);

writer.write(content);

writer.close();

}catch(IOException e) {

e.printStackTrace();

}

}/*** 往文件头插入数据

*@paramfilename

*@parampos

*@paraminsertContent*/

public static void insert(String filename, intpos, 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= newFileOutputStream(tmp);

tmpIn= newFileInputStream(tmp);

raf.seek(pos);//首先的话是0

byte[] buf = new byte[64];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) {

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值