代码:

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. cocos2d 高仿doodle jump 无源代码

    1. 游戏视频 主角眼熟吗?没错,上次跑酷游戏中的"30"来Jump了,有三种道具.主角光环,竹蜻蜓.翅膀: 有两种怪物,螃蟹和鸟: 有5种板子.点击屏幕,30会把它的嘴巴3给发射 ...

  2. hdu1074 Doing Homework(状态压缩DP Y=Y)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  3. 队列(链式存储)JAVA代码

      publicclassLinkQueue<T>{       //结点类     publicclassNode{         public T data;         pub ...

  4. Ubuntu14.04 如何修改/etc/sudoers 和错误修改权限的解决办法

    开始学习hadoop啦!!! 在Ubuntu14.04上新建了一个名为hadoop的用户,但总是遇到各种权限问题,于是就想干脆把这个账户变成root账户. 网上查到说是直接修改/etc/sudoers ...

  5. Python基础类型

    1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 names = ['Alex',"Tenglan",'Eric ...

  6. 自制Javascript分页插件,支持AJAX加载和URL带参跳转两种初始化方式,可用于同一页面的多个分页和不同页面的调用

    闲话部分 最近闲着实在无聊,就做了点小东西练练手,由于原来一直在用AspNetPager进行分页,而且也进行了深度的定制与原有系统整合的也不错,不过毕竟是用别人的,想着看自己能试着做出来不能,后台的分 ...

  7. phpmyadmin登陆提示#2002 无法登录 MySQL 服务器和设置自增

    看看mysql启动没有,结果是mysql服务没有启动,找了半天,是这个原因,那就右键计算机->管理->服务->启动mysql服务 设置自增:在显示出来的一行字段定义中把浏览器的滚动条 ...

  8. 读取XML文件的几种方式的效率分析

    第一种:使用XmlReader来读取. Stopwatch sw = Stopwatch.StartNew(); List<Dictionary<string, string>> ...

  9. 告诉你GetDC()没有释放造成的后果

    最近做的项目中需要显示视频监控窗口,从采集卡中读到图像的数据,需要实时显示出来,而且速度比较快. 由于比较简单,就直接使用了GDI画图,以前复杂的都用openGL啥的工具了,这次这个简单,就直接用GD ...

  10. SQL中删除同一字段中重复的值

    /////////////////////目地:ZDJZ_DIS中 name字段有重复的值,删除重复的值 DELETE * FROM ZDJZ_DIS WHERE NAME IN (select NA ...