How to get particular line to line from text file

2019-05-11 23:02发布

I have a text file, and i was able to read the full content and display it on a view. Sample example format for text file :-

## userdetail 

   [William]
   [Bits]
   6th cross road, City house.
   Rio.
   <051-22345690>|<002-22345690>

## calldetails

    income 22.
    out going 24.
    missed 21.


## Lorempsum
 Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
 when an unknown printer took a galley of type and scrambled it to make a type specimen book 


## userot 
 It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                  

........... ........... ...........

it like more 50 line

But my requirement is I need to get particular data from the field names.I can get "## userot" value of all unique ids. But I require the details of in-between data without hash-mapping. the data sometimes are also in the next line too. I Mean, on some case i have 5 lines, on other in have 10 lines of text. So I need all the lines below "## " till next "##" –

friend please give me solution for it ?

3条回答
仙女界的扛把子
2楼-- · 2019-05-11 23:18

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();
                }
查看更多
不美不萌又怎样
3楼-- · 2019-05-11 23:22
 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 ....

查看更多
虎瘦雄心在
4楼-- · 2019-05-11 23:31

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;
     }
}
查看更多
登录 后发表回答