import java.io.*; class Test { public static void main(String args[]) { String path = "sample.txt"; try { BufferedReader br = new BufferedReader(new FileReader(path)); int charCount = 0; int lineCount = 0; int wordCount = 0; String line; while ((line = br.readLine()) != null) { charCount += line.length(); lineCount++; String[] words = line.split("\\s+"); wordCount += words.length; } br.close(); System.out.println("Number of characters: " + charCount); System.out.println("Number of words: " + wordCount); System.out.println("Number of lines: " + lineCount); } catch (IOException e) { System.out.println(e); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter