import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JSONObject;
public class FileTool {
static String infile = "d:/testJson.data";
static String outfile = "d:/testJson.data";
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getSysTime()+" Test start");
//testWriteJsonToRowFile();
testReadJsonToRowFile();
System.out.println(getSysTime()+" Test end");
}
public static String getSysTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
String time = sdf.format(new Date());
return time;
}
public static void testReadJsonToRowFile(){
try{
BufferedReader in = new BufferedReader(
new FileReader(infile),10000);//10000不是指行数,是指缓冲区的大小
String s ;
while ((s = in.readLine()) != null){
//对s进行处理,我这里是把它转换成Json对象
JSONObject jsonObj=JSONObject.fromObject(s);//将字符串转化为json对象
//写数据库处理
ObjBean bean=(ObjBean)JSONObject.toBean(jsonObj,ObjBean.class);
System.out.println(bean.getName());
}
in.close();
}
catch(Exception e){
}
}
public static void testWriteJsonToRowFile(){
try{
BufferedWriter bw=new BufferedWriter(new FileWriter(outfile),10000);//同上
for(int i=0;i<1000;i++)
{
ObjBean bean=new ObjBean();
// JSONObject jsonObj=new JSONObject();
// for(int j=0;j<10;j++)
// {
// jsonObj.put("REAL_RESULT_"+j, "测试好不好用");
// }
JSONObject jsonObj=JSONObject.fromObject(bean);
bw.write(jsonObj.toString()+"\n");
}
bw.flush();
bw.close();
}
catch(Exception e){
}
}
}
- 1
- 2
前往页