read lines of file randomly java
import java.io.*;
import java.util.*;
public class GetLine {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Random Line : "+randomLine());
}
public static void randomLine() throws FileNotFoundException {
File f = new File("file.txt");
String result = null;
Random rand = new Random();
int n = 0;
Scanner sc = new Scanner(f);
while (sc.hasNext()) {
++n;
String line = sc.nextLine();
if (rand.nextInt(n) == 0)
result = line;
}
sc.close();
return result;
}
}
Also in Java:
- jsonobject to java object
- java previous permutation
- how to ask user for his location in android
- how to remove components from a JFRame java
- java set example
- print map java
- java list all non directory files in the directory
- jquery set data attribute value
- android java convert double to 2 decimal places
- connecting to h2 database from java
- java 11 initialize map
- setting up javafx in eclipse
- how to copy all files and subdirectories in directory in java
- java initialize string array
- demo java file
- java random integer
- @fortawesome/free-solid-svg-icons
- java check if args is empty
- java linked list functions
- non primitive data types in java
- how to create a draw Rectangle in java
- list java oracle
- change java version command line debian
- java code to save excel data to mysql