public static void staticalFeature(String source, String dest) { BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; if (!new File(source).exists()) { System.err.println("There is no such file."); return; } FileUtil.CompulsoryCreateFile(dest); try { bufferedReader = new BufferedReader(new FileReader(source)); bufferedWriter = new BufferedWriter(new FileWriter(dest)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Map<String, ArrayList<String>> featureHashMap = new HashMap<String, ArrayList<String>>(); String line = null, feature = null, queryId = null; String[] mapElements = null; ArrayList<String> features = null; System.out.println("begin to reading ..."); try { while ((line = bufferedReader.readLine()) != null) { mapElements = line.split(" "); feature = mapElements[0].substring(34); queryId = mapElements[1].substring(27); if (featureHashMap.containsKey(queryId)) { features = featureHashMap.get(queryId); if (!features.contains(feature)) features.add(feature); } else { features = new ArrayList<String>(); if (features.add(feature)) featureHashMap.put(queryId, features); else { throw new Exception("add error"); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String shortFeatures = null; Set<String> queryIds = featureHashMap.keySet(); try { for (String qId : queryIds) { shortFeatures = ""; for (String fea : featureHashMap.get(qId)) shortFeatures += fea.charAt(0); bufferedWriter.write(qId + "\t" + shortFeatures + "\n"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bufferedWriter.flush(); bufferedWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("end to write..."); }
public static void CountFeature(String source, String dest) { BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; if (!new File(source).exists()) { System.err.println("There is no such file."); return; } FileUtil.CompulsoryCreateFile(dest); try { bufferedReader = new BufferedReader(new FileReader(source)); bufferedWriter = new BufferedWriter(new FileWriter(dest)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Map<String, Integer> faturesHashMap = new HashMap<String, Integer>(); String line = null, features = null; Integer count = null; String[] mapElements = null; System.out.println("begin to reading ..."); try { while ((line = bufferedReader.readLine()) != null) { mapElements = line.split("\t"); features = mapElements[1]; if (faturesHashMap.containsKey(features)) { count = faturesHashMap.get(features); faturesHashMap.put(features, new Integer(count + 1)); } else { faturesHashMap.put(features, new Integer(1)); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Set<String> featureSet = faturesHashMap.keySet(); try { for (String feature : featureSet) { bufferedWriter.write(feature + "\t" + faturesHashMap.get(feature) + "\n"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bufferedWriter.flush(); bufferedWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("end to write..."); }