从文本文件中读取并写入
Reading from a text file and write it
我已经阅读了文本文件并计算了该文件中每个字母的出现次数。我按降序打印了次数最多的字母。我想用另一个字母替换重复的字母并写回输出文件。我尝试用 char 或字符串数组中的另一个字母替换 repeated 但我不知道是什么问题。谁能帮帮我吗。谢谢
尝试通过比较字母频率来破解简单代码是一种迭代方法,您从顺序 'e'、't'、'a'、'o'、'i', 'n', 's', 'h', 'r',...英文的特征分布。通常这不是短文本中字母的相对顺序。所以,不要指望第一次尝试就能收到预期的输出——你必须根据第一个结果的猜测修改翻译 table,重新 运行,再次猜测,等等.
这是方法计数,重写为 return 一个数组,该数组将字母 ('a'==0, 'b'==1,...) 与table 按频率排序的字母。
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();
要转储翻译 table,请使用
for( char c = 'a'; c <= 'z'; ++c ){
out.println( c + " => " + s[trans[c-'a']] );
}
我已经阅读了文本文件并计算了该文件中每个字母的出现次数。我按降序打印了次数最多的字母。我想用另一个字母替换重复的字母并写回输出文件。我尝试用 char 或字符串数组中的另一个字母替换 repeated 但我不知道是什么问题。谁能帮帮我吗。谢谢
尝试通过比较字母频率来破解简单代码是一种迭代方法,您从顺序 'e'、't'、'a'、'o'、'i', 'n', 's', 'h', 'r',...英文的特征分布。通常这不是短文本中字母的相对顺序。所以,不要指望第一次尝试就能收到预期的输出——你必须根据第一个结果的猜测修改翻译 table,重新 运行,再次猜测,等等.
这是方法计数,重写为 return 一个数组,该数组将字母 ('a'==0, 'b'==1,...) 与table 按频率排序的字母。
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();
要转储翻译 table,请使用
for( char c = 'a'; c <= 'z'; ++c ){
out.println( c + " => " + s[trans[c-'a']] );
}