统计某个文件中出现的字符个数,数字个数,空格个数,总共有多少行?

来源:互联网 发布:网络创世纪单机版 编辑:程序博客网 时间:2024/05/17 18:01
package com.xmobo.mapp.ofcard.test;import java.io.FileInputStream;public class Test {/** * Get File Infos *  * @param path * @return * @throws Exception */public static String[] getFileInfos(String path) throws Exception {int chacracter = 0;int words = 0;int workspace = 0;int enter = 0;FileInputStream file = new FileInputStream(path);byte[] temp = new byte[1024];int size = 0;while ((size = file.read(temp)) != -1) {for (int i = 0; i < size; i++) {byte tmp = temp[i];if ((tmp >= 'a' && tmp <= 'z') || (tmp >= 'A' && tmp <= 'Z')) {chacracter++;} else if (tmp >= '0' && tmp <= '9') {words++;} else if (tmp == ' ') {workspace++;} else if (tmp == '\n') {enter++;}}}file.close();// {字符,数字,空格,回车}String[] strArray = { String.valueOf(chacracter),String.valueOf(words), String.valueOf(workspace),String.valueOf(enter) };return strArray;}// mainpublic static void main(String[] args) throws Exception {String path = "config1.xml";System.out.println(Test.getFileInfos(path)[2]);}}