I think the easiest way to do this is Scanner .
Scanner sc = new Scanner(new File("donal.txt"), "UTF-8"); sc.useDelimiter("\n[ \t]*\n"); List<String> result = new ArrayList<String>(); int lineCount = 0; while (sc.hasNext()) { String line = sc.next(); System.out.printf("%n%d:%n%s%n", ++lineCount, line); result.add(line); } System.out.printf("%n%d paragraphs found.%n", lineCount);
The first and last paragraphs will actually be the header and footer; I donβt know what you want to do with it.
For readability, I assume that the line separator is always a Unix \n style, but to be safe, you must enable the Windows \r\n style and the older Mac \r style as Well. This will make the regex:
"(?:\r\n|[\r\n])[ \t]*(?:\r\n|[\r\n])
source share