将csv中信息存取到java中的map
1.从提供的csv文件中,把数据读取出来,并且把数据放在map中。2.把男女生归类,放在2个不同的map中。3.把以上结果打印到控制台。user_iduser_namea...
1. 从提供的csv文件中,把数据读取出来,并且把数据放在map中。
2. 把男女生归类,放在2个不同的map中。
3. 把以上结果打印到控制台。
user_id user_name age sex gtm_created gmt_modify
1 用户1 20 男 2011-8-22 2011-8-23
2 用户2 21 女 2011-8-23 2011-8-24
3 用户3 22 男 2011-8-24 2011-8-25
4 用户4 23 男 2011-8-25 2011-8-26
5 用户5 24 男 2011-8-26 2011-8-27
6 用户6 25 女 2011-8-27 2011-8-28 展开
2. 把男女生归类,放在2个不同的map中。
3. 把以上结果打印到控制台。
user_id user_name age sex gtm_created gmt_modify
1 用户1 20 男 2011-8-22 2011-8-23
2 用户2 21 女 2011-8-23 2011-8-24
3 用户3 22 男 2011-8-24 2011-8-25
4 用户4 23 男 2011-8-25 2011-8-26
5 用户5 24 男 2011-8-26 2011-8-27
6 用户6 25 女 2011-8-27 2011-8-28 展开
2个回答
展开全部
首先冲慧要有个实体类(javabean)放一个尺仔用户的信息:
class User {
public String userId;
public String userName;
public String age;
public String sex;
public String gtmCreated;
public String gtmModify;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGtmCreated() {
return gtmCreated;
}
public void setGtmCreated(String gtmCreated) {
this.gtmCreated = gtmCreated;
}
public String getGtmModify() {
return gtmModify;
}
public void setGtmModify(String gtmModify) {
this.gtmModify = gtmModify;
}
}
具体散困答实现:
public static void readCsvFile() throws IOException {
Map<String, User> manHs = new HashMap<String, User>();
Map<String, User> womanHs = new HashMap<String, User>();
File csvFile = new File("F:"+File.separator+"testSource"+File.separator+"csv.csv");
FileReader fr = new FileReader(csvFile);
BufferedReader br = new BufferedReader(fr);
String lineStr = null ;
int indexman = 0;
int indexwoman = 0;
while((lineStr = br.readLine()) != null) {
User user = new User();
String[] arr= lineStr.split(",");
user.setUserId(arr[0]);
user.setUserName(arr[1]);
user.setAge(arr[2]);
user.setSex(arr[3]);
user.setGtmCreated(arr[4]);
user.setGtmModify(arr[5]);
if(arr[2].equals("男")) {
manHs.put("man"+indexman, user);
indexman++;
}else {
womanHs.put("woman"+indexwoman, user);
indexwoman++;
}
}
for(Entry<String, User> entry : manHs.entrySet()) {
out.println(entry.getKey() + "\t" + entry.getValue().getUserName());
}
for(Entry<String, User> entry : womanHs.entrySet()) {
out.println(entry.getKey() + "\t" + entry.getValue().getUserName());
}
}
在main方法中直接调用
class User {
public String userId;
public String userName;
public String age;
public String sex;
public String gtmCreated;
public String gtmModify;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGtmCreated() {
return gtmCreated;
}
public void setGtmCreated(String gtmCreated) {
this.gtmCreated = gtmCreated;
}
public String getGtmModify() {
return gtmModify;
}
public void setGtmModify(String gtmModify) {
this.gtmModify = gtmModify;
}
}
具体散困答实现:
public static void readCsvFile() throws IOException {
Map<String, User> manHs = new HashMap<String, User>();
Map<String, User> womanHs = new HashMap<String, User>();
File csvFile = new File("F:"+File.separator+"testSource"+File.separator+"csv.csv");
FileReader fr = new FileReader(csvFile);
BufferedReader br = new BufferedReader(fr);
String lineStr = null ;
int indexman = 0;
int indexwoman = 0;
while((lineStr = br.readLine()) != null) {
User user = new User();
String[] arr= lineStr.split(",");
user.setUserId(arr[0]);
user.setUserName(arr[1]);
user.setAge(arr[2]);
user.setSex(arr[3]);
user.setGtmCreated(arr[4]);
user.setGtmModify(arr[5]);
if(arr[2].equals("男")) {
manHs.put("man"+indexman, user);
indexman++;
}else {
womanHs.put("woman"+indexwoman, user);
indexwoman++;
}
}
for(Entry<String, User> entry : manHs.entrySet()) {
out.println(entry.getKey() + "\t" + entry.getValue().getUserName());
}
for(Entry<String, User> entry : womanHs.entrySet()) {
out.println(entry.getKey() + "\t" + entry.getValue().getUserName());
}
}
在main方法中直接调用
展开全部
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class ReadCsv extends JFrame {
public static void main(String[] args) {
ReadCsv rc = new ReadCsv();
File file = new File("d:\\test.csv"空仿旦);
TableModel playerInfo = getFileStats(file);
JTable jt = new JTable(playerInfo);
jt.setPreferredScrollableViewportSize(new Dimension(400, 80));
rc.getContentPane().add(jt);
rc.setTitle("ReadCsv Table");
rc.setDefaultCloseOperation(EXIT_ON_CLOSE);
rc.setSize(300, 250);
rc.setVisible(true);
}
private static TableModel getFileStats(File a) {
String data;
Object[] object = null;
int columnCount = 0;//文件中最大行的列数
DefaultTableModel dt = new DefaultTableModel();
try {
BufferedReader br = new BufferedReader(new FileReader(a));
//不是文件尾一直读
while ((data = br.readLine()) != null) {
object = data.split(",");
//如果这行的列数大于最大的,那么斗扰再增加一列
if (object.length > columnCount) {
for(int i=0;i<object.length-columnCount;i++){
dt.addColumn("大拍column".concat(String.valueOf(i)));
}
columnCount = object.length;
}
//添加一行
dt.addRow(object);
}
;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return dt;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class ReadCsv extends JFrame {
public static void main(String[] args) {
ReadCsv rc = new ReadCsv();
File file = new File("d:\\test.csv"空仿旦);
TableModel playerInfo = getFileStats(file);
JTable jt = new JTable(playerInfo);
jt.setPreferredScrollableViewportSize(new Dimension(400, 80));
rc.getContentPane().add(jt);
rc.setTitle("ReadCsv Table");
rc.setDefaultCloseOperation(EXIT_ON_CLOSE);
rc.setSize(300, 250);
rc.setVisible(true);
}
private static TableModel getFileStats(File a) {
String data;
Object[] object = null;
int columnCount = 0;//文件中最大行的列数
DefaultTableModel dt = new DefaultTableModel();
try {
BufferedReader br = new BufferedReader(new FileReader(a));
//不是文件尾一直读
while ((data = br.readLine()) != null) {
object = data.split(",");
//如果这行的列数大于最大的,那么斗扰再增加一列
if (object.length > columnCount) {
for(int i=0;i<object.length-columnCount;i++){
dt.addColumn("大拍column".concat(String.valueOf(i)));
}
columnCount = object.length;
}
//添加一行
dt.addRow(object);
}
;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return dt;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询