[英]Android read line text file
I have a text file which contains all five-letter words in Turkish. 我有一个文本文件,其中包含土耳其语的所有五个字母的单词。 From there, I want to draw a random word.
从那里,我想画一个随机的词。 AssetManager screen printing on all of the word, but somehow I did not drop a single word.
AssetManager在所有单词上进行屏幕打印,但是不知何故我没有丢掉一个单词。 How can I use a loop?
如何使用循环?
I would be glad if you can help. 如果您能帮助我,我会很高兴。
Source: 资源:
public class MainActivity extends Activity {
Button degistir;
TextView kelime;
EditText ykelime;
Random rnd =new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
degistir=(Button)findViewById(R.id.btngiris);
kelime=(TextView)findViewById(R.id.kelime);
ykelime=(EditText)findViewById(R.id.giris);
AssetManager assetManager = getAssets();
// To load text file
InputStream input;
try {
input = assetManager.open("5HarfliKelimeler.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
String text = new String(buffer);
int sayi=rnd.nextInt(text.length());
kelime.setText(text);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
degistir.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
kelime.setText(ykelime.getText());
ykelime.setText(null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
firstly, if your file content won't be changed, then you have to count max line number and write it as constant: 首先,如果您的文件内容不会更改,那么您必须计算最大行数并将其写入常量:
private String getRandomLine() {
final int FILE_MAX_LINE_INDEX = 5000;//if your file has 5000 lines, and it's content never be changed
Random rnd = new Random();
final int lineIndex = rnd.nextInt(FILE_MAX_LINE_INDEX);
try {
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(getAssets().open("5HarfliKelimeler.txt")));
String s;
while ((s = lnr.readLine()) != null) {
if (lnr.getLineNumber() == lineIndex) {
lnr.close();
return s;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
or, if your content may be changed, and you don't know how many lines will be in file: 或者,如果您的内容可能更改,并且您不知道文件中将包含多少行:
private String getRandomLine() {
List<String> fiveLetterWords = new ArrayList<String>(5000);
try {
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(getAssets().open("5HarfliKelimeler.txt")));
String s;
while ((s = lnr.readLine()) != null) {
fiveLetterWords.add(s);
}
lnr.close();
} catch (IOException e) {
e.printStackTrace();
}
Random rnd = new Random();
return fiveLetterWords.get(rnd.nextInt(fiveLetterWords.size()));
}
Read file line by line and get all words to a list.( How to read line by line | check here ) 逐行读取文件并将所有单词都放入列表。( 如何逐行读取|在此处检查 )
Then, specify a random index for each button click. 然后,为每个按钮单击指定随机索引。 Finally, display the word from the list with generated index.
最后,从列表中显示单词并生成索引。 ( Index range must be from 0 to size of your list )
( 索引范围必须从0到列表的大小 )
-- I edited your code without any editor. -我没有任何编辑器即可编辑您的代码。 --
-
public class MainActivity extends Activity {
Button degistir;
TextView kelime;
EditText ykelime;
List<String> wordList=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
degistir=(Button)findViewById(R.id.btngiris);
kelime=(TextView)findViewById(R.id.kelime);
ykelime=(EditText)findViewById(R.id.giris);
AssetManager assetManager = getAssets();
// To load text file
InputStream input;
try {
input = assetManager.open("5HarfliKelimeler.txt");
wordList= new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(input , charset.forName("UTF-8")));
String line = "";
while ((line = reader.readLine()) != null) {
wordList.add(line);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
degistir.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(wordList!=null){
Random rnd =new Random();
Integer generatedIndex = rnd.nextInt(wordList.size());
String selectedWord=wordList.get(generatedIndex);
kelime.setText(selectedWord);
// ykelime.setText(null);
}
else{
//DISPLAY MSG ETC.
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.