Reputation: 2485
I'm looking for an efficient way to split a text file into a set of ArrayList. The text file is a Thread dump and I'd like to create a List for every single Thread. Every thread is separated by an empty line. For example, taken the following file:
"management-handler-thread - 66" prio=10 tid=0x00007fe960111000 nid=0x4cea waiting on condition [0x00007fe96c25c000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006019cbbd0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2082)
at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
"management-handler-thread - 65" prio=10 tid=0x00007fe968185800 nid=0x4ce9 waiting on condition [0x00007fe96c35d000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006019cbbd0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2082)
at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
The first List should contain:
"management-handler-thread - 66" prio=10 tid=0x00007fe960111000 nid=0x4cea waiting on condition [0x00007fe96c25c000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
. . .
And the second List should contain:
"management-handler-thread - 65" prio=10 tid=0x00007fe968185800 nid=0x4ce9 waiting on condition [0x00007fe96c35d000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
. . . .
What would you recommend to do that ? (Iteration, Regular expression ..) Thanks
Upvotes: 0
Views: 612
Reputation: 5973
No need for regular expressions, just read the file line by line...
List<List<String>> dumpedThreads = new ArrayList<>();
try (FileReader fr = new FileReader("path/to/thread-dump.txt")) {
try (BufferedReader br = new BufferedReader(fr)) {
List<String> thisThread = null;
for (String line = br.readLine(); line != null; line = br.readLine()) {
if (line.trim().length() == 0) {
thisThread = null;
} else {
if (thisThread == null) {
thisThread = new ArrayList<>();
dumpedThreads.add(thisThread);
}
thisThread.add(line);
}
}
}
}
Upvotes: 3