日期:2014-05-20 浏览次数:20746 次
A的内容: 这 是 一 个 B文件的内容: java 练习
import java.io.*; public class Combine { public static void main(String[] args) { String[] name = new String[2]; String nameC = null; System.out.println("About this program:这个程序可以将您输入的两个文件A与B合并成C"); System.out.println("请输入您想合并两个文件名(加上后缀),每个文件名以回车确认:"); BufferedReader getInput = new BufferedReader(new InputStreamReader( System.in)); BufferedReader bufferedR = null; BufferedWriter bufferedW = null; File[] file = new File[2]; try { for (int i = 0; i < 2; i++) { name[i] = getInput.readLine(); file[i] = new File(name[i]); } nameC = name[0].substring(0, name[1].indexOf(".")) + name[1]; bufferedW = new BufferedWriter(new FileWriter( new File(nameC))); String line; for (int i = 0; i < 2; i++) { bufferedR = new BufferedReader(new FileReader(file[i])); while ((line = bufferedR.readLine()) != null) { bufferedW.write(line + "\n"); System.out.println("next"); getInput.read(); } } System.out.println("合并成功!"); } catch (IOException e) { e.printStackTrace(); } finally { try { getInput.close(); bufferedR.close(); bufferedW.close(); } catch (Exception e) { System.out.println("Wrong when close the stream."); } } } }