FileUtil
package com.wiseweb.util; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class FileUtil { // /**
// * 获取获取系统根文件夹下xml文件的路径的方法。
// */
// @SuppressWarnings("unused")
// private String projectFilePath = new File("").getAbsolutePath()
// + "\\config.xml";
//
// /**
// * 获取Web项目公布后(源代码中src下)classes文件夹下某个xml路径的方法;
// */
// @SuppressWarnings("unused")
// private String srcFilePath = getClass().getClassLoader().getResource(
// "config.xml").getPath(); File file = null;
boolean flag = false; /**
* 写入txt文件。能够在原文件内容的基础上追加内容(并推断文件夹是否存在,不存在则生成文件夹)
*
* @param value
* 写入文件内容
* @param fileCatage
* 文件父文件夹;
* @param fileName
* 文件名称字。
* @param code
* 文件的编码;
* @throws IOException
*/
public void WriteFile(String value, String fileCatage, String fileName,
String code) {
File file = null;
try {
file = new File(fileCatage);
if (!file.isDirectory())
file.mkdir();
else {
file = new File(fileCatage + fileName);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file, true);
out.write(value.getBytes(code));
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} /***
* 覆盖原来的内容;
*
* @param filePath
* 文件的路径
* @param content
* 保存的内容;
* @return
*/
public boolean saveFile(String filePath, String content) {
boolean successful = true;
FileOutputStream fout = null;
try {
fout = new FileOutputStream(new File(filePath), false);
fout.write(content.getBytes());
} catch (FileNotFoundException e1) {
successful = false;
} catch (IOException e) {
successful = false;
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
}
}
}
return successful;
} /**
* 删除文件的综合操作( 依据路径删除指定的文件夹或文件。不管存在与否)
*
* @param sPath
* 要删除的文件夹或文件
*@return 删除成功返回 true,否则返回 false。
*/
public boolean DeleteFolder(String sPath) {
flag = false;
file = new File(sPath);
// 推断文件夹或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 推断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(sPath);
} else { // 为文件夹时调用删除文件夹方法
return deleteDirectory(sPath);
}
}
} /**
* 删除单个文件
*
* @param sPath
* 被删除文件的文件名称
* @return 单个文件删除成功返回true,否则返回false
*/
public boolean deleteFile(String sPath) {
flag = false;
file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
} /**
* 删除文件夹(文件夹)以及文件夹下的文件
*
* @param sPath
* 被删除文件夹的文件路径
* @return 文件夹删除成功返回true,否则返回false
*/
public boolean deleteDirectory(String sPath) {
// 假设sPath不以文件分隔符结尾,自己主动加入文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
// 假设dir相应的文件不存在,或者不是一个文件夹,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
flag = true;
// 删除文件夹下的全部文件(包含子文件夹)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
// 删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
} // 删除子文件夹
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
// 删除当前文件夹
if (dirFile.delete()) {
return true;
} else {
return false;
}
} /**
* 按字节【读】取文件的内容;
*
* @param Offset
* 读取内容的開始出
* @param length
* 内容的长度。
* @param filePath
* 文件的路径。
* @param code
* 编码;
* @return 返回相应的内容;
* @throws Exception
*/
public String readFileByByte(int Offset, int length, String filePath,
String code) {
File file = new File(filePath);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
try {
fis.skip(Offset);
} catch (IOException e) {
e.printStackTrace();
return null;
}
byte[] bytes = new byte[length];
try {
fis.read(bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
try {
return new String(bytes, code);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
} /**
* 将流中的文本读入一个 BufferedReader 中
*
* @param filePath
* 文件路径
* @param code
* 编码格式
* @return
* @throws IOException
*/ public BufferedReader readToBufferedReader(String filePath, String code)
throws IOException {
BufferedReader bufferedReader = null;
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 推断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(
file), code);// 考虑到编码格式
bufferedReader = new BufferedReader(read);
}
return bufferedReader;
} /**
* 将流中的文本读入一个 StringBuffer 中
*
* @param filePath
* 文件路径
* @throws IOException
*/
public StringBuffer readToBuffer(String filePath, String code) {
StringBuffer buffer = new StringBuffer();
InputStream is;
try {
File file = new File(filePath);
if (!file.exists())
return null;
is = new FileInputStream(filePath);
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), code));
line = reader.readLine(); // 读取第一行
while (line != null) { // 假设 line 为空说明读完了
buffer.append(line); // 将读到的内容加入到 buffer 中
// buffer.append("\n"); // 加入换行符
line = reader.readLine(); // 读取下一行
}
reader.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return buffer;
} public String loadFile(String filePath, String charset) {
FileInputStream fin = null;
StringBuffer sb = new StringBuffer();
try {
fin = new FileInputStream(new File(filePath));
byte[] buffer = new byte[Integer.MAX_VALUE];
int start = -1;
while ((start = fin.read(buffer)) != -1) {
sb.append(new String(buffer, 0, start, charset));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
}
}
}
return sb.toString();
} /**
* 获取某个文件夹下全部文件或者获取某个文件的大小; 单位:MB
*
* @param file
* @return
*/
public static double getDirSize(File file) {
// 推断文件是否存在
if (file.exists()) {
// 假设是文件夹则递归计算其内容的总大小
if (file.isDirectory()) {
File[] children = file.listFiles();
double size = 0;
for (File f : children)
size += getDirSize(f);
return size;
} else {// 假设是文件则直接返回其大小,以“兆”为单位
double size = (double) file.length() / 1024 / 1024;
return size;
}
} else {
System.out.println("获取文件大小错误!。文件或者文件夹不存在。请检查路径是否正确! ");
return 0.0;
}
} /**
* 获取某个文件夹下全部的文件的全路径和文件名称的集合;
*
* @return
*/
public List<List<String>> getAllFile(String mulu) {
File file = new File(mulu);
File[] files = file.listFiles();
List<List<String>> ret = new ArrayList<List<String>>();
List<String> allFilePath = new ArrayList<String>();
List<String> allFileName = new ArrayList<String>();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
allFilePath.add(files[i].toString());
allFileName.add(files[i].getName());
}
}
ret.add(allFilePath);
ret.add(allFileName);
return ret;
} }
FileUtil的更多相关文章
- FileUtil(from logparser)
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; ...
- 【hadoop】——修改hadoop FileUtil.java,解决权限检查的问题
在Hadoop Eclipse开发环境搭建这篇文章中,第15.)中提到权限相关的异常,如下: 15/01/30 10:08:17 WARN util.NativeCodeLoader: Unable ...
- hadoop 文件 复制 移动 FileUtil.copy
FileSystem fs = FileSystem.get(conf); Path pattern = new Path(resultViewPath + "phone*"); ...
- Android FileUtil(android文件工具类)
android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...
- FileUtil.java
package com.founder.util.file; import java.io.BufferedReader; import java.io.File; import java.io.Fi ...
- java FileUtil工具类
网上的版本太多,整合了一下适合自己用的工具类,包括读取.保存.拷贝文件等. public class FileUtil { /** * 私有构造方法,防止类的实例化,因为工具类不需要实例化. */ p ...
- 微信小游戏的本地缓存和清除的使用 (text.js image.js file-util.js)
参考: 微信小游戏,文件系统 UpdateManager-小游戏 一.Egret提供的本地缓存工具类( 备注:新版本进行了修改,并增加了sound.js等) 在微信小游戏项目中,Egret提供了fil ...
- 修改hadoop FileUtil.java,解决权限检查的问题
在Hadoop Eclipse开发环境搭建这篇文章中,第15.)中提到权限相关的异常,如下: 15/01/30 10:08:17 WARN util.NativeCodeLoader: Una ...
- java FileUtil(文件操作类)
package tools; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; i ...
随机推荐
- 关于js封装框架类库之选择器引擎(一)
选择器模块之传统做法 var tag = function (tag){ return document.getElementsByTagName(tag); } var id = function ...
- C#复习二(Twenty First Day)
呵呵,又来到了今天的总结.这次主要复习了一下字符串的一些处理.今天就来总结一下. 理论: String 字符串,字符串可以看成字符数组,不可变特性(通过for循环,修改string中的元素,失败!) ...
- Session累计用户数据列表
OrderForm.html <body> <center> <h1 ><font size="20">Order Items& ...
- HDU 2841 Visible Trees(莫比乌斯反演)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...
- BZOJ 2662: [BeiJing wc2012]冻结(最短路)
这道题和 BZOJ 2763飞行路线 几乎一模一样..然后飞行路线我是1A,这道题WA了4次,我开始怀疑我的智商了.. ---------------------------------------- ...
- 帝国cms让当前栏目显示不同样式(图文)
在使用帝国cms制作栏目导航条时,我们可能会需要根据当前栏目,使当前栏目样式不同. 如图: 此类导航,源代码一般为 <li>全站首页</li> <li class=&qu ...
- 转;说说AngularJS中的$parse和$eval
说说AngularJS中的$parse和$eval AngularJS的初学者常常会对$parse和$eval两个内建服务感到有些困惑,今天我们就来说说AngularJS中的$parse和$eval. ...
- Xamarin几十篇博客,roslyn和dotnet也开源了
.Net 基金会 http://www.dotnetfoundation.org/ 更新的真快,刚打完2的补丁包,3就粗来了............ https://www.visualstudio. ...
- sed(查找替换) 与awk(提取字段)
通常: sed 处理列 awk处理行 比较方便 Sed是一个基本的查找替换程序 sed -i "s/^@//g" 文件 #原地操作原文件,进行替换 cat ...
- json数据与字符串的相互转化
json转成string[需要引用json2.js文件]: var arr=[{id:'id',name:'Spring'},{id:'id2',name:'Jane'}]; var str=JSON ...