一、文本文件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();
// 如果文件不存在,返回null
File 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
*/
@Test
public 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
*/
@Test
public 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
* @return true表示拷贝成功,false表示拷贝失败
*/
public boolean copyFile(String srcPath, String dstPath) {
// 如果路径srcPath对应的不是以个文件,则返回false
File 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
*/
@Test
public 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");
}
}
}