Java 我在资产文件夹中有一个文本文件。如何从该文本文件中访问随机句子?
我在资产文件夹中创建了灵感文本文件。在这个灵感文件中,大约有20个句子。那么,我如何随机选取任何一个句子并显示在文本视图中呢。现在它显示了全部20个句子。这是代码。谁能帮我一下吗Java 我在资产文件夹中有一个文本文件。如何从该文本文件中访问随机句子?,java,android,Java,Android,我在资产文件夹中创建了灵感文本文件。在这个灵感文件中,大约有20个句子。那么,我如何随机选取任何一个句子并显示在文本视图中呢。现在它显示了全部20个句子。这是代码。谁能帮我一下吗 tv_text1 = (TextView) findViewById(R.id.textView3); String text = ""; try { InputStream is = getAssets().open("inspiration.txt"); int size = is.availabl
tv_text1 = (TextView) findViewById(R.id.textView3);
String text = "";
try {
InputStream is = getAssets().open("inspiration.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
} catch (IOException ex) {
ex.printStackTrace();
}
tv_text1.setText(text);
要从输入流读取文本,请执行以下操作:
InputStream is = getAssets().open("inspiration.txt");
List<String> doc =
new BufferedReader(new InputStreamReader(resource,
StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
要从输入流读取文本,请执行以下操作:
InputStream is = getAssets().open("inspiration.txt");
List<String> doc =
new BufferedReader(new InputStreamReader(resource,
StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
试着这样做:
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("inspiration.txt")));
// do reading, usually loop until end of file reading
String mLine;
List<String> listLines = new ArrayList<>();
while ((mLine = reader.readLine()) != null) {
//process line
listLines.add(mLine);
}
if (listLines.size() > 0) {
Random rand = new Random();
int randomNumber = rand.nextInt(listLines.size());
String randomLine = listLines.get(randomNumber);
.......
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
BufferedReader读取器=null;
试一试{
读卡器=新的BufferedReader(
新的InputStreamReader(getAssets().open(“inspiration.txt”);
//进行读取,通常循环直到文件读取结束
弦线;
List listLines=new ArrayList();
而((mLine=reader.readLine())!=null){
//生产线
listLines.add(mLine);
}
如果(listLines.size()>0){
Random rand=新的Random();
int randomNumber=rand.nextInt(listLines.size());
String randomLine=listLines.get(randomNumber);
.......
}
}捕获(IOE异常){
//记录异常
}最后{
if(读卡器!=null){
试一试{
reader.close();
}捕获(IOE异常){
//记录异常
}
}
}
试着这样做:
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("inspiration.txt")));
// do reading, usually loop until end of file reading
String mLine;
List<String> listLines = new ArrayList<>();
while ((mLine = reader.readLine()) != null) {
//process line
listLines.add(mLine);
}
if (listLines.size() > 0) {
Random rand = new Random();
int randomNumber = rand.nextInt(listLines.size());
String randomLine = listLines.get(randomNumber);
.......
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
BufferedReader读取器=null;
试一试{
读卡器=新的BufferedReader(
新的InputStreamReader(getAssets().open(“inspiration.txt”);
//进行读取,通常循环直到文件读取结束
弦线;
List listLines=new ArrayList();
而((mLine=reader.readLine())!=null){
//生产线
listLines.add(mLine);
}
如果(listLines.size()>0){
Random rand=新的Random();
int randomNumber=rand.nextInt(listLines.size());
String randomLine=listLines.get(randomNumber);
.......
}
}捕获(IOE异常){
//记录异常
}最后{
if(读卡器!=null){
试一试{
reader.close();
}捕获(IOE异常){
//记录异常
}
}
}
如果句子之间用a分隔,您可以这样做。
未经测试,您必须将randomGenerator替换为在这两个参数之间提供随机数的东西。在.split之后,您有一个包含所加载文本的所有行的数组
String splits[]=text.split('\n');
int random=randomGenerator(0,splits.length);
tv_text1.setText(splits[random]);
这实际上更像是一个普通的java问题,而不是一个android问题。这是作业吗?如果句子之间用a分隔,您可以这样做。 未经测试,您必须将randomGenerator替换为在这两个参数之间提供随机数的东西。在.split之后,您有一个包含所加载文本的所有行的数组
String splits[]=text.split('\n');
int random=randomGenerator(0,splits.length);
tv_text1.setText(splits[random]);
这实际上更像是一个普通的java问题,而不是一个android问题。这是一项作业吗?inspiration.txt中的句子是如何列出的?在没有索引的单独行中。使用缓冲区读取器逐行读取。将其存储在字符串数组中。-使用random()获取句子。1)将该文件中的句子读入列表。2) 生成范围为0.列表长度为-1的随机整数。3) 通过生成索引从列表中获取句子如何显示inspiration.txt中列出的句子?在没有索引的单独行中。使用缓冲区读取器逐行读取。将其存储在字符串数组中。-使用random()获取句子。1)将该文件中的句子读入列表。2) 生成范围为0.列表长度为-1的随机整数。3) 通过生成索引从列表中获取句子Hanks webianks,将尝试。感谢webianks,将尝试。感谢Suseendran的帮助。我会试试的。谢谢苏珊德兰的帮助。我会试试的。谢谢拉西·金努恩。谢谢拉西·金努恩。