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<Character> list = new ArrayList<Character>();
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<String> list = new ArrayList<String>();
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();
}
} }

  

java文件处理工具类的更多相关文章

  1. Java 文件切割工具类

    Story: 发送MongoDB 管理软件到公司邮箱,工作使用. 1.由于公司邮箱限制附件大小,大文件无法发送,故做此程序用于切割大文件成多个小文件,然后逐个发送. 2.收到小文件之后,再重新组合成原 ...

  2. java文件读写工具类

    依赖jar:commons-io.jar 1.写文件 // by FileUtilsList<String> lines = FileUtils.readLines(file, " ...

  3. Java文件类型工具类

    package *; import java.util.HashMap; import java.util.Map; /** * <p> * <b>FileTypeEnum2& ...

  4. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  5. Java 压缩文件夹工具类(包含解压)

    依赖jar <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons ...

  6. 文件类型工具类:FileTypeUtil

    个人学习,仅供参考! package com.example.administrator.filemanager.utils;import java.io.File;/** * 文件类型工具类 * * ...

  7. HttpTool.java(在java tool util工具类中已存在) 暂保留

    HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...

  8. 文件夹工具类 - FolderUtils

    文件夹工具类,提供创建完整路径的方法. 源码如下:(点击下载 -FolderUtils.java .commons-io-2.4.jar ) import java.io.File; import o ...

  9. Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类

    Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类   =========================== ©Copyright 蕃薯耀 2017年9月25日 http://www ...

随机推荐

  1. sizeof()用法

    参考:sizeof_百度百科 sizeof()用法汇总(经典) 声明:本文是笔者抽出对自己有用的细节,对前两文的总结. 1.sizeof概念 sizeof是C语言中判断数据类型或者表达式长度符:不是一 ...

  2. IE8,IE9,IE10绿色版,以及ColorPix

    对于开发者而言IETest是一坨屎. 但是我们终于迎来的蛋糕和火腿,今天无意间发现了竟然有绿色版的浏览器版本,IE9,IE10已经下到本地,IE8网传是有的,但是没有找到合适的版本.但是足够了,本机使 ...

  3. nutch getOutLinks 外链的处理

    转载自: http://blog.csdn.net/witsmakemen/article/details/8067530 通过跟踪发现,Fetcher获得网页解析链接没有问题,获得了网页中所有的链接 ...

  4. bzoj 1002 [FJOI2007]轮状病毒 高精度&&找规律&&基尔霍夫矩阵

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2234  Solved: 1227[Submit][Statu ...

  5. nodejs发展

    http://www.infoq.com/cn/news/2012/11/netease-nodejs-framework http://www.jlmonteagudo.com/2013/06/ja ...

  6. 安卓天天练练(五)CompoundButton

    ToggleButton 让我想起了从前jQuery还没有取消toggle方法时是怎么偷懒的.. 注意: 如果LinearLayout,与RelativeLayout不同,必须有orientation ...

  7. 用Robotium 去实现点击imageview

    今天用rototium做自动化遇到imageview无法点击的问题,最终解决如下: 有两种方法: 1.View v = solo.getView(R.id.iv_main_setting);      ...

  8. BZOJ1613: [Usaco2007 Jan]Running贝茜的晨练计划

    1613: [Usaco2007 Jan]Running贝茜的晨练计划 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1138  Solved: 554[ ...

  9. 中国版 Azure 现提供 Azure Traffic Manager

    Stephen MaloneAzure网络 - DNS和 Traffic Manager高级项目经理 我们非常高兴地宣布,中国版 Azure中现已提供 Azure Traffic Manager.Az ...

  10. Selenium如何实现dropbar移动

    遇到这个拖拽的dropbar,如何实现呢,, 经过网上查找,可以用Action的方式实现或者js来控制 原理:移动按钮的同时,数字也随着变化 解决方法:1.最简单的就是直接在文本框输入相应的数字 2. ...