概述
CsvServlet.java
package com.ajax.dao;
import java.io.IOException;
import java.util.Map;
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 CsvServlet
*/
@WebServlet("/CsvServletDemo.do")
public class CsvServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CsvServlet() {
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
request.setCharacterEncoding("utf-8");
response.setContentType("text/plain;charset=utf-8");
CsvTest csv=new CsvTest();
Map<String,Integer> map=csv.csvMap();
Gson gson =new Gson();
String message=gson.toJson(map);
response.getWriter().print(message);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
CsvTest.java
package com.ajax.dao;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.csvreader.CsvReader;
public class CsvTest {
public Map<String,Integer> csvMap() {
Map<String,Integer> map=null;
try {
//取出csv文件里面所有的数据。
List<String[]> csvFileList= new ArrayList<String[]>();
//创建一个csv读的对象CsvReader(文件路径,分隔符,编码格式)
String csvFilePath="D://hotel_comments1.csv";
CsvReader reader=new CsvReader(csvFilePath, ',', Charset.forName("utf-8"));
//初步读取除表头的数据
while(reader.readRecord()){
String[] strList=reader.getValues();
csvFileList.add(strList);
}
reader.close();
//取出所有景点名称
List<String> list=new ArrayList<String>();
for(int rows=0;rows<csvFileList.size();rows++){
String clo=csvFileList.get(rows)[0];
list.add(clo);
}
//计算每个景点出现的次数
map = new HashMap<String,Integer>();
for(int i=0;i<list.size();i++){
//取出所有的景点名称
String key=list.get(i);
//在map里面查看是否有值
Integer num=map.get(key);
if(num == null ||num==0){
map.put(key, 1);
}else{
map.put(key, num+1);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
}
csvTest.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/echarts.min.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
</head>
<body onload="csvDemo()">
<div id="main" style="width: 500px;height:400px;"></div>
<script type="text/javascript">
function csvDemo(){
$.get("CsvServletDemo.do",
function(data){
var xArr=[];
var yArr=[];
for(var i in data){
xArr.push(i);
yArr.push(data[i]);
}
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '显示景点出现的次数'
},
tooltip: {},
legend: {
data:['景点']
},
xAxis: {
data: xArr
},
yAxis: {},
series: [{
name: '景点',
type: 'bar',
data: yArr
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
}
,"json")
}
</script>
</body>
</html>
最后
以上就是曾经飞鸟为你收集整理的读取CSV文件JAVA实现的全部内容,希望文章能够帮你解决读取CSV文件JAVA实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复