文件IO的简单示例
来源:互联网 发布:读报软件下载 编辑:程序博客网 时间:2024/04/30 23:23
一、文本文件IO:
1. 本文文件IO的工具类:
package com.huey.io;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;/** * 文本文件读写的工具类 * @version 2013-08-03 * @author huey2672 * */public class TextFileIOUitl {/** * 向文件写文本 * @param text要写入文件的文本 * @param filePath要写入的文件路径 */public void writeTextToFile(String text, String filePath) {writeTextToFile(text, filePath, false);}/** * 向文件写文本 * @param text要写入文件的文本 * @param filePath要写入的文件路径 * @param append是否追加 */public void writeTextToFile(String text, String filePath, boolean append) {try {FileWriter fw = new FileWriter(filePath, append);PrintWriter pw = new PrintWriter(fw);pw.print(text);pw.close();fw.close();} catch (IOException e) {e.printStackTrace();} }/** * 从文件中读取文本 * @param filePath要读取文本所在的文件的路径 * @return读取出的文本,如果文件不存在,返回null */public String readTextFromFile(String filePath) {StringBuilder sb = new StringBuilder();// 如果文件不存在,返回nullFile file = new File(filePath);if (!file.isFile()) {return null;}try {FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);String line = null;while ((line = br.readLine()) != null) {sb.append(line + "\r\n");}br.close();fr.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return sb.toString();}}
2. 本文文件IO的工具类的测试用例:
package com.huey.test;import org.junit.Test;import com.huey.io.TextFileIOUitl;/** * 文本文件读写测试用例 * @version 2013-08-03 * @author huey2672 * */public class TextFileIOTest {/** * 测试向文件写文本 * @throws Exception */@Testpublic void testWriteTextToFile() throws Exception {String filePath = "hello.txt";TextFileIOUitl writerUitl = new TextFileIOUitl();writerUitl.writeTextToFile("Hello, Sugar!\r\n", filePath);writerUitl.writeTextToFile("Long Time No See!\r\n", filePath, true);}/** * 测试从文件读文本 * @throws Exception */@Testpublic void testReadTextFromFile() throws Exception {String filePath = "hello.txt";TextFileIOUitl readerUitl = new TextFileIOUitl();String text = readerUitl.readTextFromFile(filePath);System.out.println(text);}}
二、二进制文件IO,以文件拷贝为例:
1. 文件拷贝工具类:
package com.huey.io;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;/** * 文件的拷贝 * @version 2013-08-03 * @author huey2672 * */public class FileCopying {/** * 拷贝文件,将路径为srcPath的文件拷贝至路径为dstPath的目录下 * @param srcPath * @param dstPath * @returntrue表示拷贝成功,false表示拷贝失败 */public boolean copyFile(String srcPath, String dstPath) {// 如果路径srcPath对应的不是以个文件,则返回falseFile srcFile = new File(srcPath);if (!srcFile.isFile()) {System.out.println(srcPath + " is not a file!");return false;}// 如果路径dstPath对应的是一个文件,则返回false// 如果路径dstPath没有对应的文件或目录,则创建一个新的目录File dstDir = new File(dstPath);if (dstDir.isFile()) {System.out.println(srcPath + " is a file!");return false;} else if (!dstDir.exists()) {dstDir.mkdir();}// 创建目标文件File dstFile = new File(dstDir, srcFile.getName());try {// 获取输入流FileInputStream fis = new FileInputStream(srcFile);BufferedInputStream bis = new BufferedInputStream(fis);// 获取输出流FileOutputStream fos = new FileOutputStream(dstFile);BufferedOutputStream bos = new BufferedOutputStream(fos);// 从源文件中读取数据保存至buffer中,在把buffer中的数据写入目标文件中byte[] buffer = new byte[1024];int readSize = 0;while ((readSize = (bis.read(buffer))) > 0) {bos.write(buffer, 0, readSize);}// 关闭流bis.close();fis.close();bos.close();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return true;}}
2. 文件拷贝工具类的测试用例:
package com.huey.test;import org.junit.Test;import com.huey.io.FileCopying;/** * 文件拷贝的测试用例 * @version 2013-08-03 * @author huey2672 * */public class CopyingTest {/** * 测试文件的拷贝 * @throws Exception */@Testpublic void testCopyFile() throws Exception {String srcPath = "D:\\sugar.jpg";String dstPath = "D:\\TmpDir";FileCopying fileCopying = new FileCopying();if (fileCopying.copyFile(srcPath, dstPath)) {System.out.println("success");;} else {System.out.println("fail");}}}
- 文件IO的简单示例
- JAVA IO流的简单示例
- IO:使用FileInputStream的简单示例
- IO:使用FileInputStream的简单示例2
- java io 对文件操作的示例
- java io 对文件操作的示例
- 简单的makefile文件示例
- 简单的文件加密示例
- 简单的文件上传示例
- 简单的Log文件示例
- 简单的IO操作示例(不带缓存方式)
- 生成简单的Excel文件的示例
- IO读写文件/目录 的简单例子
- 简单说说C++的文件IO
- JAVA IO文件操作示例
- 最简单的html文件上传示例
- JavaScript读取XML文件的简单示例
- Struts2文件上传的简单示例
- 《30天自制操作系统》学习笔记_helloos0
- WIX: 用XmlConfig修改ApplicationHost.config出错
- 新浪微博密谋上市 估值缺好故事
- poj2966(有问题)
- C#委托的介绍(delegate、Action、Func、predicate)
- 文件IO的简单示例
- 在Win32应用程序中使用MFC类库(转)
- 函数setlocale()的介绍与单字符宽字符的互相转换
- HDU Coins 【多重背包问题】
- 【3D游戏引擎系列】一、渲染流程和坐标转换
- 从裸系统编译android源码
- Iscroll 4 同一页面中,能够滚动上下并且左右同存效果,(滑动时很飘)解决办法(2)
- IPAD上访问WINDOWS环境远程桌面---三款软件比较
- 数据结构之单向循环链表