读取txt文件每行内容
public Map<Integer, String> Txt(String filePath) {
//将读出来的一行行数据使用Map存储
Map<Integer, String> map = new HashMap<Integer, String>();
try {
File file = new File(filePath);
int count = 0;//初始化 key值
if (file.isFile() && file.exists()) { //文件存在的前提
InputStreamReader isr = null;
try {
isr = new InputStreamReader(new FileInputStream(file), "utf-8");
} catch (java.io.UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
while ((lineTxt = br.readLine()) != null) { //
if (!"".equals(lineTxt)) {
// String reds = lineTxt.split("\+")[0]; //java 正则表达式
String reds = lineTxt;
map.put(count, reds);//依次放到map 0,value0;1,value2
count++;
}
}
isr.close();
br.close();
}else {
Toast.makeText(getApplicationContext(),"can not find file",Toast.LENGTH_SHORT).show();//找不到文件情况下
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
读取txt文件每行内容
public Map<Integer, String>