background image

    

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);