* 读文件,返回字符串
*
* @param path
* @return
*/
public static List<String> ReadFile(String path){
List<String> dataList = new ArrayList<String>();
File file = new File(path);
BufferedReader reader = null;
try {
//以行为单位读取文件内容,一次读一整行;
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
//显示行号
// System.out.println("line " +line+ ": " +tempString);
dataList.add(tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return dataList;
}
/**
* 数据转换成JSON文件
*
* @param path
* @param array
*/
public static void writeJsonFile(String path, JSONArray array) {
File file = new File(path);
FileOutputStream fout = null;
try {
fout = new FileOutputStream(file);
for (int j = 0; j < array.size(); j++) {
fout.write(array.getJSONObject(j).toString().getBytes());
fout.write("\n".getBytes());
}
fout.close();
} catch (Exception e1) {
e1.printStackTrace();
}finally{
if(fout!=null){
try {
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void MadeOzoneDataToJsonFile(String OzoneDataFilePath ,String jsonListPath ){
//j.add(1);
List<String> list = ReadFile(OzoneDataFilePath);
JSONArray jsonList = new JSONArray();
for(String str:list){
// String a = str;
String concentration="", estate="", date="", time="";
String arg[] = str.split("\\s+");
concentration=arg[0];
date=arg[2];
estate=arg[1];
time=arg[3];
// System.out.println(arg[0]);
JSONObject jo = new JSONObject();
jo.put("concentration", concentration);
jo.put("date", date);
jo.put("time", time);
jo.put("estate", estate);
jsonList.add(jo);
//System.out.println(str);
}
writeJsonFile(jsonListPath,jsonList);
}