读取Assets中的文本

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		System.out.println(getFromAssets("cities.json"));
		Log.e("getFromAssets", getFromAssets("cities.json")+"----------");
	}

	public String getFromAssets(String fileName){ 
        try { 
             InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) ); 
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line="";
            result = "";
            while((line = bufReader.readLine()) != null)
                result += line;
            
            
        } catch (Exception e) { 
            e.printStackTrace(); 
        }finally{
        	return result;
        }
        
}

运行效果:


wKiom1WWAhDCfaUBAALdbccR1kE863.jpg

你可能感兴趣的:(读取Assets中的文本)