2016-03-08 167 views
-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(); 
    } 
} 
的情況下做
+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); 
         } 
       }