從靜態(tài)map中取值添加進(jìn)數(shù)組
public final static Map<String,Integer> WUHAN_AREAS = new LinkedHashMap<String,Integer>(); static { WUHAN_AREAS.put("北省", B_AREA_ID); WUHAN_AREAS.put("漢市", H_AREA_ID); WUHAN_AREAS.put("昌區(qū)", 04); WUHAN_AREAS.put("口區(qū)", 02); WUHAN_AREAS.put("陽區(qū)", 03); WUHAN_AREAS.put("山區(qū)", 05); WUHAN_AREAS.put("湖區(qū)", 06); WUHAN_AREAS.put("甸區(qū)", 07); WUHAN_AREAS.put("江區(qū)", 08); WUHAN_AREAS.put("黃區(qū)", 09); WUHAN_AREAS.put("新區(qū)", 10); WUHAN_AREAS.put("江區(qū)", 00); WUHAN_AREAS.put("漢區(qū)", 01); WUHAN_AREAS.put("南區(qū)", 11); WUHAN_AREAS.put("青區(qū)", 12); } //區(qū)的id數(shù)組 public final static Long[] HA_AREAS_ARY = new Long[HA_AREAS.size()]; static{ int i = 0; for(String key : HA_AREAS.keySet()){ HA_AREAS_ARY[i++] = Long.parseLong(HA_AREAS.get(key).toString()); } }從項(xiàng)目編譯后的src下取文件到java中
@Controller@RequestMapping("/opinion/risk")public class RiskController extends BaseController { @RequestMapping("loadInfo") public ModelAndView loadInfo(String beginDate,String endDate,Page page,int articleType,int sortType){ page.setPageSize(9); ModelAndView mav = new ModelAndView(); page = riskService.getRiskArticle(beginDate, endDate, sortType, articleType, page); List<NewsBean> newsList = page.getData(); StringUtil util = new StringUtil(); String path = RiskController.class.getResource("/").getPath(); String fileName=path + "config/opinionWord.txt"; List<String> wordlist =util.readFileByLines(fileName); for(NewsBean bean : newsList){ String title = bean.getTitle(); String fmtTitle =util.colorStrByOpinionWord(wordlist,title,"red"); bean.setTitle(fmtTitle); } mav.addObject("page", page); mav.setViewName(PATH_RISK + "articleInfo"); return mav; }}//StringUtil中的readFileByLines和colorStrByOpinionWord方法: /** * 讀取工程中的text文件 * @param filePath * @return */ public List<String> readFileByLines(String filePath){ List <String> li = new ArrayList<String>(); File file = new File(filePath); BufferedReader br = null; try { br = new BufferedReader( new InputStreamReader( new FileInputStream(filePath),"UTF-8")); String tempString = null; while((tempString = br.readLine()) != null){ li.add(tempString); } br.close(); return li; } catch (FileNotFoundException e) { e.PRintStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br!= null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }/** * 給字符串中預(yù)警詞加高亮背景 * @param srcStr:源字符串 * @param color:高亮顏色 * @return */ public String colorStrByOpinionWord(List<String> wordlist, String srcStr, String color){ if(wordlist != null){ for(String keyword:wordlist){ if(keyword != null && !"".equals(keyword)){ color = (color == null || "".equals(color)) ? "red" : color;//默認(rèn)紅色高亮 srcStr = srcStr.replaceAll(keyword, "<font color=/""+color+"/">" + keyword + "</font>"); } } return srcStr; } return null; }
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注