Copying about text file

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyOboutFile {

	/**
	 * direction:If you need to copy a file from one to another,the following
	 * code can do it. There has some steps:
	 * (1)Creates File objects including both file you need to use.
	 * (2)Creates File according to the giving pathname.
	 * (3)Reading and writing and don't forget close the input and output stream.
	 * 
	 */
	public static void main(String[] args) throws IOException {
		String[] path = { "e:/a.in", "e:/a.out" };

		File in = new File(path[0]);
		File out = new File(path[1]);
		if (!in.exists()) {
			System.out.println("系统找不到需要拷贝的文件");
			return;
		}
		if (!out.exists()) {
			if (!out.getParentFile().exists()) {
				out.getParentFile().mkdirs();
				out.createNewFile();
			} else {
				out.createNewFile();
			}

		}
		
		

		BufferedReader br = new BufferedReader(new FileReader(in));
		BufferedWriter bf = new BufferedWriter(new FileWriter(out));
		int a;
		while ((a = br.read()) != -1) {

			bf.write(a);
		}
		
		//You can also use this way to copy,but this way may lead to add a newline character.
		
//		String s=null;
//		while((s = br.readLine()) != null){
//			bf.write(s);
//			bf.newLine();
//		}
		
		
		br.close();
		bf.close();

		System.out.println("数据已写入");
		
		

	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值