代码:

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中文件操作的工具类的更多相关文章

  1. java中excel导入\导出工具类

    1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...

  2. java中定义一个CloneUtil 工具类

    其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...

  3. java中的Arrays这个工具类你真的会用吗

    Java源码系列三-工具类Arrays ​ 今天分享java的源码的第三弹,Arrays这个工具类的源码.因为近期在复习数据结构,了解到Arrays里面的排序算法和二分查找等的实现,收益匪浅,决定研读 ...

  4. Java 借助poi操作PDF工具类

    ​ 一直以来说写一个关于Java操作PDF的工具类,也没有时间去写,今天抽空写一个简单的工具类,拥有PDF中 换行,字体大小,字体设置,字体颜色,首行缩进,居中,居左,居右,增加新一页等功能,如果需要 ...

  5. java中文件操作《一》

    在日常的开发中我们经常会碰到对文件的操作,在java中对文件的操作都在java.io包下,这个包下的类有File.inputStream.outputStream.FileInputStream.Fi ...

  6. 在JAVA中自定义连接数据库的工具类

    为什么要自定义数据库连接的工具类: 在开发中,我们在对数据库进行操作时,必须要先获取数据库的连接,在上一篇随笔中提到的获取数据库连接的步骤为: 1.定义好4个参数并赋值 2.加载驱动类 3.获取数据库 ...

  7. Java中的集合Collections工具类(六)

    操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...

  8. java里poi操作Excel工具类【我改】

    参考原文: https://www.cnblogs.com/yizhang/p/7244917.html 我改: package test; import java.io.File; import j ...

  9. java中重要的多线程工具类

    前言 之前学多线程的时候没有学习线程的同步工具类(辅助类).ps:当时觉得暂时用不上,认为是挺高深的知识点就没去管了.. 在前几天,朋友发了一篇比较好的Semaphore文章过来,然后在浏览博客的时候 ...

随机推荐

  1. Android Configuration change引发的问题及解决方法

    之前在学习Fragment和总结Android异步操作的时候会在很多blog中看到对Configuration Change的讨论,以前做的项目都是固定竖屏的,所以对横竖屏切换以及横竖屏切换对程序有什 ...

  2. Ubuntu apt-get 错误 -11 -system error

    错误图片 上述错误是dns解析错误,不能解析域名了,所以也访问不了 解决办法,添加dns,命令如下 sudo vim /etc/resolv.conf 添加 nameserver (此外填写域名服务器 ...

  3. Collections.unmodifiableList()的使用与场景

    在<重构——改善既有代码的设计>一书中,有一种重构手法叫Encapsulate Collection(封装集群),为了演示该重构手法,我写了四个类,通过对比重构前后的代码,加深对这一重构手 ...

  4. nyoj 756 重建二叉树

    重建二叉树主要是给你一颗二叉树的前序遍历的结果和中序遍历的结果或者后序遍历的结果或者中序遍历的结果,让你求出其中的后序遍历的结果或者前序遍历的结果,这里知道其中的两个就能求出第三个,但是知道的两个必须 ...

  5. NYOJ 214 最长上升子序列nlogn

    普通的思路是O(n2)的复杂度,这个题的数据量太大,超时,这时候就得用nlogn的复杂度的算法来做,这个算法的主要思想是只保存有效的序列,即最大递增子序列,然后最后得到数组的长度就是最大子序列.比如序 ...

  6. CRC32校验的用法

    CRC32校验数据的完整性 这里的数据包括字符串.文件,还有哪些? 文件校验相当于下载大型软件,有md5加密结果.这里的用途是什么?

  7. MS SQL Sever数据库还原

    一.右键 数据库 二.点击 [还原文件和文件组(E)...],弹出下图的窗口界面 1.在 目标数据库 的输入框填写你的数据库名(注意这是新建一个数据库供还原使用,不能还原到已有的数据库) 三.点击[源 ...

  8. 如何查看linux系统下的各种日志文件 linux 系统日志的分析大全

    日志分类: 1. 连接时间的日志 连接时间日志一般由/var/log/wtmp和/var/run/utmp这两个文件记录,不过这 两个文件无法直接cat查看,并且该文件由系统自动更新,可以通过如下: ...

  9. eclipse - copy类的全名

    由于多次操作,感觉eclipse应该提供这个功能,网上搜一下,发现需要安装插件. 下载地址为 http://www.jave.de/eclipse/copyfully/copyfully_1.2.0. ...

  10. Centos 5.5下安装samba

    1.安装: Centos 5.5下安装samba,具体步骤如下: [root@bogon ~]# rpm -q samba Package samba is not installed [root@b ...