【问题标题】:read from file and display random line从文件中读取并显示随机行
【发布时间】:2011-09-29 18:59:26
【问题描述】:

我试图让我的 android 应用程序从文本文件中读取并随机选择一个条目然后显示它,我该怎么做呢?我想我必须使用缓冲区读取器或输入流命令,但我不知道如何使用这些,我试过谷歌搜索但没有找到太多帮助。

据我所知(在一些帮助下)我必须阅读文本文件,将其添加到字符串中?并使用以下命令随机选择一个条目

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

我该怎么做呢? :\ 我对所有这些 bufferreader 等东西都很陌生。

【问题讨论】:

标签: java android file-io io


【解决方案1】:

您在这里询问 2 种不同的操作。不要把问题混为一谈。您想知道如何:

  1. 将文件从磁盘读取到一组字符串中。
  2. 从一组字符串中随机选择一个字符串。

    // 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()));
    

【讨论】:

  • 顺便说一句,为了清楚起见,我忽略了这里所有的 try/catch。
【解决方案2】:

这里有一些从文本文件中读取的快速代码。您需要修改它以在行尾拆分,显然解决 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)
    {
    }
}

【讨论】:

    【解决方案3】:
    /*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();
    }
    }
    

    【讨论】:

    • 您确定“从 txt 文件读取”是有效的类名(带空格)。我没有更改它,因为我不想更改您的代码
    • 从 txt 文件读取肯定是一个无效的类名 ;)
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多