JAVA读取本地txt文件,并将每行数据存入数组的方法:
/** * 读取本地普通文件,将其转化为一个字符串数组 * @return */ public ArrayList<AdShow> getTxt(String filepath){ try{ String temp = null; File f = new File(filepath); String adn=""; //指定读取编码用于读取中文 InputStreamReader read = new InputStreamReader(new FileInputStream(f),"GBK"); ArrayList<String> readList = new ArrayList<String>(); ArrayList<AdShow> retList = new ArrayList<AdShow>(); BufferedReader reader=new BufferedReader(read); //bufReader = new BufferedReader(new FileReader(filepath)); while((temp=reader.readLine())!=null &&!"".equals(temp)){ readList.add(temp); } read.close(); return retList; }catch (Exception e) { // TODO: handle exception logger.info("读取文件--->失败!- 原因:文件路径错误或者文件不存在"); e.printStackTrace(); return null; } }