代码:

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. shell 判断文件、目录是否存在

    shell判断文件是否存在   1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. m ...

  2. 子查询in和表连接效率

    在数据查询时,尽量减少in子查询而使用表连接的方式进行,效率更高.

  3. Visual Studio 2012 Ultimate 上安装 Python 开发插件 PTVS

    1.我的环境 操作系统:32位 Win7 旗舰版 Service Pack 1 VS版本:Microsoft Visual Studio Ultimate 2012 版本 11.0.50727.1 R ...

  4. NYOJ 1091 超大01背包(折半枚举)

    这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...

  5. Eclipse+maven发布ee项目jar包未发布

    背景:在Eclipse中搭建好EE环境后,发布时,出现ClassNotFoundException: XX.XX.ConfigureListener,查看时,对应的jar包都引入了,项目没也没出错,但 ...

  6. nginx环境下搭建nagios 3.5.0,及配置pnp4nagios画图

    本文基于<LNMP最新源码安装脚本>,Nagios依赖PHP环境和perl环境,由于Nginx不支持Perl的CGI,需先来搭建Perl环境,Nagios原理介绍略.一.下载最新稳定源码包 ...

  7. Objective-C 笔记一(HelloWorld)

    作为一个果粉And程序员,奔着对OC浓厚的兴趣,开始学习IOS.并以后也想从事IOS开发工作.并将自己的学习记录下来,俗话说的好,不会总结的程序猿,不是好程序员! Xcode可以在AppStore里下 ...

  8. QT5-控件-QProgressBar-进度条-用来做下载进度,文件读取进度还不错

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QProgressBar> ...

  9. UVA11538Chess Queen(组合数学推公式)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题目意思:在n*m的棋盘中放置两个不同的皇后,使得两者能够相互攻击,共有多少种放置 ...

  10. Graphics2D 中文乱码

    今天遇到了一个乱码问题,合成的小票图片上的中文全部变成了口口口,后来在网上查了资料,发现是Graphics2D用了宋体字,而linux服务器上没有对应的字体库. 把本地的字体库上传上去就解决了. 本地 ...