Below is the text file, in which I want to read each line in some random way by printing each number in the line in some random order. I can read each line one by one and then print number corresponding to each line in a sequential order, But is there any way we can read line in some random way so that I can print all the numbers in some random order.
Line1 1 1116 2090 100234 145106 76523
Line2 1 10107 1008 10187
Line3 1 10107 10908 1109
Any suggestions will be appreciated. Below is the code that I wrote it will read the line sequentially.
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing\\Test.txt"));
while ((sCurrentLine = br.readLine()) != null) {
String[] s = sCurrentLine.split("\\s+");
for (String split : s) {
if(split.matches("\\d*"))
System.out.println(split);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
If you mean to rearrange the order of each line, you can use Collections.shuffle:
This will print the lines sequentially, but the numbers in each line will be shuffled.
If mean to shuffle the order of the lines as well, just add each line to an
ArrayList<List<String>>, shuffle the ArrayList, then shuffle each line: