java 文件处理 工具_java文件处理工具类

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

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 java.io.InputStream;

import java.io.InputStreamReader;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import java.io.RandomAccessFile;

import java.io.Reader;

import java.util.ArrayList;

import java.util.List;

/**

* java文件处理工具类

*

* @author lay at 2014年8月8日11:30:38

*

* @version 1.0

*/

public class FileUtil {

/**

* 复制文件

*

* @param srcPath

* 源文件

* @param desPath

* 目标文件

* @return boolean 是否复制成功

*/

public static boolean copyFile(String srcPath, String desPath) {

FileInputStream is = null;

OutputStream os = null;

BufferedInputStream bufInput = null;

BufferedOutputStream bufOnput = null;

File file = new File(desPath);

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

if (file.exists()) {

file = null;

return false;

}

if (!new File(srcPath).exists()) {

return false;

}

try {

is = new FileInputStream(srcPath);

os = new FileOutputStream(desPath);

bufInput = new BufferedInputStream(is);

bufOnput = new BufferedOutputStream(os);

int read = bufInput.read();

while (read != -1) {

bufOnput.write(read);

read = bufInput.read();

}

bufOnput.flush();

return true;

} catch (IOException e) {

return false;

} finally {

try {

if (bufInput != null) {

bufInput.close();

bufInput = null;

}

} catch (Exception e) {

}

try {

if (bufOnput != null) {

bufOnput.close();

bufOnput = null;

}

} catch (Exception e) {

}

try {

if (os != null) {

os.close();

os.close();

}

} catch (Exception e) {

}

try {

if (is != null) {

is.close();

is.close();

}

} catch (Exception e) {

}

}

}

/**

* 以对象流将对象写入文件

*

* @param filePath

* @param obj

*/

public static void writeObject(String filePath, Object obj) {

OutputStream os = null;

ObjectOutputStream oos = null;

File file = new File(filePath);

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

try {

os = new FileOutputStream(filePath);

oos = new ObjectOutputStream(os);

oos.writeObject(obj);

oos.flush();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (oos != null) {

oos.close();

oos = null;

}

} catch (Exception e) {

e.printStackTrace();

}

try {

if (os != null) {

os.close();

os = null;

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

* 以对象流从文件中读取对象

*

* @param filePath

* @return

*/

public static Object readObject(String filePath) {

Object obj = null;

FileInputStream is = null;

ObjectInputStream ois = null;

try {

is = new FileInputStream(filePath);

ois = new ObjectInputStream(is);

obj = ois.readObject();

} catch (Exception e) {

} finally {

try {

if (ois != null) {

ois.close();

}

} catch (IOException e) {

}

try {

if (is != null) {

is.close();

}

} catch (IOException e) {

}

}

return obj;

}

/**

* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

*

* @param fileName

* 文件的名

* @return

*/

public static byte[] readFileByBytes(String fileName) {

InputStream in = null;

byte[] b = null;

try {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] tempbytes = new byte[1024];

int len = 0;

in = new FileInputStream(fileName);

while ((len = in.read(tempbytes)) != -1) {

baos.write(tempbytes, 0, len);

}

byte[] temp = baos.toByteArray();

System.arraycopy(temp, 0, b, 0, temp.length);

baos = null;

temp = null;

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e1) {

}

}

}

return b;

}

/**

* 以字符为单位读取文件,常用于读文本,数字等类型的文件

*

* @param fileName

* 文件名

* @return

*/

public static char[] readFileByChars(String fileName) {

File file = new File(fileName);

Reader reader = null;

char[] ch = null;

try {

reader = new InputStreamReader(new FileInputStream(file));

List list = new ArrayList();

int tempchar;

while ((tempchar = reader.read()) != -1) {

list.add((char) tempchar);

}

reader.close();

ch = new char[list.size()];

for (int i = 0; i < list.size(); i++) {

ch[i] = list.get(i);

}

} catch (Exception e) {

e.printStackTrace();

}

return ch;

}

/**

* 以行为单位读取文件,常用于读面向行的格式化文件

*

* @param fileName

* 文件名

* @return

*/

public static String[] readFileByLines(String fileName) {

File file = new File(fileName);

FileInputStream fis = null;

InputStreamReader isr = null;

BufferedReader reader = null;

String[] strs = null;

try {

List list = new ArrayList();

fis = new FileInputStream(file);

// 如果是windows自建的txt,其自带编码格式GBK,读的时候需要自带编码如下

// isr = new InputStreamReader(fis, "GBK");

isr = new InputStreamReader(fis);

reader = new BufferedReader(isr);

String tempString = null;

while ((tempString = reader.readLine()) != null) {

list.add(tempString);

}

reader.close();

strs = new String[list.size()];

strs = list.toArray(strs);

list = null;

} catch (IOException e) {

e.printStackTrace();

} finally {

if (fis != null) {

try {

fis.close();

} catch (IOException e1) {

}

}

if (isr != null) {

try {

isr.close();

} catch (IOException e1) {

}

}

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

return strs;

}

/**

* 随机读取文件内容

*

* @param fileName

* 文件名

*/

public static void readFileByRandomAccess(String fileName) {

RandomAccessFile randomFile = null;

try {

// 打开一个随机访问文件流,按只读方式

randomFile = new RandomAccessFile(fileName, "r");

// 文件长度,字节数

long fileLength = randomFile.length();

// 读文件的起始位置

int beginIndex = (fileLength > 4) ? 4 : 0;

// 将读文件的开始位置移到beginIndex位置。

randomFile.seek(beginIndex);

byte[] bytes = new byte[10];

int byteread = 0;

// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

// 将一次读取的字节数赋给byteread

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 e1) {

}

}

}

}

/**

* A方法追加文件:使用RandomAccessFile

*

* @param fileName

* 文件名

* @param content

* 追加的内容

*/

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();

}

}

/**

* B方法追加文件:使用FileWriter

*

* @param fileName

* 文件名

* @param content

* 追加的内容

*/

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();

}

}

}

package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值