文件IO的简单示例

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值