合并两个文件的内容

来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 18:35 点击:
package lianxi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Test2 {
 public static void main(String[] args) { 
 
  File a = new File("E:/", "a.txt");
     File b = new File("E:/" ,"b.txt");
     File c = new File("E:/", "c.txt");
 
   try{
    
      if(a==null){
           a.createNewFile();
      }
     
         if(b==null){
           b.createNewFile();
         }
      
        if(c==null){
          c.createNewFile();
        }
     
     }
    catch (Exception e) {
         e.printStackTrace();
    }
   
       copy(c, b, a);
 }
 /**
  * 将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中 
  */
 
 public static void copy(File c,File a,File b){
 
         //读取a文件中的单词
     String astr = getMsg(a);
 
        //读取b文件中的单词
     String bstr = getMsg(b);
 
       //把a文件中的单词放在数组aw中
     String aw[] = getWord(astr);
   
   
      //把b文件中的单词放在数组bw中
     String bw[] = getWord(bstr);
 
     int total = aw.length+bw.length;
         //构建新的数组t,长度为aw和bw数组长度之和
     String t[] = new String[total];
 
      t = getNewString(t, aw, bw);
 
          copy(c, t);
         
 }
 /**
  * 读取文件中的内容 
  */
 public static String getMsg(File file){
 
  FileReader reader=null;
     String results = "";
    
     try {
           reader = new FileReader(file);
           char[] buf = new char[(int)file.length()];
           int len = reader.read(buf);
           results = new String(buf,0,len);
      }
     catch (Exception e) {
           e.printStackTrace();
     }
     finally{
     
       try {
        
          if(reader!=null){
               reader.close();
          }
       
 &nb

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Java编程语言的八大优点
      • JVM对象生命周期详细介绍
      • Java平台上的CRM系统
      • Java 算数测试小程序
      • Command(命令模式)
      • Java 一个简单的画图程序
      • Java环境 使用Resin在NT环境下配置JSP环境
      • Java 日历的小程序
      • Java 统计代码的小工具
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1