java读取文件乱码
List<String> lines=new ArrayList<String>(); BufferedReader br = new BufferedReader(new FileReader(fileName)); String line = null; while ((line = br.readLine()) != null) { lines.add(line); } br.close();
Java的I/O类处理如图:
Reader 类是 Java 的 I/O 中读字符的父类,而 InputStream 类是读字节的父类,InputStreamReader 类就是关联字节到字符的桥梁,它负责在 I/O 过程中处理读取字节到字符的转换,而具体字节到字符的解码实现它由 StreamDecoder 去实现,在 StreamDecoder 解码过程中必须由用户指定 Charset 编码格式。值得注意的是如果你没有指定 Charset,将使用本地环境中的默认字符集,例如在中文环境中将使用 GBK 编码。
总结:Java读取数据流的时候,一定要指定数据流的编码方式,否则将使用本地环境中的默认字符集。
public static List<Organ> fileinput() { // TODO Auto-generated method stub List<Organ> list = new ArrayList<Organ>(); try { String fileName = "C://Users/Administrator/Desktop/备份/bustno.txt"; StringBuffer sb = new StringBuffer(""); InputStreamReader sr = new InputStreamReader(new FileInputStream(fileName),"UTF-8"); BufferedReader br = new BufferedReader(sr); String str = null; while ((str = br.readLine()) != null) { String[] firstSplit = str.split("\n"); int recordNum = firstSplit.length; for(int i=0; i < recordNum;i++){ String[] one = str.split("\\|"); Organ organ = new Organ(); organ.setParentID(0); organ.setOrganValue(one[0].substring(0, 3)); organ.setOrganName(one[1]); list.add(organ); } } br.close(); sr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.print("read succeeded"); return list; }
list结果集分组:
private static void fenzu(String filepath) throws IOException, ParseException { // TODO Auto-generated method stub SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM"); List<String[][]> valueList = new ArrayList<String[][]>();//将结果集转化为二维数组 StringBuffer sb = new StringBuffer(""); InputStreamReader sr = new InputStreamReader(new FileInputStream(filepath),"UTF-8"); BufferedReader br = new BufferedReader(sr); String str = null; List<Canal> list = new ArrayList<Canal>(); while ((str = br.readLine()) != null) { String[] firstSplit = str.split("\n"); int recordNum = firstSplit.length; for(int i=0; i < recordNum;i++){ String[] one = str.split("\t"); Canal canalone = new Canal(); canalone.setCanal(one[0]); canalone.setSa_tx_m(one[1]); canalone.setCr_tx_amt(one[2]); list.add(canalone); }} System.out.print("list size :"+list.size()+ "\n"); //结果集分组 List uniqueList = new ArrayList();//存放Canal个数 for (int j = 0; j < list.size(); j++) { Canal canal = list.get(j); if (!uniqueList.contains(canal.getCanal())) { System.out.print("uniqueList not contains : " +canal.getCanal().toString() + "\n" ); String[][] b = new String[2][12];//String[0][]为canal,长度为1,String[1][]对应每个月的数值 b[0][0] = canal.getCanal();//canal String amtValue = list.get(j).getCr_tx_amt();// 金额 Calendar Date = Calendar.getInstance(); Date.setTime(simpleDate.parse(canal.getSa_tx_m())); int month = Date.get(Calendar.MONTH); // 因为数组的编号要减一,这里不用加一 b[1][month] = amtValue; uniqueList.add(canal.getCanal());//将canal种类放在集合里 valueList.add(b);//将数值集合放在valueList里 } else { for (int k = 0; k < valueList.size(); k++) { String[][] b = valueList.get(k); if (b[0][0].equals(canal.getCanal())) { valueList.remove(k);//先remove结果集里不完整的记录 System.out.print("has found canal : " + canal.getCanal() + "\n"); String amtValue = canal.getCr_tx_amt();// 金额 Calendar Date = Calendar.getInstance(); Date.setTime(simpleDate.parse(canal .getSa_tx_m())); int month = Date.get(Calendar.MONTH); // 因为数组的编号要减一,这里不用加一 b[1][month] = amtValue.toString(); valueList.add(b);//加了数值再add到分组的list里 break; //找到就返回 } } } } }
坚持不懈