简体   繁体   中英

How to read random line from remote txt file?

I can not understand how to make a random string was taken from a remote file Now i have this construct. It show me with Toast first line of file. How can i read a random line?

public void bClick(View view) throws IOException {
   String test = new Scanner(new URL("http://www.somesite.us/1.txt").openStream()).
        nextLine();
   Toast.makeText(this, test, Toast.LENGTH_SHORT).show();
}

You can try to call nextLine() until you reach the line that you want

Scanner scanner = new Scanner(new URL("http://www.somesite.us/1.txt").openStream())
while (scanner.hasNextLine()) {
            String test = scanner.nextLine();
             Toast.makeText(this, test, Toast.LENGTH_SHORT).show();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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