【发布时间】:2014-01-12 06:54:29
【问题描述】:
好的,我正在编写一些会生成密码的东西,并为每个字母分配一个从 2 到 6 的随机值的字符串
import java.io.*;
import java.util.*;
public class filewriters {
public static void main(String[] args) throws IOException{
FileWriter a = new FileWriter("crypt.txt");
char whynot[];
whynot = new char[97];
String b[] = new String[97];
for(int w = 30; w<127; w++){
char lol =(char) w;
whynot[w - 30] = lol;
a.write(lol + " : " );
String and = "";
int um = (int) (Math.random() * 5 + 1);
for(int q = 0; q<um; q++){
int well = (int)( Math.random() * 97 + 30);
char hello = (char) well;
and+= hello;
}
b[w - 30] = and;
a.write(and + "\n");
}
toencode(whynot, b);
a.close();
}
private static void toencode(char[] whynot, String[] b) throws IOException{
Scanner sc = new Scanner(System.in);
FileWriter themessage = new FileWriter("themessage.txt");
FileWriter theEncrypted = new FileWriter("encrypted.txt");
FileWriter toDecode = new FileWriter("DecodeThis.txt");
String thevar = sc.nextLine();
char lol[] = thevar.toCharArray();
for(int w = 0; w < lol.length; w++){
char a = lol[w];
themessage.write(a + " : ");
for(int q = 0; q<whynot.length; q++){
if(a == whynot[q]){
themessage.write(b[q] + "\n");
theEncrypted.write(b[q] + "\n");
toDecode.write(b[q]);
System.out.println(b[q]);
}
}
}
theEncrypted.close();
themessage.close();
toDecode.close();
}
}
好的,大部分情况下它都可以正常工作。唯一的问题是我希望它保留前一个文件的内容,并向文件写入更多内容,但每次运行后,文件的前一个内容都会被删除。有人可以帮忙吗?
【问题讨论】:
标签: java file filereader filewriter