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();
}
这给了我所需的行但我无法从该行获得特定的单词 并将其添加到android布局中的textview。请帮帮我。
我的预期输出是显示1,2,5,6行的单词,每行留下前10个字母。
答案 0 :(得分:0)
这将为您提供所有单词(包括空格)的列表:
String[] words = line.split(" ");
你可以像这样访问每个单词:
foreach(String word in words) {
System.out.println(word.trim()); // trim gets rid of the space from the beginning & end
}
答案 1 :(得分:0)
我不确定你要完成什么,如果你想要一个你已经知道的特定单词或者特定索引值中的任何单词。但你可以随时查看:
if(line.contains("word"){
fBldGp.setText("word");
}
但除此之外,line.substring会给你一个从特定索引开始的单词