例如raw文件夹下面的文件是w_city_code.txt,那么读取参考代码如下:
BufferedReader reader=new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.w_city_code)));
boolean found=false;
try {
String line;
String name;
boolean isPreEmpty=true;
while((line=reader.readLine())!=null){
if(line.length()>10){
if(isPreEmpty){
name=line.substring(10);
if(name.equals(cityName)){
// find the city for shenghui
found=true;
// search the city names and fill the list
cities=searchCitys(line.substring(0, 9),line.substring(10),reader);
fillCities(cities);
break;
}
}
isPreEmpty=false;
}else{
isPreEmpty=true;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
if(!found){
setListAdapter(null);
}
}