Java String count line
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] argv) throws IOException{ System.out.println(countLinesBuffer("\n\n\n\n\n")); }//from w w w .j av a2 s. c om /** * Count the number of lines in the buffer. * * @param strData * String * @return int * @throws IOException */ public static int countLinesBuffer(final String strData) throws IOException { if (strData != null) { final ByteArrayInputStream stream = new ByteArrayInputStream(strData.getBytes()); final BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); int lineNumber = 0; String data = ""; do { data = reader.readLine(); if (data != null) { lineNumber++; } // End of the if // } while (data != null); reader.close(); return lineNumber; } return 0; } }