package com.swift;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; /**
* Servlet implementation class DeleteServlet
*/
@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public DeleteServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
File file=new File("d:/student.json");
//获取全部学生对象
List<Student> list=getAllStudent(file);
System.out.println(list.toString());
//获取要删除学生姓名
String name=request.getParameter("name");
System.out.println(name);
//从list对象列表中删除匹配姓名的学生对象
deleteStudentByName(response, list, name);
System.out.println(list.toString());
//将删除后的对象列表写入磁盘文件
StringBuffer sb=new StringBuffer();//将剩下的json信息保存到sb中,不然文件中只能得到最后一个json数据
Gson gson=new Gson();
for(Student stu:list) {
String json=gson.toJson(stu);
sb.append(json);
sb.append("\r\n");
}
writeToFile(file, sb.toString());
} private void deleteStudentByName(HttpServletResponse response, List<Student> list, String name) throws IOException {
for(int i=0;i<list.size();i++) {
if(list.get(i).getName().equals(name)) {
list.remove(i);
response.getWriter().println("You have already delete the name "+name);
}
}
} private void writeToFile(File file,String json) {
PrintWriter pw = null;
try {
try {
pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.println(json);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
pw.close();
}
} private List<Student> getAllStudent(File file) throws UnsupportedEncodingException, FileNotFoundException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
List<Student> list=new ArrayList<Student>();
String str;
while((str=br.readLine())!=null) {
Gson gson=new Gson();
Student stu=gson.fromJson(str, Student.class);
list.add(stu);
}
br.close();
return list;
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

使用Servlet根据浏览器request的get方法获取值,将磁盘中与之对应的json数据删除的方法的更多相关文章

  1. AJAX跨域请求json数据的实现方法

    这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式, ...

  2. 使用Python解析JSON数据的基本方法

    这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下:     ----------------------------------- ...

  3. JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  4. 【jmeter】JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  5. JMeter中返回Json数据的处理方法(转)

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  6. JMeter 中对于Json数据的处理方法

    JMeter中对于Json数据的处理方法 http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/ J ...

  7. 转-oracle中比较两表表结构差异和数据差异的方法

    oracle中比较两表表结构差异和数据差异的方法 原作者:li2008xue2008ling  出处:http://blog.csdn.net       在工作中需要完成这么一个需求:比较两个表的表 ...

  8. 【转】JMeter中对于Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  9. jmeter(二十)JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

随机推荐

  1. Windows 在目录中搜索哪个文件中有指定字符串

    findstr /s /i "string" *.* 表示,当前目录以及子目录下的所有文件中查找"string"这个字符串. *.*表示所有类型的文件. /s ...

  2. (6)ASP.NET Core 中使用IHttpClientFactory发出HTTP请求

    1.HttpClient类使用存在的问题 HttpClient类的使用所存在的问题,百度搜索的文章一大堆,好多都是单纯文字描述,让人感觉不太好理解,为了更好理解HttpClient使用存在的问题,下面 ...

  3. webpack 3 优化

    编译时间太长 项目为多页面应用时,编译的时候每个入口都会读取依赖的路径,所以入口越多,会导致编译越慢 公用库提取 除了公用的框架(如 Vue.React)以外,不同页面所需要的第三方库可能不一样,而且 ...

  4. Ini文件格式说明

    http://www.cnblogs.com/CUIT-DX037/ 百度百科介绍:ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储 ...

  5. php 出现空格,换行原因

    1.空格:转换成utf-8无bom格式 2.空格:<?php ?>标签结尾有中文,英文空格 3.换行,一个文件中有多个<?php ?>标签,标签间隔中有换行符合

  6. axios使用配置

    axios 配置 下载cnpm install axios vue-axios --save-dev main.js文件中配置 import axios from 'axios' import Vue ...

  7. 【Linux/Ubuntu学习 10】unbuntu 下 eclipse 中文乱码的解决

    wangdd@wdd-pc:~$ gedit /var/lib/locales/supported.d/local 添加: zh_CN.GBK GBK zh_CN.GB2312 GB2312 终端执行 ...

  8. Google pieCharts的学习

    在公司项目开发过程中, 尤其是在网站的开发过程中,用到很多的前端的插件,在这里, 我简单介绍下近期Google pieCharts的是使用方法 https://developers.google.co ...

  9. 《Unity預計算即時GI》笔记:二、光照图

    说明 这篇文章是对<Unity預計算即時GI>这个系列文章的笔记. 光照图 什么是光照图 光照图在第三章中有如下的定义,读起来很是费解. 一個光照圖(Chart)是表示一個光照貼圖的區域, ...

  10. python3基础05(有关日期的使用1)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import timefrom datetime import datetime,timedelta,timez ...