出現数と周波数でjavaIOは、英語の単語のテキストフローや手紙を読んで

まず、英語の文字の数はアルファベットの降順テキストおよび出力の割合を読んでいるように見えます

出典;

パッケージの合計。

輸入java.io.BufferedReader;
輸入java.io.FileReader;
インポートにjava.io.IOException;
輸入java.util.Arrays。

パブリック クラスStatistics_letter {

    パブリック 静的 ボイドメイン(文字列[]引数)がスローにIOException {
         // TODO自動生成方法スタブ 
        FileReaderのFR = 新しい FileReaderの( "A.TXTを" )。
        BufferedReaderのBUFR = 新しいをBufferedReader(FR)。
        StringBufferの列str = 新しいStringBufferを();
        文字列のライン = nullをしばらく((ライン= bufr.readLine())!= nullの){
            str.append(ライン);
        }
        bufr.close();
        
        ダブル capitalletter [] = 新しい ダブル [26 ]。
        ダブル lowercaseletter [] = 新しい ダブル [26 ]。
        int型のカウント= 0 ;
        以下のためにINT ; I <str.length(); iが0 = I ++ ){
             チャー CH = str.charAt(I)。
            もし(CH> = 'A' && CH <= 'Z' || CH> = 'A' && CH <= 'Z' ){
                 ためINT J = 0であり、j <26; J ++ ){
                     場合(CH ==」 A '+ J)
                    capitalletter [J] ++ ;
                }
                int型のk = 0; K <26; K ++ ){
                     場合(CH == 'A' + K)
                        lowercaseletter [K] ++ ;
                }
                カウント ++ ;    
            }
        }
        
        二重コマンドpercentage1 [] = 新しい ダブル [52 ]。
        二重 percentage2に[] = 新しい ダブル [52 ]。
        以下のためにINT iは= 0; I <26; I ++ ){
            コマンドpercentage1 [I] = capitalletter [I] / カウント。
            percentage2に[I] = コマンドpercentage1 [I]。
        }
        以下のためにINT iは= 26、I <52; I ++ ){
            コマンドpercentage1 [I] = lowercaseletter [I-26] / カウント。
            percentage2に[I] = コマンドpercentage1 [I]。
        }
        Arrays.sort(コマンドpercentage1)。
        以下のためにINT I 51 =; I> = 0; i-- ){
             int型の最大値= 0 INT J = 0であり、j <52; J ++ ){
                 場合(percentage2に[J] == コマンドpercentage1 [i])と
                    最大 = J;
            }
            もし(最大> = 26 
                System.out.print(((CHAR)( '' + MAX-26))+ ":" )。
            
            System.out.print(((文字)( 'A' + MAX))+ ":" );
            System.out.println(String.Formatの( "%の.2f"、コマンドpercentage1 [I] * 100)+ '%' );
        }
        System.out.println(「英文字の合計数:」+ COUNT)。
    }
}

スクリーンショットの営業成績:

 

 

 

第二に、英語の単語やプレスのテキストを読んで出力を降順で表示されます

出典:

パッケージの合計。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;


public class Statistics_words {
    public Map<String, Integer> map1 = new HashMap<String, Integer>();

    public static void main(String arg[]) throws IOException {
        String sz[];
        Integer num[];
        final int MAXNUM = 20; 

        sz = new String[MAXNUM + 1];
        num = new Integer[MAXNUM + 1];
        Statistics_words Statistics_words = new Statistics_words();
        int account = 1;
        // Vector<String> ve1=new Vector<String>();
            Statistics_words.textImport();
        System.out.println("文本出现单词的次数情况为:");
        int g_run = 0;

        for (g_run = 0; g_run < MAXNUM + 1; g_run++) {
            account = 1;
            for (Map.Entry<String, Integer> it : Statistics_words.map1.entrySet()) {
                if (account == 1) {
                    sz[g_run] = it.getKey();
                    num[g_run] = it.getValue();
                    account = 2;
                }
                if (account == 0) {
                    account = 1;
                    continue;
                }
                if (num[g_run] < it.getValue()) {
                    sz[g_run] = it.getKey();
                    num[g_run] = it.getValue();
                }
                // System.out.println("英文单词: "+it.getKey()+" 该英文单词出现次数: "+it.getValue());
            }
            Statistics_words.map1.remove(sz[g_run]);
        }
        int g_count = 1;
        String tx1 = new String();
        for (int i = 0; i < g_run; i++) {
            if (sz[i] == null)
                continue;
            if (sz[i].equals(""))
                continue;
            tx1 += "出现次数第" + (g_count) + "多的单词为:" + sz[i] + "\t\t\t出现次数: " + num[i] + "\r\n";
            System.out.println("出现次数第" + (g_count) + "多的单词为:" + sz[i] + "\t\t\t出现次数: " + num[i]);
            g_count++;
        }
            Statistics_words.textExport(tx1);

    }

    public void textImport() throws IOException {

        File a = new File("C:\\Users\\22400\\Desktop\\a.txt");
        FileInputStream b = new FileInputStream(a);
        InputStreamReader c = new InputStreamReader(b, "UTF-8");
        String string2 = new String();
        while (c.ready()) {
            char string1 = (char) c.read();
            if (!isWord(string1)) {
                if (map1.containsKey(string2)) {
                    Integer num1 = map1.get(string2) + 1;
                    map1.put(string2, num1);
                } else {
                    Integer num1 = 1;
                    map1.put(string2, num1);
                }
                string2 = "";
            } else {
                string2 += string1;
            }
        }
        if (!string2.isEmpty()) {
            if (map1.containsKey(string2)) {
                Integer num1 = map1.get(string2) + 1;
                map1.put(string2, num1);
            } else {
                Integer num1 = 1;
                map1.put(string2, num1);
            }
            string2 = "";
        }
        c.close();
        b.close();
    }

    public void textExport(String txt) throws IOException {
        File fi = new File("StatisticsWord.txt");
        FileOutputStream fop = new FileOutputStream(fi);
        OutputStreamWriter ops = new OutputStreamWriter(fop, "UTF-8");
        ops.append(txt);
        ops.close();
        fop.close();
    }

    public boolean isWord(char a) {
        if (a <= 'z' && a >= 'a' || a <= 'Z' && a >= 'A')
            return true;
        return false;
    }

}

おすすめ

転載: www.cnblogs.com/weixiao1717/p/12151740.html