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"));
}
}