Search code examples
javastringcharacterreplaceall

Reading from a text file and write it


I have read text file and counted the occurrences of every letter in that file. I have printed the most repeated letter with the count in the descending order. I want to replace repeated letter with another letter and write back into an output file. I have tried replacing the repeated with another letter in char or string array but I don't know what is the problem. Can anyone please help me. Thank you


Solution

  • Trying to break a simple code by comparing letter frequencies is an iterative approach where you begin with the order 'e', 't', 'a', 'o', 'i', 'n', 's', 'h', 'r',... the characteristic distribution for English. Usually this will not be the relative order of letters in a short text. So, don't expect to receive the expected output on the first try - you'll have to modify the translation table according to guesses based on the first result, re-run, guess again, etc.

    Here is method count, rewritten to return an array that associates a letter ('a'==0, 'b'==1,...) with a position in the table of letters ordered by frequency.

    public int[] load() throws IOException {
        File fp = new File("realtext.txt");
        BufferedReader in = new BufferedReader(new FileReader(fp));
        int[] count = new int[26];
        int nextChar;
        while ((nextChar = in.read()) != -1) {
            int ch = ((char) nextChar);
            if (ch >= 'a' && ch <= 'z') {
                count[ch - 'a']++;
            }
        }
    
        int[] position = new int[count.length];
        for (int k = 0; k < 26; k++) {
            position[k] = k;
        }
        for (int k = 0; k < 26; k++) {
            int max = count[k];
            int maxpos = k;
            for (int l = k + 1; l < 26; l++) {
                if( count[l] > max ){
                    max = count[l];
                    maxpos = l;
                }
            }
            int h = count[k];
            count[k] = count[maxpos];
            count[maxpos] = h;
            h = position[k];
            position[k] = position[maxpos];
            position[maxpos] = h;
        }
    
        int trans[] = new int[position.length];
        System.out.println("Descending order");
        for (int k = 0; k < 26; k++) {
            trans[position[k]] = k;
            System.out.printf("%c = %d -> %d-th\n",
                              position[k] + 'A', count[k], k);
        }
        return trans;
    }
    

    Method replacing uses this array:

    public void replacing(int[] trans) throws IOException {
        File fp = new File("ciphertext1.txt");
        BufferedReader in = new BufferedReader(new FileReader(fp));
        char[] s = {'e', 't', 'a', 'o', 'i', 'h', 's', 'n', 'd', 'r',
                    'l', 'c', 'u', 'm', 'w', 'f', 'g', 'y', 'p', 'b',
                    'v', 'k', 'j', 'x', 'q', 'z'};
        String line;
        while ((line = in.readLine()) != null) {
            StringBuilder sb = new StringBuilder();
            for( int i = 0; i < line.length(); ++i ){
                char c = line.charAt(i);
                if( 'a' <= c && c <= 'z' ){
                    sb.append( s[trans[c-'a']] );
                } else {
                      sb.append( c );
                }
            }
            System.out.println( sb.toString() );
        }
        in.close();
    }
    

    To write to a file, use

    File outfp = new File("decoded.txt");
    PrintWriter out = new PrintWriter(new FileWriter(outfp));
    ...
       out.println( sb.toString() );
    ...
    out.close();
    

    To dump the translation table, use

    for( char c = 'a'; c <= 'z'; ++c ){
        out.println( c + " => " + s[trans[c-'a']] );
    }