开发者

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? 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.

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. 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.

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();
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜