java中文件操作的工具类

java

代码:

package com.lky.pojo;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import org.junit.After;

import org.junit.Ignore;

import org.junit.Test;

/**

* @Title: fileUtil.java

* @Package com.lky.pojo

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

* @author lky

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

* @version V1.0

*/

public class fileUtil {

/**

* @Title: stringToFile

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

* @param str 待存储的字符串

* @param fname 文件名

* @param flag 是否以追加的方式写入文件

*/

public void stringToFile(String str, String fname, boolean flag) {

FileOutputStream fos = null;

File file = new File(fname);

try {

if (!file.exists()) {

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

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

file.createNewFile();

}

fos = new FileOutputStream(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: 将字符串存入文件(仅在存入的数据为纯文本时,推荐使用,其它类型不使用)

* @param str 待存储的字符串

* @param fname 文件名

* @param flag 是否以追加的方式写入文件

*/

public void StringToFileBuffered(String str, String fame, boolean flag) {

BufferedWriter bw = null;

FileWriter fw = null;

File file = new File(fame);

try {

if (!file.exists()) {

new File(file.getParent()).mkdirs();

file.createNewFile();

}

fw = new FileWriter(file, flag);

bw = new BufferedWriter(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 文件名

*/

public String fileToStringBuffered(String fname) {

BufferedReader br = null;

FileReader fr = null;

StringBuffer sBuffer = new StringBuffer();

File file = new File(fname);

try {

if (!file.exists()) {

return sBuffer.append(new String("文件不存在")).toString();

}

fr = new FileReader(file);

br = new BufferedReader(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();

}

}

return sBuffer.toString();

}

/**

* @Title: fileToStringBurstMode

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

* @param 文件名

*/

public String fileToStringBurstMode(String fname) {

File file = new File(fname);

FileInputStream fis = null;

StringBuffer sBuffer = new StringBuffer();

try {

if (!file.exists()) {

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

return sBuffer.toString();

}

fis = new FileInputStream(file);

int length = fis.available();

if (length <= 0)

return sBuffer.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();

}

}

}

return sBuffer.toString();

}

/**

* @Title: filetoStringSlowMode

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

* @param 文件名

*/

public String filetoStringSlowMode(String fname) {

FileInputStream fis = null;

ByteArrayOutputStream baos = null;

StringBuffer sBuffer = new StringBuffer();

File file = new File(fname);

try {

if (!file.exists()) {

return sBuffer.append("该文件不存在").toString();

}

baos = new ByteArrayOutputStream();

fis = new FileInputStream(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();

}

}

return sBuffer.toString();

}

@Test

@Ignore

public void test1() {

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

}

@After

public void testAfter() {

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

}

@Test

@Ignore

public void test2() {

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

}

@Test

@Ignore

public void test3() {

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

}

@Test

public void test4(){

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

}

@Test

@Ignore

public void test5(){

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

}

}

以上是 java中文件操作的工具类 的全部内容, 来源链接: utcz.com/389921.html

回到顶部