1
            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 转换为更有助于读取行和分割空格字符的形式吗?

谢谢你!

4

4 回答 4

3

您可以使用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 
}       
于 2013-07-03T05:51:06.947 回答
2
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line;
while ((line = br.readLine()) != null) 
{
   // perform your task here 
}
于 2013-07-03T05:53:02.163 回答
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();
}
于 2013-07-03T05:52:30.063 回答
0

这是从 Assets 中读取的代码文件......

AssetManagerassetManager = getResources().getAssets(); 输入流 inputStream = null;

try {
    inputStream = assetManager.open("foo.txt");
        if ( inputStream != null)
            Log.d(TAG, "It worked!");
    } catch (IOException e) {
        e.printStackTrace();
    }

不要使用 InputStream is = assetsManager.open("assets/foo.txt");

试试这个链接来分割你需要的东西......

于 2013-07-03T05:52:51.033 回答