将硬盘中两个TXT文件读出来,并写入另一个文件

package com.yonge.day07;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class ReadWriterTXT {
	//读出硬盘中的txt文件的数据。
	public static String read(String path1,String path2) {
		String encoding = "GBK";
		String lineTxt2 = null;
		String lineTxt1 = null;
		File f1 = new File(path1);
		File f2 = new File(path2);
		//isFile()测试此抽象路径名表示的文件是否是一个标准文件。
		//exists() 测试此抽象路径名表示的文件或目录是否存在。
		if(f1.isFile()&&f1.exists()){

			try {
				InputStreamReader read = new InputStreamReader(new FileInputStream(f1),encoding);
				BufferedReader br = new BufferedReader(read);
				lineTxt1 = null;
				lineTxt1 = br.readLine();
				
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		if(f2.isFile()&&f2.exists()){
			
			try {
				InputStreamReader read = new InputStreamReader(new FileInputStream(f2),encoding);
				BufferedReader br = new BufferedReader(read);
				lineTxt2 = null;
				lineTxt2 = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return lineTxt1+lineTxt2;
	}
	//往硬盘中的txt文件写入数据。
	public static void writer(String path,String txt){
		File file = new File(path);
		FileWriter writer;
		try {
			writer = new FileWriter(file, true);
			writer.write(txt);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {

		String path = "f:/test/dd/ss.txt";
		String path1 = "f:/test/dd/ee.txt";
		String path2 = "f:/test/dd/dd.txt";
		String txt=read(path1,path2);
		System.out.println(txt);
		writer(path,txt);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值