All files in the assets folder are in the original file format. You need to use AssetManager to read files in the form of byte streams.
1. Call getAssets () in the Activity to obtain the AssetManager reference.
2. Use AssetManager's open (String fileName, int accessMode) method to specify the file to be read and the access mode to obtain the input stream InputStream.
3. Use inputStream of the open file to read the file. After reading the file, remember inputStream. close ().
4. Call AssetManager. close () to close AssetManager.
Note that files from Resources and Assets can only be read but cannot be written.
Read from Raw files as follows:
[Java] public String getFromRaw (){
Try {
InputStreamReader inputReader = new InputStreamReader (getResources (). openRawResource (R. raw. test1 ));
BufferedReader bufReader = new BufferedReader (inputReader );
String line = "";
String Result = "";
While (line = bufReader. readLine ())! = Null)
Result + = line;
Return Result;
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public String getFromRaw (){
Try {
InputStreamReader inputReader = new InputStreamReader (getResources (). openRawResource (R. raw. test1 ));
BufferedReader bufReader = new BufferedReader (inputReader );
String line = "";
String Result = "";
While (line = bufReader. readLine ())! = Null)
Result + = line;
Return Result;
} Catch (Exception e ){
E. printStackTrace ();
}
}
Read from assets directly
[Java] public String getFromAssets (String fileName ){
Try {
InputStreamReader inputReader = new InputStreamReader (getResources (). getAssets (). open (fileName ));
BufferedReader bufReader = new BufferedReader (inputReader );
String line = "";
String Result = "";
While (line = bufReader. readLine ())! = Null)
Result + = line;
Return Result;
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public String getFromAssets (String fileName ){
Try {
InputStreamReader inputReader = new InputStreamReader (getResources (). getAssets (). open (fileName ));
BufferedReader bufReader = new BufferedReader (inputReader );
String line = "";
String Result = "";
While (line = bufReader. readLine ())! = Null)
Result + = line;
Return Result;
} Catch (Exception e ){
E. printStackTrace ();
}
}
Of course, if you want to get the memory stream, you can also directly return the memory stream!
From agods-footprint