博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔...
阅读量:7244 次
发布时间:2019-06-29

本文共 2952 字,大约阅读时间需要 9 分钟。

hot3.png

这种方法好点:

package cglib;

import java.io.File;

import java.io.FileReader;
import java.io.FileWriter;
public class StringNumber {
    
    public static void main(String args[]) throws Exception{
       
        FileManager a=new FileManager("D:\\文件\\QC处理\\2016年11月\\a.txt",new char[]{'\n'});
        FileManager b=new FileManager("D:\\文件\\QC处理\\2016年11月\\b.txt",new char[]{' ','\n'});
        FileWriter c=new FileWriter("D:\\文件\\QC处理\\2016年11月\\c.txt");
        String aWord = null;  
        String bWord = null;  
        while ((aWord = a.nextWord()) != null) {  
            c.write(aWord);  
            bWord = b.nextWord();  
            if (bWord != null) {  
                c.write(bWord);  
            }  
        }  
        if (bWord != null) {  
            c.write(bWord);  
        }  
        c.close();  
        System.out.println("finish");  
        }
    }

class FileManager{  

 
    String[] words = null;  
    int pos = 0;  
    @SuppressWarnings("resource")
    public FileManager(String fileName, char spilt[]) throws Exception {
        File file = new File(fileName);  
        FileReader fr = new FileReader(file);  
        char buf[] = new char[(int) file.length()];  
        int len = fr.read(buf);  
        String bufString = new String(buf, 0, len);  
        StringBuffer temp = new StringBuffer("");  
        temp.append(spilt[0]);  
        if (spilt.length > 1) {  
            int posl = 2;  
            while (posl <= spilt.length) {  
                temp.append("|");  
                temp.append(spilt[posl - 1]);  
                posl++;  
            }  
        }  
        String bs = temp.toString();  
        words = bufString.split(bs);  
    }  
 
    public String nextWord() {  
        if (pos == words.length) {  
            return null;  
        } else {  
            return words[pos++];  
        }  
}
}

 

 

 

第二种:

 

package cglib;

import java.io.File;

import java.io.FileReader;
import java.io.FileWriter;
public class StringNumber {
    
    public static void main(String args[]) throws Exception{
        try{
        FileManager a=new FileManager("D:\\文件\\QC处理\\2016年11月\\a.txt",new char[]{'\n'});
        FileManager b=new FileManager("D:\\文件\\QC处理\\2016年11月\\b.txt",new char[]{' ','\n'});
        FileWriter c=new FileWriter("D:\\文件\\QC处理\\2016年11月\\c.txt");
        String aWord= null;  
        String bWord= null;  
         //读取一个aWord,调用c写入,读取一个bWord,调用 c写入  
        while((aWord= a.nextWord()) !=null ){
            System.out.println("aWord="+aWord);
               c.write(aWord+ "\n");  
               bWord= b.nextWord();
               System.out.println("bWord="+bWord);
               if(bWord!= null){  
                   c.write(bWord+ "\n");
                   
               }
        }  
      //还得考虑a.txt内容读取完,b.txt还有内容没弄完
        while((bWord= b.nextWord()) != null){  
               c.write(bWord+ "\n");  
        }     
         
        c.close();
        System.out.println("finish");
        }catch(Exception e){
            e.printStackTrace();
        }
    
        }
    }
    
class FileManager{
     String[] words =null;  
     int pos = 0;  
     //把文件转换成String类型,然后分割成String[]  
     @SuppressWarnings("resource")
    public FileManager(String filename,char[] seperators) throws Exception{  
              
            File f = new File(filename);  
            FileReader reader = new FileReader(f);  
            //声明一个char数组缓冲区  
            char[] buf =new char[(int)f.length()]; //char占用两个字节  
            //调用reader读取,放入char数组中  
            int len =reader.read(buf);  
            String results = new String(buf,0,len);  
            //声明一个regex表达式null,然后进行赋值  
            String regex= null;  
            if(seperators.length>1 ){  
                   regex= "" + seperators[0] + "|" + seperators[1];  
            }else{  
                   regex= "" + seperators[0];  
            }  
            words =results.split(regex);  
     }  
      
     public String nextWord(){  
            if(pos ==words.length)  
                   return null;  
            return words[pos++];  
     }  
 
}
    
   

 

              

            
    

 

    

 

 

转载于:https://my.oschina.net/u/2822116/blog/781798

你可能感兴趣的文章
webpack和webpack-simple区别(如何引入css文件)
查看>>
进程间通信:共享内存2
查看>>
C# WinForm中NotifyICon控件的用法
查看>>
数论 + 公式 - HDU 4335 What is N?
查看>>
内核Linux系统调用的列表zz
查看>>
Android 蓝牙通信——AndroidBluetoothManager
查看>>
pyqt的setObjectName()/findChild()
查看>>
linux中iptables详解
查看>>
数据产品设计专题(2)- 数据产品设计方法论之框架体系
查看>>
搭建nagios+ncpa监控
查看>>
iReport4.5.1、Struts2.2.3生成Html文档时页面红叉叉的解决办法。
查看>>
计算机字符串的长度
查看>>
Mybatis最入门---动态查询(foreach)
查看>>
Exchange 2007 配置POP3
查看>>
vmware 中centos NAT(网络地址转换)安装与使用。
查看>>
产品设计体会(7018)人人都是产品经理
查看>>
Linux学习 --&gt;解决Ubuntu系统上 No command &#39;crond&#39; found
查看>>
如何使core dump生效之unlimit及gdb相关命令(C++)
查看>>
运维部奖励员工之大连游记
查看>>
基于OHCI的USB主机开发 —— OHCI(控制端口写数据)
查看>>