【问题标题】:Reading from a text file and write it从文本文件中读取并写入
【发布时间】:2015-10-11 05:32:18
【问题描述】:

我已阅读文本文件并计算了该文件中每个字母的出现次数。我已经按降序打印了重复次数最多的字母。我想用另一个字母替换重复的字母并写回输出文件。我尝试用 char 或字符串数​​组中的另一个字母替换重复的字母,但我不知道问题出在哪里。谁能帮帮我吗。谢谢

【问题讨论】:

  • 你能多解释一下你的代码吗?喜欢添加 cmets?
  • Mr.Nivedita 在代码中添加了 cmets。
  • 我是个女孩:P(填充物)
  • 对不起妮维蒂塔。很高兴认识你... :)

标签: java string character replaceall


【解决方案1】:

尝试通过比较字母频率来破解简单代码是一种迭代方法,您可以从“e”、“t”、“a”、“o”、“i”、“n”、“s”的顺序开始, 'h', 'r',... 英语的特征分布。通常这不会是短文本中字母的相对顺序。因此,不要期望在第一次尝试时收到预期的输出 - 您必须根据基于第一个结果的猜测修改翻译表,重新运行,再次猜测等。

这里是 count 方法,重写后返回一个数组,该数组将字母 ('a'==0, 'b'==1,...) 与按频率排序的字母表中的位置相关联。

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;
}

方法替换使用这个数组:

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();
}

要写入文件,请使用

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

要转储转换表,请使用

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

【讨论】:

  • 嗨 Mr.Laune 我得到了一些单词与其他单词的组合,例如这样正在被替换.. U -> e, V-> t (字母和密钥).. 我怎样才能将它写入新文件..
  • a => v b => i c => g d => y e => c f => n g => z h => b i => f j => p k => h l => u m => q n => m o => k p => r q => d r => m s => s t => a u => e v => t w => l x => o y => x z => w a => v b => i c => g d => y翻译表Mr.laune的输出文件是这样的
猜你喜欢
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多