有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来。

本文介绍了一个Java程序示例,该程序通过读取两个文件实现特定字符串的查找和替换功能。首先从配置文件中加载键值对,接着在内容文件中搜索并替换带有特定格式的占位符。
package com.cn.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class IOTest {
	Map<String, String[]> words = new HashMap<String, String[]>();

	public void getFile1(String filename1) {
		FileInputStream in1 = null;
		StringBuffer sb1 = null;
		try {
			in1 = new FileInputStream(filename1);
			byte[] buf = new byte[1024];
			sb1 = new StringBuffer();

			int hasRead = 0;
			while ((hasRead = in1.read(buf)) > 0) {
				sb1.append(new String(buf, 0, hasRead));
			}

			String string1 = sb1.toString();
			String[] ss = string1.split(" ");

			for (int i = 0; i < ss.length; i++) {

				String[] infos = ss[i].split("=");

				if (infos.length > 0) {

					if (infos[1].indexOf(":") > 0 || infos[1].indexOf(":") > 0) {
						String[] vals = infos[1].split(":|:");
						words.put(infos[0], vals);
					} else {
						words.put(infos[0], new String[] { infos[1] });
					}

				}
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @param filename0
	 * @param filename1
	 */
	public String getInfo(String filename0, String filename1) {
		getFile1(filename1);
		printMap();
		StringBuffer strings = new StringBuffer();
		
		FileInputStream in0 = null;
		try {
			in0 = new FileInputStream(filename0);	
			byte[] buf = new byte[1024];
			// 用于保存实际读取的字节数
			int hasRead = 0;
			while ((hasRead = in0.read(buf)) > 0) {
				strings.append(new String(buf, 0, hasRead));
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				in0.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		String string = strings.toString();
		System.out.println(string);
		StringBuffer sb = new StringBuffer();
		while (string.length() > 0 && string.contains("$")) {
			int ch = string.indexOf('$');
			int ch1 = string.indexOf('(');
			if (ch1 == ch + 1) {
				sb.append(string.substring(0, ch1 - 1));
				string = string.substring(ch1 + 1);

				int ch2 = string.indexOf(')');
				int point = string.indexOf('.');

				if (point > 0 && point < ch2) {
					String key = string.substring(0, point);

					String[] vals = words.get(key);
					
					String num = string.substring(point + 1, ch2);

					int number = Integer.parseInt(num) - 1;
					String value = null;
					if (vals != null && vals.length > 0) {
						value = vals[number];
					}
					sb.append(value);
					string = string.substring(ch2 + 1);
				} else {
					String key = string.substring(0, ch2);

					String[] vals = words.get(key);
					String value = null;
					if (vals != null && vals.length > 0) {
						value = vals[0];
					}
					sb.append(value);
					string = string.substring(ch2 + 1);
				}
			}
		}
		sb.append(string);
		
		return sb.toString();
	}

	public void printMap() {
		Set<Entry<String, String[]>> set = words.entrySet();
		Iterator<Entry<String, String[]>> iter = set.iterator();
		while (iter.hasNext()) {
			Entry<String, String[]> entry = iter.next();
			String key = entry.getKey();
			String[] value = entry.getValue();
			for (int i = 0; i < value.length; i++) {
				System.out.println(key + "," + value[i]);
			}

		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IOTest test = new IOTest();
		System.out.println(test.getInfo("E://content.txt", "E://word.conf"));

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值