read lines of file randomly java
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:
- Title
- do statement java
- Category
- Java
- Title
- split with scquere braket in java
- Category
- Java
- Title
- jtable disable row selection
- Category
- Java
- Title
- Java loop throug gson JsonElement
- Category
- Java
- Title
- java iterator example
- Category
- Java
- Title
- java if different ways
- Category
- Java
- Title
- jbutton close jframe java
- Category
- Java
- Title
- Java If statemtn
- Category
- Java
- Title
- java how to show an array
- Category
- Java
- Title
- array in java stack overflow
- Category
- Java
- Title
- java mouseevent
- Category
- Java
- Title
- web app runtime for java
- Category
- Java
- Title
- java arraylist to array
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- summary of operator java
- Category
- Java
- Title
- Enums injava
- Category
- Java
- Title
- spigot repeating task
- Category
- Java
- Title
- how to convert string to array in java
- Category
- Java
- Title
- how to cut a certion part from a string in java
- Category
- Java
- Title
- string replace last character java
- Category
- Java
- Title
- splitting using regex java
- Category
- Java
- Title
- tostring() java example
- Category
- Java
- Title
- bukkit java get max players
- Category
- Java
- Title
- covariant type in java
- Category
- Java
- Title
- Java Read Files
- Category
- Java
- Title
- how to iterate hashmap java
- Category
- Java
- Title
- how to remove all special characters from a string in java
- Category
- Java
- Title
- how to calculate min, max and average and write the output into into a text file in java
- Category
- Java
- Title
- faire un timer en java
- Category
- Java
- Title
- springBoot
- Category
- Java
- Title
- abstract class in java
- Category
- Java
- Title
- counting the sequence of character in string java
- Category
- Java
- Title
- long input in JAVA
- Category
- Java
- Title
- creating thread in java example
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.security.MessageDigest$Delegate
- Category
- Java
- Title
- java string regexp replace
- Category
- Java
- Title
- HTTP FAILED: java.io.InterruptedIOException: interrupted
- Category
- Java
- Title
- java int passed by reference
- Category
- Java
- Title
- java create jframe
- Category
- Java
- Title
- when to use serializable in java
- Category
- Java
- Title
- how to pass enum in postman body
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- java length of matrix
- Category
- Java
- Title
- how to minimize jframe in java
- Category
- Java
- Title
- java import
- Category
- Java
- Title
- localdate to string java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java treeset
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- android java shared preferences remove key
- Category
- Java
- Title
- loop through java object
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- int to binary java
- Category
- Java
- Title
- java method to capitalize first letter
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- how to fix Index 2 out of bounds for length 2 when looping through an array in java
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- java instantiate collection with values
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- java 8 findany on null list
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- get cursor position in textarea java
- Category
- Java
- Title
- how to find the divisors of a number in java
- Category
- Java
- Title
- determine if a given binary tree is a valid bst
- Category
- Java
- Title
- how to check if a list is empty java
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- best companies to workk in
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- run spring boot application command line
- Category
- Java
- Title
- Java how to copy file
- Category
- Java
- Title
- java script zip function
- Category
- Java
- Title
- kotlin vs java
- Category
- Java
- Title
- java remove duplicates
- Category
- Java
- Title
- edit a txt file using java
- Category
- Java
- Title
- hashmaps java
- Category
- Java
- Title
- java check if int
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- replace regex group() java
- Category
- Java
- Title
- java how to initialize an array
- Category
- Java
- Title
- keep jframe on top
- Category
- Java
- Title
- get drawable with string android java
- Category
- Java
- Title
- find min in array java
- Category
- Java
- Title
- import classes from another project java
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- java 8 stream option
- Category
- Java
- Title
- lastindexof java
- Category
- Java
- Title
- generate all prime number less than n java
- Category
- Java
- Title
- how get started with LWJGL 3
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- and roid shape setCornerRadii
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- java read file text
- Category
- Java
- Title
- int java
- Category
- Java
- Title
- java interface
- Category
- Java
- Title
- how to create a list in java
- Category
- Java
- Title
- java android development get element by id
- Category
- Java