不同的方法。使用字符串查找行数,单词数和字符数:
public static void main(String[] args) throws IOException { //counters int charsCount = 0; int wordsCount = 0; int linesCount = 0; Scanner in = null; File selectedFile = null; JFileChooser chooser = new JFileChooser(); // choose file if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { selectedFile = chooser.getSelectedFile(); in = new Scanner(selectedFile); } while (in.hasNext()) { String tmpStr = in.nextLine(); if (!tmpStr.equalsIgnoreCase("")) { String replaceAll = tmpStr.replaceAll("\s+", ""); charsCount += replaceAll.length(); wordsCount += tmpStr.split(" ").length; } ++linesCount; } //display the count of characters, words, and lines System.out.println("# of chars: " + charsCount); System.out.println("# of words: " + wordsCount); System.out.println("# of lines: " + linesCount); in.close(); }注意:
对于其他编码样式,请使用
new Scanner(new File(selectedFile), "###");代替
newScanner(selectedFile);。
###是需要设置的字符。引用这个和维基



