How to get particular line to line from text file and display array list in android

纵然是瞬间 提交于 2019-12-05 22:54:55

The solution below is find all the lines after '## userot' until the next ##

FileInputStream fileIn = openFileInput("mytextfile.txt");
InputStreamReader inputRead = new InputStreamReader(fileIn);
BufferedReader reader = new BufferedReader(inputRead);

while ((str = reader.readLine()) != null){
    if(str.contains("##") && str.contains("userot"){
        while ((str = reader.readLine()) != null){
            if(str.contains("##"))
              break;
            else
              Log.d("TAG","output: " + str);
        }
        break;
     }
}

Use the below code, it may help you:-

String sAddr="";
        try {
            FileInputStream fileIn=c.openFileInput(LOGS_TEXT_FILE);
            InputStreamReader InputRead= new InputStreamReader(fileIn);

            char[] inputBuffer= new char[READ_BLOCK_SIZE];
            int charRead;

            while ((charRead=InputRead.read(inputBuffer))>0) {
                // char to string conversion
                String readstring=String.copyValueOf(inputBuffer,0,charRead);
                sAddr +=readstring;
            }
            InputRead.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
          String[]sAllAddrs = sAddr.split("\n"); // it would read the next line data and save it to a string.
          for(int i=0; i<sAllAddrs.length;i++){
                try {
                    //perform your logic to get those and write them...
                }catch(Exception e){
                    e.printStackTrace();
                }
 InputStream inputStream = getResources().openRawResource(R.raw.storydata);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), 8192);

    try {
        String line_1;
        while ((line_1 = reader.readLine()) != null)
        {
            while ((line_1 = reader.readLine()) != null)
            {
            if(line_1.contains("##") && line_1.contains("userot"))
            {
                while ((line_1 = reader.readLine()) != null)
                {
                    if(line_1.contains("##"))
                        break;
                    else
                        Toast.makeText(getBaseContext(), "" +line_1, Toast.LENGTH_LONG).show();
                }
                break;
            }


        }


        }
    } catch (IOException e)
    {
        throw new RuntimeException(e);
    }

line by line reader text file .... thanks for Hussain Alaidarous its work perfectly ....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!