The Java file manipulation code snippet instance implements the function of counting the number of letters in the file

  • 2020-04-01 02:46:10
  • OfStack


String fileName = "D:/date.java.bak";
        // String fileName = "D:/test.qqq";
        String line;
        int i = 0, j = 0, f = 0, k = 0;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            line = in.readLine();
            while (line != null) {
                // System.out.println(line);
                char c[] = line.toCharArray();
                for (int i1 = 0; i1 < c.length; i1++) {
                    //If it's a letter
                    if (Character.isLetter(c[i1]))
                        i++;
                    //If it's a number
                    else if (Character.isDigit(c[i1]))
                        j++;
                    //Is a space
                    else if (Character.isWhitespace(c[i1]))
                        f++;
                }
                line = in.readLine();
                k++;
            }
            in.close();
            System.out
                    .println(" The letter :" + i + ", digital :" + j + ", The blank space :" + f + ", The number of rows :" + k);
        } catch (IOException e) {
            e.printStackTrace();
        }


Related articles: