-1

i am trying to read write from a file but i am always getting a error while either reading or writing to the file. when option -d is used, the input file will contain the ciphertext and the output file will contain the original plaintext, and the opposite is done for the case of option -e for both Caesar or Vigenere


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();
    }
}
Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
Abdulla
  • 39
  • 1
  • 2
  • 6
  • Which error? Please apply try-with-resources to the file streams. The files may be still open and cannot be opened anew. – Markus Kull Mar 08 '16 at 07:37

1 Answers1

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);
                      }
              }
Abdulla
  • 39
  • 1
  • 2
  • 6