之前都是用python处理实验数据,现在用下java,纯当复习下IO的知识。输入文件为列数为2的CSV文件,如下:
有点杂乱,代码如下:
package com.edu.njupt.www;
import java.io.*;
import java.util.Arrays;
/**
* @author Bangwen Chen
*
* 2013-8-25
*/
public class Simply_data_v1 {
public static void main(String [] args) throws IOException{
File f = null;
System.out.println("please input a path");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String path = br.readLine();
f = new File(path);
File [] list = f.listFiles();//用list遍历path下的文件
br.close();
String str = null;
int num=0;
while(num<list.length){//遍历path下的文件
BufferedReader bfr = new BufferedReader(new FileReader(list[num]));
// System.out.println(list[num].getName());//取得文件名
String[] tmp = list[num].getName().split("-");
String out_filename =tmp[0]+"-2"+".csv";//构造新的文件名 1-1,2-1...都变为1-2,2-2
BufferedWriter bfw = new BufferedWriter(new FileWriter(path+"\\"+out_filename));//输出路径
int j=0;
while((str=bfr.readLine())!=null){
if(j==0){//文件的第一行都是固定的,所以直接写
String firstline ="module,"+tmp[0]+"\n";
bfw.write(firstline);
j++;
}else{//对第一行之后的所有行进行处理
String [] content = str.split(";");
// System.out.println(Arrays.toString(content));
String [] location =content[0].split("\\.");//用点对字符串进行分隔需要转义,加俩反斜杠
// System.out.println(Arrays.toString(location));
String data = location[1]+","+content[1]+"\n";
bfw.write(data);
}
}
//System.out.println(out_filename);
//System.out.println(list[num].getParent());
//System.out.println(list[num].getPath());
bfw.flush();
bfr.close();
bfw.close();
num++;
}
System.out.println("It has done,A Biang");
}
}