问题:如果一个文本中有如下内容
a b c d .....e.....
a1 b1 c1 d1 .....e1.....
a2 b2 c2 d2 .....e2.....
a3 b3 c3 d3 .....e3.....
a4 b4 c4 d4 .....e4.....
a5 b5 c5 d5 .....e5.....
a6 b6 c6 d6 .....e6.....
a7 b7 c7 d7 .....e7.....
a8 b8 c8 d8 .....e8.....
a9 b9 c9 d9 .....e9.....
要求给你a,d两列,如何把文本中对应的a,d两列输入到另一个文本中,变成如下所示?
a d
a1,d1
a2,d2
a3,d3
a4,d4
a5,d5
a6,d6
a7,d7
a8,d8
a9,d9
第一种方法:先查找并记录要求列在文本中对应的下标,再输出文本对应下标列:
public static void main(String[] args) throws Exception {
File file = new File("E:\\WorkSpace\\Test\\TxtReaderTest1.txt");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:\\WorkSpace\\Test\\TxtReaderTest1W.txt")),"UTF-8"));
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));//将文件转成字节流
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream),5*1024*1024);//将转好的字节流转成字符流
BufferedReader systemRead = new BufferedReader(new InputStreamReader(System.in));//用户输入数据流
try {
String line = null;//原文本中的每一行
List strs1 = new ArrayList();//用户输入的要读的列标签
int num[] = null;//每个列标签的位置
/**
* 第一步:记录下要读的标签,以及它的下标位置
*/
if((line = reader.readLine()) != null) {//读取第一列
String[] infoArraay = line.split(" ");//根据空格分割第一列进数组
System.out.println("请从以下数组任选几个列,最多输入"+infoArraay.length+"个");
System.out.println(Arrays.toString(infoArraay));
for (int i = 0;i < infoArraay.length;i++) {
System.out.println("请输入,输入好列后请输入OK:");
String input = systemRead.readLine();
if ("OK".equals(input)) {
break;
}
if (Arrays.asList(infoArraay).contains(input)) {
strs1.add(input);
continue;
}
System.out.println("不包含");
}
num = new int[strs1.size()];//每个标签的位置
//看要求读的标签各在哪个下标,记录下来
for (int i = 0; i < strs1.size(); i++) {//用户输入的数组组
for (int j = 0; j < infoArraay.length; j++) {//需要对照的文本的第一列首
if(infoArraay[j].equals(strs1.get(i))) {
num[i] = j;
writer.write(infoArraay[j]+" ");
break;
}
}
}
writer.newLine();//换行
}
/**
* 第二步:通过已记录的下标位置,逐行获取对应的列并拼好,放入writer对象
*/
while((line = reader.readLine()) != null) {//逐行的读
//将读取到的行进行分割进数组
String[] infoArraay = line.split(" ");
String str = "";
//根据已记录的下标获取数组里的值,进行拼接字符串
for (int i = 0; i < num.length; i++) {
str += infoArraay[num[i]]+",";
}
str = str.substring(0, str.length()-1);//保持完整性,去掉符号“,”
writer.write(str);//写入一行
writer.newLine();//换行
}
} catch (Exception e) {
System.out.println("毁灭吧,赶紧的");
e.printStackTrace();
} finally {
systemRead.close();
reader.close();
writer.close();
}
}
第二种方法:通过正则表达式匹配:
public static void main(String[] args) throws IOException {
File fileR = new File("E:\\WorkSpace\\Test\\PatternMatcherReadTest.txt");
File fileW = new File("E:\\WorkSpace\\Test\\PatternMatcherReadTestW.txt");
BufferedReader br = new BufferedReader(new FileReader(fileR));
BufferedWriter bw = new BufferedWriter(new FileWriter(fileW ));
try {
String s;
String[] ss = {"a","d"};
Pattern p =null;
while( (s = br.readLine() ) != null){
System.out.println(s);
String str="";
for (int i = 0 ; i<ss.length;i++){
p = Pattern.compile(ss[i]+".");
Matcher m = p.matcher(s);
if (m.find() ){
str += m.group(0)+",";
}
}
str = str.substring(0, str.length()-1);//末尾去掉符号“,” 保持每一行输出的完整性
bw.write(str);
bw.newLine();//换行
bw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
br.close();
bw.close();
fileR.exists();
fileW.exists();
}
}