java中文件操作的工具类
代码:
package com.lky.pojo; import java.io.BufferedReader;
import java.io.BufferedWriter;
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 org.junit.After;
import org.junit.Ignore;
import org.junit.Test; /**
* @Title: fileUtil.java
* @Package com.lky.pojo
* @Description: 文件操作的工具类
* @author lky
* @date 2015年10月20日 下午4:21:35
* @version V1.0
*/
public class fileUtil { /**
* @Title: stringToFile
* @Description: 将字符串存入文件(适用于任何类型的数据:图片,音频,视频,文本)
* @param str 待存储的字符串
* @param fname 文件名
* @param flag 是否以追加的方式写入文件
*/
public void stringToFile(String str, String fname, boolean flag) {
FileOutputStream fos = null;
File file = new File(fname);
try {
if (!file.exists()) {
String parentName = file.getParent();// 获取父文件夹名
new File(parentName).mkdirs();// 创建父文件夹
file.createNewFile();
}
fos = new FileOutputStream(file, flag);
fos.write(str.getBytes());
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* @Title: StringToFileBuffered
* @Description: 将字符串存入文件(仅在存入的数据为纯文本时,推荐使用,其它类型不使用)
* @param str 待存储的字符串
* @param fname 文件名
* @param flag 是否以追加的方式写入文件
*/
public void StringToFileBuffered(String str, String fame, boolean flag) {
BufferedWriter bw = null;
FileWriter fw = null;
File file = new File(fame); try {
if (!file.exists()) {
new File(file.getParent()).mkdirs();
file.createNewFile();
} fw = new FileWriter(file, flag);
bw = new BufferedWriter(fw); bw.write(str);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
} /**
* @Title: fileToStringBuffered
* @Description: 从文件中读入字符串(仅当该文件为文本文件,推荐使用)
* @param 文件名
*/
public String fileToStringBuffered(String fname) {
BufferedReader br = null;
FileReader fr = null;
StringBuffer sBuffer = new StringBuffer();
File file = new File(fname); try {
if (!file.exists()) {
return sBuffer.append(new String("文件不存在")).toString();
} fr = new FileReader(file);
br = new BufferedReader(fr); String line = null;
while ((line = br.readLine()) != null) {
sBuffer.append(line+"\r\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return sBuffer.toString();
} /**
* @Title: fileToStringBurstMode
* @Description: 从文件中读入数据(快字节流方式)
* @param 文件名
*/
public String fileToStringBurstMode(String fname) {
File file = new File(fname);
FileInputStream fis = null;
StringBuffer sBuffer = new StringBuffer();
try {
if (!file.exists()) {
System.out.println("文件不存在。。。。");
return sBuffer.toString();
}
fis = new FileInputStream(file);
int length = fis.available();
if (length <= 0)
return sBuffer.toString();
;
byte[] buffer = new byte[length];
int offset = 0;
int toRead = length - offset;
while (toRead > 0) {
int len = fis.read(buffer, offset, toRead);
offset += len;
toRead -= len;
}
sBuffer.append(new String(buffer, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sBuffer.toString();
} /**
* @Title: filetoStringSlowMode
* @Description: 从文件中读入数据(慢字节流方式)
* @param 文件名
*/
public String filetoStringSlowMode(String fname) {
FileInputStream fis = null;
ByteArrayOutputStream baos = null;
StringBuffer sBuffer = new StringBuffer();
File file = new File(fname); try {
if (!file.exists()) {
return sBuffer.append("该文件不存在").toString();
} baos = new ByteArrayOutputStream();
fis = new FileInputStream(file);
int len = 0;
while ((len = fis.read()) != -1) {
baos.write(len);
} sBuffer.append(baos.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (baos != null) {
baos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return sBuffer.toString();
} @Test
@Ignore
public void test1() {
stringToFile("l love you " + "\r\n", "c:\\li\\one.txt", true);
} @After
public void testAfter() {
System.out.println("---------------------------");
} @Test
@Ignore
public void test2() {
System.out.println(fileToStringBurstMode("c:\\one.txt"));
} @Test
@Ignore
public void test3() {
System.out.println(filetoStringSlowMode("c:\\one.txt"));
} @Test
public void test4(){
System.out.println(fileToStringBuffered("c:\\zhang\\one.txt"));
} @Test
@Ignore
public void test5(){
StringToFileBuffered("l love you " + "\r\n", "c:\\zhang\\one.txt", true);
}
}
java中文件操作的工具类的更多相关文章
- java中excel导入\导出工具类
1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...
- java中定义一个CloneUtil 工具类
其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...
- java中的Arrays这个工具类你真的会用吗
Java源码系列三-工具类Arrays 今天分享java的源码的第三弹,Arrays这个工具类的源码.因为近期在复习数据结构,了解到Arrays里面的排序算法和二分查找等的实现,收益匪浅,决定研读 ...
- Java 借助poi操作PDF工具类
一直以来说写一个关于Java操作PDF的工具类,也没有时间去写,今天抽空写一个简单的工具类,拥有PDF中 换行,字体大小,字体设置,字体颜色,首行缩进,居中,居左,居右,增加新一页等功能,如果需要 ...
- java中文件操作《一》
在日常的开发中我们经常会碰到对文件的操作,在java中对文件的操作都在java.io包下,这个包下的类有File.inputStream.outputStream.FileInputStream.Fi ...
- 在JAVA中自定义连接数据库的工具类
为什么要自定义数据库连接的工具类: 在开发中,我们在对数据库进行操作时,必须要先获取数据库的连接,在上一篇随笔中提到的获取数据库连接的步骤为: 1.定义好4个参数并赋值 2.加载驱动类 3.获取数据库 ...
- Java中的集合Collections工具类(六)
操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...
- java里poi操作Excel工具类【我改】
参考原文: https://www.cnblogs.com/yizhang/p/7244917.html 我改: package test; import java.io.File; import j ...
- java中重要的多线程工具类
前言 之前学多线程的时候没有学习线程的同步工具类(辅助类).ps:当时觉得暂时用不上,认为是挺高深的知识点就没去管了.. 在前几天,朋友发了一篇比较好的Semaphore文章过来,然后在浏览博客的时候 ...
随机推荐
- 深入探讨 java.lang.ref 包--转
概述 Java.lang.ref 是 Java 类库中比较特殊的一个包,它提供了与 Java 垃圾回收器密切相关的引用类.这些引用类对象可以指向其它对象,但它们不同于一般的引用,因为它们的存在并不防碍 ...
- Java-20个非常有用的程序片段
下面是20个非常有用的Java程序片段,希望能对你有用. 1.字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric string ...
- TODO、FIXME和XXX转载
代码中特殊的注释技术——TODO.FIXME和XXX的用处 // TODO Auto-generated method stub
- php的mq客户端获取队列方法改造
获取mq中消息然后处理失败重试机制: 下面的代码是php连接mq客户端的获取queue队列中的消息代码: public function createDurableSubscriber($queue, ...
- 在浏览器中打不开Oracle 11gR2的企业管理器页面
最简单的办法,重建EM 四个步骤: emca -repos drop emca -repos create emca -config dbcontrol db emctl start dbconsol ...
- ORACLE日期加减【转】
首先,感谢这个作者的辛勤汗水给我们带来的总结,因为日期函数操作对平时的使用真的是很常用,所以收藏一下以作后期使用. 原贴地址:http://www.cnblogs.com/xiao-yu/archiv ...
- PHP HTTP
安装 HTTP 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP HTTP 函数 PHP:指示支持该函数的最早的 PHP 版本. 函数 描述 PHP header() 向客户端发送原 ...
- zepto源码研究 - zepto.js - 1
简要:网上已经有很多人已经将zepto的源码研究得很细致了,但我还是想写下zepto源码系列,将别人的东西和自己的想法写下来以加深印象也是自娱自乐,文章中可能有许多错误,望有人不吝指出,烦请赐教. 首 ...
- DOM动态添加表格
var table=document.createElement("table"); table.border=1; var b=document.createElement(&q ...
- HashTable 及应用
HashTable-散列表/哈希表,是根据关键字(key)而直接访问在内存存储位置的数据结构. 它通过一个关键值的函数将所需的数据映射到表中的位置来访问数据,这个映射函数叫做散列函数,存放记录的数组叫 ...