BufferedReader
reader
=
null;
String
laststr
=
"";
try
{
//System.out.println(" 以 行 为 单 位 读 取 文 件 内 容 , 一 次 读 一 整 行 : ");
reader
=
new
BufferedReader(new
FileReader(file));
String
tempString
=
null;
int
line
=
1;
// 一 次 读 入 一 行 , 直 到 读 入
null
为 文 件 结 束
while ((tempString = reader.readLine()) != null) {
//
显
示
行
号
System.out.println("line
"
line
":
"
tempString);
laststr
=
laststr
tempString;
line
;
}
reader.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
finally
{
if
(reader
!=
null)
{
try
{
reader.close();
}
catch
(IOException
e1)
{
}
}
}
return
laststr;
}
//
把
json
格
式
的
字
符
串
写
到
文
件
public void writeFile(String filePath, String sets) throws IOException {
FileWriter
fw
=
new
FileWriter(filePath);
PrintWriter
out
=
new
PrintWriter(fw);
out.write(sets);
out.println();
fw.close();
out.close();
}
4. 调 用 , 使 用 ( 在 网 站 的
controller
里 调 用 的 )
//
取
出
json
对
象
public
void
GetElectSettings(HttpServletRequest
request,
HttpServletResponse
response)
throws
Exception
{
ElectSet
electset=new
ElectSet();
String
absPath
=
request.getRealPath("\");
String
filePath
=
absPath
"public\sets\electSets.json";
electset=businessService.findElectSets(filePath);//这里是调用,大家自己改改,
我
调
用
的
业
务
层
的
。
JSONArray
jsonItems
=
new
JSONArray();
jsonItems.add(electset);
JSONObject
jo=new
JSONObject();
jo.put("data",
jsonItems);
System.out.println(jo);
request.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
PrintWriter
out
=
response.getWriter();
out.print(jo);