• 编写一个程序,将 a.txt 文件中的单词与 b.txt 文件中的单词交替合并到 c.txt 文件中,a.txt 文件中的单词用回车符分隔,b.txt 文件中用回车或空格进行分隔。


    package IO;
    
    import java.io.*;
    
    public class test {
    	
    	public  void connectWords(File file1, File file2, File file3)throws IOException
    	{
    		String[] str1 = split(file1, "
    ");
    		String[] str2 = split(file2, "
    "+"|"+" ");
    		try(FileWriter fw = new FileWriter(file3))
    		{	
    			int index = 0;
    			while(index != str1.length||index != str2.length)
    			{
    				if(index < str1.length)
    					fw.write(str1[index]);
    				if(index < str2.length)
    					fw.write(str2[index]);
    				index ++;
    			}
    		}
    	}
    	
    	public String[] split(File f, String regex)throws IOException
    	{
    		try(FileReader fr = new FileReader(f))
    		{
    			char[] cbuf = new char[(int)f.length()];
    			int hasRead = fr.read(cbuf);
    			String str = new String(cbuf, 0, hasRead);
    			String[] strArr = str.split(regex);
    			return strArr;
    		}
    	}
    	public static void main(String[] args) throws IOException
    	{
    		File f1 = new File("./a.txt");
    		File f2 = new File("./b.txt");
    		File f3 = new File("./c.txt");
    		test t = new test();
    		t.connectWords(f1, f2, f3);
    	}
    	
    }
    
  • 相关阅读:
    excel查找定位操作(for lutai)
    sqlserver检查sql执行时间
    excel操作for(lutai)
    android studio 3.0+发布签名apk注意的情况
    二维码
    svn文件合并
    生成wsdl代理c#代码
    LOCK_TIMEOUT
    支持chrome30下载文件
    miniui处理多重子表级联,一次性提交多表数据的ui要点
  • 原文地址:https://www.cnblogs.com/masterlibin/p/5646798.html
Copyright © 2020-2023  润新知