我在读取原始文件的 Android 应用程序中有这样的方法:
public String inputStreamToString(InputStream isTwo) throws IOException {
StringBuffer sBuffer = new StringBuffer();
DataInputStream dataIO = new DataInputStream(isTwo);
String strLineTwo = null;
while ((strLineTwo = dataIO.readLine()) != null) {
sBuffer.append(strLineTwo + "\n");
}
dataIO.close();
isTwo.close();
return sBuffer.toString();
}
但是,DataInputStream 对象现在似乎已被弃用。我研究了它,听说最好readline()
用BufferedInputStream
. 有人可以帮我完成他的(填写缺失的行)吗?我不确定如何声明br
var。这是我到目前为止所拥有的:
public String inputStreamToString(InputStream isTwo) throws IOException {
String strLineTwo = null;
BufferedReader br = null;
StringBuffer sBuffer = new StringBuffer();
InputStreamReader dataIO = new InputStreamReader(isTwo);
while ((strLineTwo = br.readLine()) != null) {
sBuffer.append(strLineTwo + "\n");
}
dataIO.close();
isTwo.close();
return sBuffer.toString();
这是调用此方法的前面我尚未触及的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tech);
InputStream iFileTwo = getResources().openRawResource(R.raw.testing);
try {
TextView helpText = (TextView) findViewById(R.id.tvStream);
String strFileTwo = inputStreamToString(iFileTwo);
helpText.setText(strFileTwo);
} catch (Exception e) {
Log.e(DEBUG_TAG_THREE, "InputStreamToString failure", e);
}
}
另外,我想确保它适用于 Android 2.3 到 4.2(当前)。谢谢你的帮助。