【问题标题】:How to get particular line to line from text file and display array list in android如何从文本文件中获取特定的行并在android中显示数组列表
【发布时间】:2016-01-10 19:52:23
【问题描述】:

我有一个文本文件,我能够阅读全部内容并将其显示在视图上。 文本文件的示例格式:-

## 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.                  

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

喜欢50多行

但我的要求是我需要从字段名称中获取特定数据。我可以获得所有唯一 ID 的“## userot”值。但是我需要没有哈希映射的中间数据的详细信息。 数据有时也在下一行。我的意思是,在某些情况下,我有 5 行, 在其他有 10 行文本。所以我需要“##”下面的所有行直到下一个“##”——

朋友请给我解决办法?

【问题讨论】:

  • 您是否尝试过逐行读取文件直到找到## userot,然后读取行直到找到以## 开头的另一行?
  • 这是 ## 独一无二的每个

标签: java android arrays arraylist text-files


【解决方案1】:

使用下面的代码,它可以帮助你:-

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();
                }

【讨论】:

    【解决方案2】:

    下面的解决方案是找到'## userot'之后的所有行直到下一个##

    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;
         }
    }
    

    【讨论】:

    • 这个代码行的朋友,为什么要让你在“文件名”中得到两次简单的告诉我..
    • 我有错误“in”文件名..如何初始化文件名​​朋友,否则我使用这个 BufferReader 为“reader.readLine”
    • 使用你想要的任何东西,你只需要将“readline(filename)”更改为可以工作的东西,然后这段代码应该可以顺利运行。
    • ok ...朋友我再次更改此readline(文件名)会发生错误,我需要运行此方法tutorialspoint.com/… ......示例页面创建并运行此方法
    • 好的,我编辑了这个,所以你可以通过更改文件名来运行它。
    【解决方案3】:
     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);
        }
    

    逐行阅读器文本文件....感谢 Hussain Alaidarous 完美的工作....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多