-1
我正在尝试从文件中读取数据,但在读取或写入文件时总是收到错误。 当使用选项-d中,输入文件将包含密文和所述输出文件将包含原始明文,将其相反为选项-e两个凯撒或的V @ genere读取写入文件Substitution_Ciphers
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();
}
}
的情况下做
哪个错误?请将试用资源应用于文件流。这些文件可能仍然打开,无法重新打开。 –