1

如何将本地 html 文件(从资产文件夹)加载到字符串?我试过这段代码,但结果只是“??????...”。

InputStream is = getAssets().open("aaa.html");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String str = new String(buffer);
System.out.println(str);

谢谢你的帮助!

4

2 回答 2

4

您没有阅读整个文件。试试这个:

StringBuilder builder = new StringBuilder();

byte[] buffer = new byte[1024];
while(is.read(buffer) != -1) {
    builder.append(new String(buffer));
}
is.close();
String str = builder.toString();
于 2013-11-11T07:35:24.963 回答
0

试试这个......

File file = new File("file:///android_asset/yuor_file.html");        
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

StringBuffer sb = new StringBuffer();
String linewise = br.readLine();

while(linewise != null) {
  sb.append(linewise );
  sb.append("\n");
  linewise = br.readLine();
}
//now data in sb
于 2013-11-11T07:38:54.657 回答