我正在尝试从文件中读取写入但是在读取或写入文件时我总是收到错误。 当使用选项-d时,输入文件将包含密文,输出文件将包含原始明文,而对于Caesar或Vigenere选项-e的情况则相反;
public static void main(String[] args) throws IOException {
BufferedReader br = null;
String sCurrentLine=null;
try {
StringBuilder sb = new StringBuilder();
br = new BufferedReader(new FileReader("C:\\Users\\Mariam\\Documents\\NetBeansProjects\\SecurityHW1\\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
sb.append(sCurrentLine);
sCurrentLine= br.readLine();
}
sCurrentLine=sCurrentLine.toString();
System.out.println(sCurrentLine);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
File file = new File("C:\\Users\\Mariam\\Documents\\NetBeansProjects\\SecurityHW1\\out.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("Done");
String s = null, key;
do {
System.out.println("Please enter the substitution Ciphers you want to use? (Caesar/Vigenere) ");
Scanner in = new Scanner(System.in);
s = in.nextLine();
if (s.equalsIgnoreCase("Caesar") || s.equalsIgnoreCase("Vigenere")) {
if (s.equalsIgnoreCase("Caesar")) {
System.out.println(" please enter the key");
key = in.nextLine();
Integer intkey = Integer.valueOf(key);
System.out.println(" Select -d for decrypting or -e for encrypting");
String choose=in.nextLine();
if (choose.equalsIgnoreCase("d")) {
System.out.println(caesar(sCurrentLine, intkey*-1));
bw.write(caesar(sCurrentLine, intkey*-1));
}
else if (choose.equalsIgnoreCase("e"))
bw.write(caesar(sCurrentLine, intkey));
System.out.println(caesar(sCurrentLine, intkey));
}
else if (s.equalsIgnoreCase("Vigenere")) {
System.out.println(" please enter the keyWord");
String KeyWord=in.nextLine();
System.out.println(" Select -d for decrypting or -e for encrypting");
String choose2=in.nextLine();
String encryptedMsg=null ;
if (choose2.equalsIgnoreCase("e")) {
encryptedMsg = VigenereEncrypt( sCurrentLine,KeyWord);
bw.write(VigenereEncrypt( sCurrentLine,KeyWord));
}
else if(choose2.equalsIgnoreCase("d")) {
System.out.println("Decrypted message: " + VigenereDecrypt(encryptedMsg, KeyWord));
bw.write(VigenereDecrypt(encryptedMsg, KeyWord));
}
}
}
else if(s.equalsIgnoreCase("-1")) {
break;
}
else
System.out.println("Please enter a valid option");
}
while (!s.equalsIgnoreCase("Caesar") || !s.equalsIgnoreCase("Vigenere"));
bw.close();
}
}
答案 0 :(得分:0)
if (s.equalsIgnoreCase("Caesar")) {
System.out.println(" please enter the key");
key = in.nextLine();
Integer intkey = Integer.valueOf(key);
System.out.println(" Select -d for decrypting or -e for encrypting");
String choose=in.nextLine();
if (choose.equalsIgnoreCase("d"))
{
System.out.println("enter file name");
fName=in.next();
FileReader f= new FileReader("//Users//n//Desktop//"+fName);
BufferedReader read=new BufferedReader(f);
String c="";
String line=read.readLine();
while(line !=null){
c=c+line;
line=read.readLine();
}
c=c.toLowerCase();
System.out.println(caesar(c, intkey*-1));
output=caesar(c, intkey*-1);
System.out.println("Enter your file Name with .txt");
fName=in.next();
File file1 = new File("//Users//n//Desktop//"+fName);
FileWriter f2 = new FileWriter(file1);
try (BufferedWriter out = new BufferedWriter(f2)) {
out.write(output);
}
}