public static void wirteToJson(String fileUrl,String ss)
{
try {
FileOutputStream fos=new FileOutputStream(new File(fileUrl));
byte [] buf=new byte[1024];
int len=0;
try {
fos.write(ss.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String readJson(String fileUrl)
{
StringBuilder strs=new StringBuilder();
File file=new File(fileUrl);
try {
FileInputStream fis=new FileInputStream(file);
byte [] buf=new byte[1024];
int len=0;
try {
while((len=fis.read(buf))!=-1)
{
String ss=new String(buf,0,len);
strs.append(ss);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strs.toString();
}
CommonsUtil.wirteToJson(fileUrl, jObject.toString());
String ss=CommonsUtil.readJson(fileUrl);
JSONObject objs=JSONObject.fromObject(ss);
bean=(WsPendingWorkformDetailBean) com.alibaba.fastjson.JSON.parseObject(ss,WsPendingWorkformDetailBean.class);
本文提供了一个使用Java进行文件读写的示例,重点展示了如何将对象转换为JSON字符串并写入文件,以及如何从文件中读取JSON字符串并解析成对象。
432

被折叠的 条评论
为什么被折叠?



