zoukankan      html  css  js  c++  java
  • 算法与编程--1

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

    package com.test;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Arrays;
    
    public class TestCopyChar {
    	
    	public static void main(String[] args) {
    		testCopy();
    	}
    	/**
    	 * 编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。
    	 */
    	public static void testCopy() {
    		
    		File file = new File("./demo/a.txt");
    		FileReader fr = null;
    		try {
    			fr = new FileReader(file);
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		File file1 = new File("./demo/b.txt");
    		FileReader fr1 = null;
    		try {
    			fr1 = new FileReader(file1);
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		FileWriter fw = null;
    		try {
    			fw = new FileWriter("./demo/c.txt");
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		char[] buf = new char[(int) file.length()];
    		char[] bufb = new char[(int) file1.length()];
    		int len = 0;
    		int len1 = 0;
    		try {
    			len = fr.read(buf);
    			len1 = fr1.read(bufb);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		String aStr = new String(buf,0,len);
    		String bStr = new String(bufb,0,len1);
    		System.out.println(aStr);
    		System.out.println(bStr);
    		System.out.println("***********");
    		String[] aArr = aStr.split("[
    ]");
    		String[] bArr = bStr.split("[ 
    ]");
    		System.out.println(Arrays.toString(aArr));
    		System.out.println(Arrays.toString(bArr));
    		if (aArr.length > bArr.length) {
    			for (int i = 0; i < aArr.length; i++) {
    				try {
    					
    					fw.write(aArr[i] + "
    ");
    					if ((i < bArr.length) && (null != bArr[i])) {
    						fw.write(bArr[i] + "
    ");
    					}
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}else {
    			for (int i = 0; i < bArr.length; i++) {
    				try {
    					fw.write(bArr[i] + "
    ");
    					if ((i < aArr.length) && (null != aArr[i])) {
    						fw.write(aArr[i] + "
    ");
    					}
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    		try {
    			fr.close();
    			fr1.close();
    			fw.close();//fw.flush(); close()有flush()的作用,将输出流中的字符写入到文件中
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	
    }
    

      

  • 相关阅读:
    【javascript】ajax 基础
    【javascript】菜单滚动至顶部后固定
    【jquery】返回顶部效果
    【css】浅谈 inlineblock
    【jquery】checkbox——类似邮箱全选功能
    【javascript】返回顶部效果
    wcf基础知识之 查看soap消息 TcpTrace
    wcf系列之服务契约ServiceContract 之操作重载
    wcf 基础知识 之 消息交换模式 response/reply oneway deplex
    wcf基础知识之端口共享 portSharing
  • 原文地址:https://www.cnblogs.com/hblthink/p/8343460.html
Copyright © 2011-2022 走看看