Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java InputStreamReader并从.txt文件中读取随机行_Java_Android_Inputstreamreader - Fatal编程技术网

Java InputStreamReader并从.txt文件中读取随机行

Java InputStreamReader并从.txt文件中读取随机行,java,android,inputstreamreader,Java,Android,Inputstreamreader,我有一个方法让我的应用程序从文本文件中随机读取一行并返回它。我使用randTxt()读取并返回txt文件中的随机行。 但它每次只显示同一行(第1行) public String randTxt(){ // Read in the file into a list of strings InputStreamReader inputStream = new InputStreamReader(getResources().openRawResource(R.raw.randomstuff

我有一个方法让我的应用程序从文本文件中随机读取一行并返回它。我使用randTxt()读取并返回txt文件中的随机行。 但它每次只显示同一行(第1行)

public String randTxt(){

  // Read in the file into a list of strings
  InputStreamReader inputStream = new InputStreamReader(getResources().openRawResource(R.raw.randomstuff));
  //ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

  String theLine="";
  int i;
  try {
    i = inputStream.read();
    while (i != -1) {
      i = inputStream.read();
    }
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

  LineNumberReader  rdr = new LineNumberReader(inputStream);
  int numLines = 30;
  Random r = new Random();
  rdr.setLineNumber(r.nextInt(numLines));

  try {
    theLine = rdr.readLine();
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

  return theLine;
}
我怎样才能修好它?有人能解释一下我代码中的错误吗?

我认为Random()函数返回的值介于0和1之间。因此,您可能必须将其与100相乘才能得到整数值。甚至可以考虑一个MOD“你的上限”操作来保证你最终得到的索引在0和你的上限

之间。 在setLineNumber()方法中使用由此计算的索引

编辑: 正如john所说,我们可以使用Random()对象得到整数。

我认为Random()函数返回的值介于0和1之间。因此,您可能必须将其与100相乘才能得到整数值。甚至可以考虑一个MOD“你的上限”操作来保证你最终得到的索引在0和你的上限

之间。 在setLineNumber()方法中使用由此计算的索引

编辑:
正如john所说,我们可以使用Random()对象获取整数。

inputStream.read不返回行号。它返回读取的字节。这不是你逐行阅读的方式。要逐行读取,应该使用缓冲读取器的readLine方法。此时,将其全部读入本地数组并使用该数组随机获取条目可能比使用行号读取器更容易。

inputStream.read不返回行号。它返回读取的字节。这不是你逐行阅读的方式。要逐行读取,应该使用缓冲读取器的readLine方法。此时,将所有内容读入本地数组并使用该数组随机获取条目可能比使用行号读取器更容易。

以下是使用BufferedReader执行所需操作的框架。在这种情况下,不需要将值存储在临时数组中

InputStreamReader inputStream = new InputStreamReader
  (getResources().openRawResource(R.raw.randomstuff));
BufferedReader br = new BufferedReader(inputStream);
int numLines = 30;
Random r = new Random();
int desiredLine = r.nextInt(numLines);

String theLine="";
int lineCtr = 0;
while ((theLine = br.readLine()) != null)   {
  if (lineCtr == desiredLine) {
    break;
  }
  lineCtr++;
 }
...
Log.d(TAG, "Magic line is: " +theLine);

下面是使用BufferedReader执行所需操作的框架。在这种情况下,不需要将值存储在临时数组中

InputStreamReader inputStream = new InputStreamReader
  (getResources().openRawResource(R.raw.randomstuff));
BufferedReader br = new BufferedReader(inputStream);
int numLines = 30;
Random r = new Random();
int desiredLine = r.nextInt(numLines);

String theLine="";
int lineCtr = 0;
while ((theLine = br.readLine()) != null)   {
  if (lineCtr == desiredLine) {
    break;
  }
  lineCtr++;
 }
...
Log.d(TAG, "Magic line is: " +theLine);

您已经得到了如何修复代码的答案,但没有解释为什么我们的原始代码不起作用

setLineNumber(int)不会转到实际行,它只是更改您调用当前行的数字

因此,假设您读了两行,getLineNumber()现在将返回2(它从0开始,每次遇到换行时增加1)。如果现在设置LineNumber(10),则getLineNumber()将返回10。读取另一行(第三行)将导致getLineNumber()返回11


这在中有描述。

您已经得到了如何修复代码的答案,但没有解释为什么我们的原始代码不起作用

setLineNumber(int)不会转到实际行,它只是更改您调用当前行的数字

因此,假设您读了两行,getLineNumber()现在将返回2(它从0开始,每次遇到换行时增加1)。如果现在设置LineNumber(10),则getLineNumber()将返回10。读取另一行(第三行)将导致getLineNumber()返回11

这在。

公共字符串getRandomLine(字符串fileLoc)抛出IOException中进行了描述
{
BufferedReader reader=new BufferedReader(new FileReader(fileLoc));
ArrayList行=新的ArrayList();
字符串行=null;
而((line=reader.readLine())!=null)
行。添加(行);
//从列表中随机选择一个
返回lines.get(newrandom().nextInt(lines.size());
}
公共字符串getRandomLineOpt(字符串fileLoc)引发IOException
{
文件f=新文件(fileLoc);
RandomAccessFile rcf=新的RandomAccessFile(f,“r”);
long rand=(long)(new Random().nextDouble()*f.length());
rcf.seek(兰特);
rcf.readLine();
返回rcf.readLine();
}
公共字符串getRandomLine(字符串fileLoc)引发IOException
{
BufferedReader reader=new BufferedReader(new FileReader(fileLoc));
ArrayList行=新的ArrayList();
字符串行=null;
而((line=reader.readLine())!=null)
行。添加(行);
//从列表中随机选择一个
返回lines.get(newrandom().nextInt(lines.size());
}
公共字符串getRandomLineOpt(字符串fileLoc)引发IOException
{
文件f=新文件(fileLoc);
RandomAccessFile rcf=新的RandomAccessFile(f,“r”);
long rand=(long)(new Random().nextDouble()*f.length());
rcf.seek(兰特);
rcf.readLine();
返回rcf.readLine();
}

在调试器中单步执行代码时,您学到了什么?在调试器中单步执行代码时,您学到了什么?但我使用的是nextInt(n),它返回0到n之间的整数。否,随机。nextInt(int)返回整数。您不需要对任何内容进行mod或乘法。但我使用的是nextInt(n),它返回一个介于0到n之间的整数。不,随机。nextInt(int)返回整数。您不需要对任何内容进行mod或乘法。我的文本文件有长字符串。我认为将其加载到数组中不是一个好主意。文件中的行数是静态的还是动态的?如果它是静态的,你可以用它作为基础。既然这是android,为什么不把文件的内容放在sqllite数据库中呢?再看一看,如果你想看到一种更快的方法来计算文件中的行数:它是静态的。。因为我只是一个初学者,我根本没有使用过sqlite。我的文本文件有很长的字符串。我认为将其加载到数组中不是一个好主意。文件中的行数是静态的还是动态的?如果它是静态的,你可以用它作为基础。既然这是android,为什么不把文件的内容放在sqllite数据库中呢?再看一看,如果你想看到一种更快的方法来计算文件中的行数:它是静态的。。因为我只是个初学者,我还没有