从Android中的文本文件中的特定行获取特定单词

时间:2021-08-30 00:00:01
String line = "";
        int lineNo;
        try 
        {
            File file = new File("/sdcard/mysdfile.txt");
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                //char[] inputBuffer1 = new char[READ_BLOCK_SIZE];
                for (lineNo = 1; lineNo < 20; lineNo++) 
                {
                     if (lineNo == 1)
                     {
                         line = br.readLine();
                         etFName.setText(line);


                     }else//{br.readLine();}   
                     if (lineNo == 2)
                     {
                             line = br.readLine();
                             char c = line.charAt(0);
                             //char c =line.charAt(0);
                             String s1 = Character.toString(c); 
                             //String Str1 = line;
                             //String bloodgroup = String.copyValueOf(Str1, 10,13);
                             fBldGp.setText(s1);
                     }else//{br.readLine();} 
                     if (lineNo == 5)
                     {
                         line = br.readLine();
                         char c = line.charAt(0);
                         String s1 = Character.toString(c);
                         etMName.setText(s1);                        
                     }else
                     if (lineNo == 6)
                     {
                         line = br.readLine();
                         char c = line.charAt(0);
                         String s1 = Character.toString(c);
                         mBldGp.setText(s1);                         
                     }else br.readLine();
                }
        } 
        catch (IOException e)
        {
                e.printStackTrace();
        }

This gives me the required line but i am unable to get the specific word from that line and add it to the textview in android layout. Please help me.

这给了我所需的行但我无法从该行获取特定的单词并将其添加到android布局中的textview。请帮我。

My expected output is to display the word of the line 1,2,5,6th line leaving first 10 alphabets of every line.

我的预期输出是显示第1,2,5,6行的字,每行留下前10个字母。

2 个解决方案

#1


0  

I'm not really sure what you're trying to accomplish, if you want a specific word you already know or whatever word is in a specific index value. But you can always check something like:

如果你想要一个你已经知道的特定单词或者特定索引值中的任何单词,我真的不确定你要完成什么。但你可以随时查看:

if(line.contains("word"){
     fBldGp.setText("word");
}

But other than that then line.substring would give you a word starting at a specific index

但除此之外,line.substring会给你一个从特定索引开始的单词

#2


0  

This will give you list of all words(including space):

这将为您提供所有单词(包括空格)的列表:

String[] words = line.split(" ");

You can access each word like this:

您可以像这样访问每个单词:

foreach(String word in words) {
     System.out.println(word.trim()); // trim gets rid of the space from the beginning & end
}

#1


0  

I'm not really sure what you're trying to accomplish, if you want a specific word you already know or whatever word is in a specific index value. But you can always check something like:

如果你想要一个你已经知道的特定单词或者特定索引值中的任何单词,我真的不确定你要完成什么。但你可以随时查看:

if(line.contains("word"){
     fBldGp.setText("word");
}

But other than that then line.substring would give you a word starting at a specific index

但除此之外,line.substring会给你一个从特定索引开始的单词

#2


0  

This will give you list of all words(including space):

这将为您提供所有单词(包括空格)的列表:

String[] words = line.split(" ");

You can access each word like this:

您可以像这样访问每个单词:

foreach(String word in words) {
     System.out.println(word.trim()); // trim gets rid of the space from the beginning & end
}