我有一个存储在手机“SaveTT.txt”中的文件。 文件中的每个单词都用空格分隔。 我希望从文件中检索每个单词并在单独的textViews中显示每个单词。 这个怎么做。 请帮忙
我能够使用以下代码将文件的内容检索为字符串
try { BufferedReader inputReader = new BufferedReader(new InputStreamReader( openFileInput("SAVETT.txt"))); StringBuffer stringBuffer = new StringBuffer(); while ((inputString = inputReader.readLine()) != null) { stringBuffer.append(inputString + "\n"); //EditText txt = (EditText) findViewById(R.id.temp); //txt.setText(inputString); } } catch (IOException e) { e.printStackTrace(); }使用此代码在此之后将整个字符串输入inputString。 我使用以下代码对字符串进行标记
StringTokenizer tokenizer = new StringTokenizer(inputString); String[] arr = new String[tokenizer.countTokens()]; while(tokenizer.hasMoreElements()) { arr[i]= tokenizer.nextToken(); i++; }使用上面的代码我试图在数组中保存eack令牌。 接下来,我尝试在Textviews中显示文本。
我不知道我的错误在哪里。 活动停止并显示NullPointer异常。
I have a file stored in my phone "SaveTT.txt" . Every word in the file is seperated by spaces. i wish to retrieve each word from the file and display every word in seperate textViews. How to do this. please help
I am able to retrieve the contents of the file into a string with the following code
try { BufferedReader inputReader = new BufferedReader(new InputStreamReader( openFileInput("SAVETT.txt"))); StringBuffer stringBuffer = new StringBuffer(); while ((inputString = inputReader.readLine()) != null) { stringBuffer.append(inputString + "\n"); //EditText txt = (EditText) findViewById(R.id.temp); //txt.setText(inputString); } } catch (IOException e) { e.printStackTrace(); }with this code get the entire string into inputString after this . i am tokenizing the string with the following code
StringTokenizer tokenizer = new StringTokenizer(inputString); String[] arr = new String[tokenizer.countTokens()]; while(tokenizer.hasMoreElements()) { arr[i]= tokenizer.nextToken(); i++; }with the above code i am trying to save eack token in an array. Next I try to display the text in Textviews.
I dont know where i am goig wrong. the activity is stopped and a NullPointer exception is displayed.
最满意答案
我自己想出了问题。 问题是inputString的值在Try catch块之外是不可访问的,因为它是在块内部设置的;
所以,如果我在try catch中编写字符串标记化块,它可以正常工作
I Figured out the problem myself. The problem was that the value of inputString is inaccessible from outside the Try catch block since it is set inside the block;
so if i write the string tokenisation block inside try catch it works perfectly fine
更多推荐
发布评论