文件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");}}}


原创粉丝点击