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文章过来,然后在浏览博客的时候 ...
随机推荐
- Js 正则表达式知识测试
本文对javascript中正则表达式进行了总结汇总,将知识点和注意点都理了一下,并附上2个练习题,供大家参考学习. 正则表达式: 1.什么是RegExp?RegExp是正则表达式的缩写.RegExp ...
- 第二章:开始开发mod前你需要知道的一些事情
<基于1.8 Forge的Minecraft mod制作经验分享> 是的童鞋,别着急.不管我写的再认真也不可能面面俱到,那么如果遇到了什么问题怎么办呢?所以咱必须先掌握一些基础的东西,这样 ...
- linux性能调优概述
- 什么是性能调优?(what) - 为什么需要性能调优?(why) - 什么时候需要性能调优?(when) - 什么地方需要性能调优?(where) - 什么人来进行性能调优?(who) - 怎么样 ...
- linux中切换用户方式su和su -的区别
Using su The su command allows users to open a terminal window, and from that terminal start a sub ...
- 【转】iOS实时卡顿监控
转自http://www.tanhao.me/code/151113.html/ 在移动设备上开发软件,性能一直是我们最为关心的话题之一,我们作为程序员除了需要努力提高代码质量之外,及时发现和监控软件 ...
- java反射机制 struts2 获取 action、method、invocation、proxy
ActionInvocation invocation = ActionContext.getContext().getActionInvocation(); Object action = invo ...
- Win7上IIS发布网站系统\部署项目
1.系统已经安装IIS,如果没有安装,请到[控制面板]→[程序]→[程序和功能]→[打开或关闭Windows功能],选中Internet 信息服务Web管理工具下面的所有选项,确定:如下图 2.发布文 ...
- linux定时执行任务 转
转自:http://www.cnblogs.com/thinksasa/archive/2013/06/06/3121030.html linux定时执行任务 (1)Linux下如何定时执行php ...
- 前端/html5效果收藏
H5应用 9款漂亮的H5效果 8款漂亮的H5效果 36漂亮的button效果 颜色RGB表 省市二级联动
- js回网页顶部
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...