java文件操作工具类_java中文件操作的工具类

packagecom.lky.pojo;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importorg.junit.After;importorg.junit.Ignore;importorg.junit.Test;/*** @Title: fileUtil.java

* @Package com.lky.pojo

* @Description: 文件操作的工具类

*@authorlky

* @date 2015年10月20日 下午4:21:35

*@versionV1.0*/

public classfileUtil {/*** @Title: stringToFile

* @Description: 将字符串存入文件(适用于任何类型的数据:图片,音频,视频,文本)

*@paramstr 待存储的字符串

*@paramfname 文件名

*@paramflag 是否以追加的方式写入文件*/

public void stringToFile(String str, String fname, booleanflag) {

FileOutputStream fos= null;

File file= newFile(fname);try{if (!file.exists()) {

String parentName= file.getParent();//获取父文件夹名

new File(parentName).mkdirs();//创建父文件夹

file.createNewFile();

}

fos= newFileOutputStream(file, flag);

fos.write(str.getBytes());

fos.flush();

}catch(Exception e) {

e.printStackTrace();

}finally{if (fos != null) {try{

fos.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}/*** @Title: StringToFileBuffered

* @Description: 将字符串存入文件(仅在存入的数据为纯文本时,推荐使用,其它类型不使用)

*@paramstr 待存储的字符串

*@paramfname 文件名

*@paramflag 是否以追加的方式写入文件*/

public void StringToFileBuffered(String str, String fame, booleanflag) {

BufferedWriter bw= null;

FileWriter fw= null;

File file= newFile(fame);try{if (!file.exists()) {newFile(file.getParent()).mkdirs();

file.createNewFile();

}

fw= newFileWriter(file, flag);

bw= newBufferedWriter(fw);

bw.write(str);

bw.flush();

}catch(Exception e) {

e.printStackTrace();

}finally{try{if (bw != null) {

bw.close();

}if (fw != null) {

fw.close();

}

}catch(Exception e2) {

e2.printStackTrace();

}

}

}/*** @Title: fileToStringBuffered

* @Description: 从文件中读入字符串(仅当该文件为文本文件,推荐使用)

*@param文件名*/

publicString fileToStringBuffered(String fname) {

BufferedReader br= null;

FileReader fr= null;

StringBuffer sBuffer= newStringBuffer();

File file= newFile(fname);try{if (!file.exists()) {return sBuffer.append(new String("文件不存在")).toString();

}

fr= newFileReader(file);

br= newBufferedReader(fr);

String line= null;while ((line = br.readLine()) != null) {

sBuffer.append(line+"\r\n");

}

}catch(Exception e) {

e.printStackTrace();

}finally{try{if (br != null) {

br.close();

}if (fr != null) {

fr.close();

}

}catch(Exception e2) {

e2.printStackTrace();

}

}returnsBuffer.toString();

}/*** @Title: fileToStringBurstMode

* @Description: 从文件中读入数据(快字节流方式)

*@param文件名*/

publicString fileToStringBurstMode(String fname) {

File file= newFile(fname);

FileInputStream fis= null;

StringBuffer sBuffer= newStringBuffer();try{if (!file.exists()) {

System.out.println("文件不存在。。。。");returnsBuffer.toString();

}

fis= newFileInputStream(file);int length =fis.available();if (length <= 0)returnsBuffer.toString();

;byte[] buffer = new byte[length];int offset = 0;int toRead = length -offset;while (toRead > 0) {int len =fis.read(buffer, offset, toRead);

offset+=len;

toRead-=len;

}

sBuffer.append(new String(buffer, "utf-8"));

}catch(Exception e) {

e.printStackTrace();

}finally{if (fis != null) {try{

fis.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}returnsBuffer.toString();

}/*** @Title: filetoStringSlowMode

* @Description: 从文件中读入数据(慢字节流方式)

*@param文件名*/

publicString filetoStringSlowMode(String fname) {

FileInputStream fis= null;

ByteArrayOutputStream baos= null;

StringBuffer sBuffer= newStringBuffer();

File file= newFile(fname);try{if (!file.exists()) {return sBuffer.append("该文件不存在").toString();

}

baos= newByteArrayOutputStream();

fis= newFileInputStream(file);int len = 0;while ((len = fis.read()) != -1) {

baos.write(len);

}

sBuffer.append(baos.toString());

}catch(Exception e) {

e.printStackTrace();

}finally{try{if (fis != null) {

fis.close();

}if (baos != null) {

baos.close();

}

}catch(Exception e2) {

e2.printStackTrace();

}

}returnsBuffer.toString();

}

@Test

@Ignorepublic voidtest1() {

stringToFile("l love you " + "\r\n", "c:\\li\\one.txt", true);

}

@Afterpublic voidtestAfter() {

System.out.println("---------------------------");

}

@Test

@Ignorepublic voidtest2() {

System.out.println(fileToStringBurstMode("c:\\one.txt"));

}

@Test

@Ignorepublic voidtest3() {

System.out.println(filetoStringSlowMode("c:\\one.txt"));

}

@Testpublic voidtest4(){

System.out.println(fileToStringBuffered("c:\\zhang\\one.txt"));

}

@Test

@Ignorepublic voidtest5(){

StringToFileBuffered("l love you " + "\r\n", "c:\\zhang\\one.txt", true);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值