java FileUtil(文件操作类)
package tools; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import org.bson.Document; /**
* @author chenhuan001
*
*/
public class FileUtil {
String newline = "\r\n";//windows /**
* 写入文件,末尾自动添加\r\n
* utf-8 追加
* @param path
* @param str
*/
public static void writeLog(String path, String str)
{
try
{
File file = new File(path);
if(!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file); //true表示追加
StringBuffer sb = new StringBuffer();
sb.append(str + "\r\n");
out.write(sb.toString().getBytes("utf-8"));//
out.close();
}
catch(IOException ex)
{
System.out.println(ex.getStackTrace());
}
} /**
* 写入文件,末尾自动添加\r\n
* @param path
* @param str
*/
public static void writeLog(String path, String str, boolean is_append, String encode)
{
try
{
File file = new File(path);
if(!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file, is_append); //true表示追加
StringBuffer sb = new StringBuffer();
sb.append(str + "\r\n");
out.write(sb.toString().getBytes(encode));//
out.close();
}
catch(IOException ex)
{
System.out.println(ex.getStackTrace());
}
}
/**
* 整个文件以string放回,添加\r\n换行
* @param path
* @return
*/
public static String readLogByString(String path)
{
StringBuffer sb=new StringBuffer();
String tempstr=null;
try {
File file=new File(path);
if(!file.exists())
throw new FileNotFoundException();
FileInputStream fis=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(fis, "utf-8"));
while((tempstr=br.readLine())!=null) {
sb.append(tempstr + "\r\n");
}
} catch(IOException ex) {
System.out.println(ex.getStackTrace());
}
return sb.toString();
} /**
* 加入编码
* 整个文件以string放回,添加\r\n换行
* @param path
* @return
*/
public static String readLogByStringAndEncode(String path, String encode)
{
StringBuffer sb=new StringBuffer();
String tempstr=null;
try {
File file=new File(path);
if(!file.exists())
throw new FileNotFoundException();
FileInputStream fis=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(fis, encode));
while((tempstr=br.readLine())!=null) {
sb.append(tempstr + "\r\n");
}
} catch(IOException ex) {
System.out.println(ex.getStackTrace());
}
return sb.toString();
} /**
* 按行读取文件,以list<String>的形式返回
* @param path
* @return
*/
public static List<String> readLogByList(String path) {
List<String> lines = new ArrayList<String>();
String tempstr = null;
try {
File file = new File(path);
if(!file.exists()) {
throw new FileNotFoundException();
}
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis, "utf-8"));
while((tempstr = br.readLine()) != null) {
lines.add(tempstr.toString());
}
} catch(IOException ex) {
System.out.println(ex.getStackTrace());
}
return lines;
} public static List<Document> readDocsFromFile(String path) {
List<String> str_docs = readLogByList(path);
List<Document> docs = new ArrayList<Document>();
//System.out.println(str_docs.size());
for (int i = 0; i < str_docs.size(); i++) {
String str_doc = str_docs.get(i);
//System.out.println(str_doc);
Document doc = null;
try{
doc = Document.parse(str_doc);
} catch(Exception e) {
LogUtil.error("\nreadDocsFromFile 中异常, 文件:" + path + "\n第" + i + "行,\n" + str_doc);//好吧有一条没写完...
}
if (null != doc) {
docs.add(doc);
}
}
return docs;
} /**
* 创建目录
* @param dir_path
*/
public static void mkDir(String dir_path) {
File myFolderPath = new File(dir_path);
try {
if (!myFolderPath.exists()) {
myFolderPath.mkdir();
}
} catch (Exception e) {
LogUtil.error("新建目录操作出错");
e.printStackTrace();
}
} /**
* 创建文件
* @param file_path
*/
public static void createNewFile(String file_path) {
File myFilePath = new File(file_path);
try {
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
}
catch (Exception e) {
LogUtil.error("新建文件操作出错");
e.printStackTrace();
}
} /**
* 递归删除文件或者目录
* @param file_path
*/
public static void deleteEveryThing(String file_path) {
try{
File file=new File(file_path);
if(!file.exists()){
return ;
}
if(file.isFile()){
file.delete();
}else{
File[] files = file.listFiles();
for(int i=0;i<files.length;i++){
String root=files[i].getAbsolutePath();//得到子文件或文件夹的绝对路径
deleteEveryThing(root);
}
file.delete();
}
} catch(Exception e) {
LogUtil.error("删除文件失败");
}
} /*
* 得到一个文件夹下所有文件
*/
public static List<String> getAllFileNameInFold(String fold_path) {
List<String> file_paths = new ArrayList<String>(); LinkedList<String> folderList = new LinkedList<String>();
folderList.add(fold_path);
while (folderList.size() > 0) {
File file = new File(folderList.peekLast());
folderList.removeLast();
File[] files = file.listFiles();
ArrayList<File> fileList = new ArrayList<File>();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
folderList.add(files[i].getPath());
} else {
fileList.add(files[i]);
}
}
for (File f : fileList) {
file_paths.add(f.getAbsoluteFile().getPath());
}
}
return file_paths;
} public static void main(String[] args) {
// String path = "C:\\Users\\chenhuan001\\workspace\\CrawlSinaBySelenium\\src";
// List<String> file_paths = getAllFileNameInFold(path);
// for(String file_path : file_paths) {
// System.out.println(file_path);
// }
deleteEveryThing("C:\\Users\\chenhuan001\\Desktop\\testDelete.txt");
// TODO Auto-generated method stub
// List<Document> docs = readDocsFromFile("Data/user_program_data.txt");
// System.out.println(docs.size());
// for (int i = 0; i < docs.size(); i++) {
// System.out.println(docs.toString());
// }
//mkDir("tmp_dir");
//createNewFile("tmp_dir/new_file1.txt");
//deleteEveryThing("save.arff");
} }
java FileUtil(文件操作类)的更多相关文章
- java csv 文件 操作类
		
一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...
 - java的文件操作类File
		
java.io.File类,是java获取文件/文件夹的所有属性,和完成所有相关操作的类 例子: package test.file.IO; import java.io.*; public clas ...
 - Java文件操作类效率对比
		
前言 众所周知,Java中有多种针对文件的操作类,以面向字节流和字符流可分为两大类,这里以写入为例: 面向字节流的:FileOutputStream 和 BufferedOutputStream 面向 ...
 - JAVA文件操作类和文件夹的操作代码示例
		
JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...
 - android 文件操作类简易总结
		
android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...
 - java中文件操作《一》
		
在日常的开发中我们经常会碰到对文件的操作,在java中对文件的操作都在java.io包下,这个包下的类有File.inputStream.outputStream.FileInputStream.Fi ...
 - C# 文件操作类大全
		
C# 文件操作类大全 时间:2015-01-31 16:04:20 阅读:1724 评论:0 收藏:0 [点我收藏+] 标签: 1.创建文件夹 //usin ...
 - Android FileUtils 文件操作类
		
系统路径 Context.getPackageName(); // 用于获取APP的所在包目录 Context.getPackageCodePath(); //来获得当前应用程序对应的apk文件的路径 ...
 - File  文件操作类  大全
		
File 文件操作类 大全 许多人都会对文件操作感到很难 我也是 但是一个好的项目中必定会涉及到文件操作的 文件的复制 粘贴 等等等 公司大佬写了 一个文件操作的工具类 感觉还是棒棒的啦 ...
 
随机推荐
- 配置Apache与PHP的环境
			
http://www.cnblogs.com/zhcncn/archive/2013/05/09/3068318.html 1. 下载 Apache版本号为2.2.22. 最好下载msi安装文件.下载 ...
 - 轻松使用jquery解析XML
			
xml文件结构:books.xml <?xml version="1.0" encoding="UTF-8"?><root> &l ...
 - loadrunner  -56992
			
设置Run-time Settings ,network speed ,use bandwidth为 512: 回放脚本没有报错,5个并发运行场景报如下错误: vuser_init.c(12): Er ...
 - 最大割(Maximum cut)
			
问题描述:把图中点分为两部分V1和V2,使得V1和V2之间的连边值最大.
 - (转)基于live555的流媒体代理转发服务器
			
对于并发量并不大而且对性能要求不是很高的流媒体传输模块,live555还是很好的选择,下面说一下我所实现的流媒体代理服务器(目前只能实现对H264单视频的转发)代理转发主要 对于并发量并不大而且对性能 ...
 - 在 Mac 上搭建 Nginx PHP Mysql 开发环境
			
事实上这个过程跟Linux下安装都几乎相同,仅仅是部分命令有区别,大同小异. 网上看到非常多教程都是用 brew 之类的包管理器安装,可是 Mac 自带了 php , 难道还要再装一个第三方的?强迫症 ...
 - 手动安装OpenStack Mistral
			
Prepare packages: $ sudo apt-get install python-dev python-setuptools python-pip libffi-dev libxslt1 ...
 - 动态调用WCF不添加服务(svcutil.exe)
			
记录下 首先用svcutil.exe把指定wcf接口的信息下载下来. 生成代理类 比如说接口地址为 http://localhost:6666/Service1.svc 以管理员身份打开cmd 执形 ...
 - postman从入门到精通
			
今天总监让我给测试同事们培训postman,使用过postman的朋友应该知道,这个简直就是前后端接口调试神器.根据平时的经验以及自己到网上看了相关的帖子,对于postman又有了新的认识. post ...
 - 【Java面试题】57  short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
			
Java规范有这样的规则 [ 1.高位转低位需要强制转换 2.低位转高位自动转. ] short s1 = 1; s1 = s1 + 1;有什么错? 答: i 是int 型 s1 short型 通 ...