1.android 读取assets下文件
public static String loadDataFromAssets(Context context,String assetsName){
InputStream in = null;
String jsonstr = null;
try {
//get input stream of assets file
in = context.getAssets().open(assetsName);
//new a output stream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//new a buffer array
int len = -1;
byte buf[] = new byte[1024 * 10];
//read file stream form input stream to output stream
while ((len = in.read(buf)) != -1) {
bos.write(buf, 0, len);
}
//get the result string
jsonstr = bos.toString();
} catch (IOException e) {
e.printStackTrace();
}
return jsonstr;
}
2.java读取本地文件
public static String readStringFromFileCache(Context context, String fileName) {
if (context == null || fileName == null) {
return null;
}
try {
// 打开文件输入流
FileInputStream fis = context.openFileInput(fileName);
byte[] buff = new byte[1024];
int hasRead = 0;
StringBuilder sb = new StringBuilder("");
// 读取文件内容
while ((hasRead = fis.read(buff)) > 0) {
sb.append(new String(buff, 0, hasRead));
}
// 关闭文件输入流
fis.close();
return sb.toString();
} catch (Exception e) {
}
return null;
}