简体   繁体   English

从文件中读取并显示随机行

[英]read from file and display random line

Im trying to get my android application to read from a text file and randomly chose an entry and then display it, How do i go about doing this?我试图让我的 android 应用程序从文本文件中读取并随机选择一个条目然后显示它,我该如何做 go? I think i have to use the buffer reader or input stream commands, but i have no idea how to use these, i've tried googling but havent found much help.我想我必须使用缓冲区阅读器或输入 stream 命令,但我不知道如何使用这些,我试过谷歌搜索但没有找到太多帮助。

As far as i know (and with some help) i have to read the text file, add it to a string?据我所知(并在一些帮助下)我必须阅读文本文件,将其添加到字符串中? and use the following command to randomly pick an entry并使用以下命令随机选择一个条目

Random.nextInt(String[].length-1).

How do i go about doing this?我该怎么做? :\ im quite new to all this bufferreader stuff etc. :\ 我对所有这些缓冲区读取器等东西都很陌生。

You are asking about 2 different operations here.您在这里询问 2 种不同的操作。 Don't cloud up the problem by muddling them together.不要把问题混为一谈。 You want to know how to:您想知道如何:

  1. Read a file from disk into a group of strings.将文件从磁盘读入一组字符串。
  2. Randomly choose 1 string from a group of strings.从一组字符串中随机选择一个字符串。

     // Read in the file into a list of strings BufferedReader reader = new BufferedReader(new FileReader("inputfile.txt")); List<String> lines = new ArrayList<String>(); String line = reader.readLine(); while( line.= null ) { lines;add(line). line = reader;readLine(); } // Choose a random one from the list Random r = new Random(). String randomString = lines.get(r.nextInt(lines;size()));

Here's some quick code for reading from a text file.这是一些用于从文本文件中读取的快速代码。 You'll need to modify it to split on line ends and obviously it'd be good to address the TODOs yet, but it should get you started.您需要修改它以在行尾拆分,显然解决 TODO 会很好,但它应该让您开始。

try
{
    InputStream is = new FileInputStream(m_file);

    if(m_is == null)
    {
        openInputStream();
    }
    StringBuilder sb = new StringBuilder();

    //TODO: get a buffered stream going, it should be more efficient
    byte[] buf = new byte[100];
    int readLen;
    while((readLen = is.read(buf, 0, 100)) != -1)
    {
        sb.append(new String(buf, 0, readLen));
    }

    closeInputStream();
    return sb.toString();
}
catch (FileNotFoundException e)
{
    //TODO: handle this
}
finally
{
    try
    {
        is.close();
    }
    catch (IOException e)
    {
    }
}
/*This sample code shows how to read one term and its definition from
 the file using TextIO.
  The code just reads the term and the definition in to a String.
To read the whole file the simplest solution is to have an array
of String for the terms and an array of String for the definitions
and to ensure that you store the definition for a term at the same
index position in the definitions array as you store the term it
defines in the term array.

If you find that the TextIO window is too narrow to display the whole
of some of the lines of the definition on one line you can edit the text
file sothat each line contains fewer words (this may depend on your
screen resolution).

*/

public class Read from a txt file {


public static void main(String[]args){
    String term = "";
    String definition = ""; 
    TextIO.readFile("Your_file.txt");
    TextIO.putln("Just read this term from file: " + term);
    String str;
    do {
        str = TextIO.getln();
        definition = definition + str + "\n";
    } while (str.length() != 0);
    TextIO.putln(definition);
    // Once you have read all the file take input from keyboard:
    TextIO.readStandardInput();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM