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 HttpComponents 获取Cookie
package org.apache.http.examples.client; import java.util.List; import org.apache.http.HttpEntity; i ...
- Android——列表视图 ListView(一)Arrayadapter
一.ArrayAdapter 只显示文字 activitylistview_layout.xml <?xml version="1.0" encoding="utf ...
- linux 使用NSF 映射远程磁盘目录
假设源目录在192.168.1.1机器上,目录为/data 客户端集群在192.168.1.2, 需要将192.168.1.1机器上的/data目录到本地的/data目录 1.在两台机器上安装nsf ...
- Entity Framework管理实体关系(二):管理一对二关系
在上一篇文章中,简单的介绍了使用Fluent API如何管理一对一的实体关系,在这篇文章中,接着介绍Fluent API如何管理一对多的实体关系. 要在数据库中配置一对多关系,我们可以依赖EF约定,还 ...
- 刘昕明:送给和我一样曾经浮躁过的PHP程序员
作者注:2012年偶决定开始写博客了,不为别的,就希望可以通过博客记录我的成长历程同时也希望可以帮助一些刚毕业,刚入行业的兄弟姐们们.我们是一群充满浮躁.抱怨.迷茫的程序猿,想一想3年就这么过去了,社 ...
- .net开发遇到的一个问题
之前项目有个entity是写在Entity层的,相关的配置项也写死在程序里了,而且还是个static的配置,后来有了新需求,上峰指示要从CMS读取配置内容,大概是要在BLL实现,BLL依赖IBLL的I ...
- 【转】BMP图像文件格式
5.1 BMP图像文件格式 BMP图像文件格式是游戏中常用的图像资源文件格式,BMP图像文件起源早,程序员对BMP都比较熟悉,再加上BMP格式简单,读取和写入非常容易实现,所以无论Windows的还 ...
- windows 批处理文件中引用日期
参见:http://blog.csdn.net/iw1210/article/details/39313677 %DATE%输出的是: yyyy/mm/dd 星期* (例如:2008/12/18 星期 ...
- IoC最大的好处是什么?
IoC最大的好处是什么?因为把对象生成放在了XML里定义,所以当我们需要换一个实现子类将会变成很简单(一般这样的对象都是实现于某种接口的),只要修改XML就可以了,这样我们甚至可以实现对象的热插拨(有 ...
- EntityFramework定向加载实体
Reference()和Collection() 方法 IList<Student> studList = context.Students.ToList<Student>() ...