日期:2014-05-17 浏览次数:20760 次
txt 中的资料 张三;男;20;; 李四;女;19;大专; 王五;男;23;本科; 已能读取 txt 的资料, 现在的问题是如何把 txt中的资料分组取出! 求相关例子!tks!
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class test { private void test(){ FileReader reader; try { reader = new FileReader("D:\\test.txt"); BufferedReader br = new BufferedReader(reader); String s1 = null; while((s1 = br.readLine()) != null) { String[] data=s1.split(";"); String realname=data[0]; //姓名 String sex=data[1]; //性别 String age=data[2]; //年龄 String education=data[3]; //学历 //写入数据库 省略 } br.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
------解决方案--------------------
支持ls的!!
做好加上去掉空格
trim()
------解决方案--------------------
写了一段,希望多理解一下吧。理解了的东西才是自己的
public class Test { public List<PersonInfo> getPersonInfo(String path){ List<PersonInfo> persons = new ArrayList<PersonInfo>(); FileReader reader; try { reader = new FileReader("D:\\test.txt"); BufferedReader br = new BufferedReader(reader); String s1 = null; while((s1 = br.readLine()) != null) { String[] data=s1.split(";"); if(data.length != 4){ String[] strs = new String[4]; for(int i = 0; i < data.length; i++){ strs[i] = data[1]; } data = strs; } persons.add(new PersonInfo(data[0],data[1],Integer.parseInt(data[2]),data[3])); } br.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } return persons; } public void insertPersons(List<PersonInfo> persons){ Connection c = null; Statement st = null; try { Class.forName("");//加载驱动 c = DriverManager.getConnection("", "", "");//获取连接 List<String> sqls = getInsertSql(persons); st = c.createStatement(); int count = 1; c.setAutoCommit(false); for(String sql : sqls){ st.addBatch(sql); count++; if(count == 100){ st.executeBatch(); count = 1; } } c.commit(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally{ try { if(c != null){ c.close(); } if(st != null){ st.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public List<String> getInsertSql(List