try {
inputStream = assetManager.open("model.obj");
if (inputStream != null)
{
Log.d("aaa", "It worked!");
//Get length of inputstream
for(int i=0;i<inputStream.available();i++)
{
}
//String line = null, input="";
/*while( (line = inputStream. ) != null )
{
input += line;
}*/
}
} catch (IOException e) {
e.printStackTrace();
}
我有inputStream,它看起来像是原始字节,我可以从inputStream获取一些字节并循环。 我实际想要做的是循环读取文件中的每一行,并在空格字符上拆分,如split(“”)。 可以将inputStream转换成某种形式,更方便读取行和拆分空格字符吗?
谢谢!
答案 0 :(得分:2)
您可以按如下方式使用BufferedReader
类的readLine()
方法来检测是否存在新行。
InputStream is = new ByteArrayInputStream("file content".getBytes());
//read it with BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null)
{
// perform your task here
}
答案 1 :(得分:1)
Scanner课程将派上用场。您可以将输入流传递给扫描仪类,然后轻松地将它们作为线条。
try {
inputStream = assetManager.open("model.obj");
Scanner sc;
if (inputStream != null) {
sc = new Scanner(inputStream); // also has a constructor which take in a charsetName
while(sc.hasNextLine()) {
sc.nextLine();
}
}
} catch (IOException e) {
e.printStackTrace();
}
答案 2 :(得分:1)
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line;
while ((line = br.readLine()) != null)
{
// perform your task here
}
答案 3 :(得分:0)
这是Assets ....的代码读取文件。
AssetManager assetManager = getResources()。getAssets(); InputStream inputStream = null;
try {
inputStream = assetManager.open("foo.txt");
if ( inputStream != null)
Log.d(TAG, "It worked!");
} catch (IOException e) {
e.printStackTrace();
}
不要使用InputStream is = assetManager.open(“assets / foo.txt”);
Try this link分割你需要的东西......