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的更多相关文章

  1. FileUtil(from logparser)

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; ...

  2. 【hadoop】——修改hadoop FileUtil.java,解决权限检查的问题

    在Hadoop Eclipse开发环境搭建这篇文章中,第15.)中提到权限相关的异常,如下: 15/01/30 10:08:17 WARN util.NativeCodeLoader: Unable ...

  3. hadoop 文件 复制 移动 FileUtil.copy

    FileSystem fs = FileSystem.get(conf); Path pattern = new Path(resultViewPath + "phone*"); ...

  4. Android FileUtil(android文件工具类)

    android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...

  5. FileUtil.java

    package com.founder.util.file; import java.io.BufferedReader; import java.io.File; import java.io.Fi ...

  6. java FileUtil工具类

    网上的版本太多,整合了一下适合自己用的工具类,包括读取.保存.拷贝文件等. public class FileUtil { /** * 私有构造方法,防止类的实例化,因为工具类不需要实例化. */ p ...

  7. 微信小游戏的本地缓存和清除的使用 (text.js image.js file-util.js)

    参考: 微信小游戏,文件系统 UpdateManager-小游戏 一.Egret提供的本地缓存工具类( 备注:新版本进行了修改,并增加了sound.js等) 在微信小游戏项目中,Egret提供了fil ...

  8. 修改hadoop FileUtil.java,解决权限检查的问题

        在Hadoop Eclipse开发环境搭建这篇文章中,第15.)中提到权限相关的异常,如下: 15/01/30 10:08:17 WARN util.NativeCodeLoader: Una ...

  9. java FileUtil(文件操作类)

    package tools; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; i ...

随机推荐

  1. HTML元素的ID和Name属性的区别

    HTML元素的ID和Name属性的区别今天突然兴致来了,想深究下这两属性的具体区别最classical的答案:ID就像是一个人的身份证号码,而Name就像是他的名字,ID显然是唯一的,而Name是可以 ...

  2. zoj 3490

    蛋都疼了,高了半天,Output Limit Exceeded 原来是输入的问题,我靠!!以后还是用输入输出c++好,这尼玛!!郁闷!!!!! #include<stdio.h> #inc ...

  3. 记录一次webbrowser无法加载 activex 遇到的问题

    关联配置: win7 x64 Adobe Reader XI activex 安装目录X84 笔者项目运行Any CPU 无论如何也加载不出PDF 刚开始还以为自己封装的控件XWebBrowser的问 ...

  4. Session会话跟踪

    用encodeURL重写URL public class SessionA extends HttpServlet { @Override protected void doGet(HttpServl ...

  5. Android应用开发基础篇(16)-----ScaleGestureDetector(缩放手势检测)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/11/2390274.html 一.概述 ScaleGestureDetector这个类是专门用来 ...

  6. BZOJ 2821: 作诗(Poetize)( 分块 )

    分块,分成N^0.5块.O(N^1.5)预处理出sm[i][j]表示前i块中j的出现次数, ans[i][j]表示第i~j块的答案. 然后就可以O(N^0.5)回答询问了.总复杂度O((N+Q)N^0 ...

  7. C文件操作(转载)

    /*1.文件的打开(fopen函数)fopen函数用来打开一个文件,其调用的一般形式为:文件指针=fopen(文件名,文件操作方式):例如:FILE *fpinfpin=fopen("c:\ ...

  8. Ubuntu 12.04中文输入法的安装(转)

    Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu的中文系统中自带了中文输入法,通过Ctrl+S ...

  9. 转 释一首美国民谣:沉默之音(The Sound Of Silence)

    Ask not what your country can do for you , ask what you can do for your country.    六十年代对美国而言是个多事之秋的 ...

  10. PowerPoint2003/2007幻灯片常用快捷键大全

    熟习掌握PowerPoint快捷键不仅可以对我们在制作幻灯片的时候更加熟练节省时间.而且在我们演示PPT文稿的时候,也不需要停下演讲去调试菜单工具来切换下一张,我们只需要轻轻的按一下键盘即可轻松切换P ...