2016-05-27 3 views
0

私は、プログラムが実行されるたびに生成された左側(文字az、AZなど)の配列として格納されたキーを使ってテキストファイルをエンコードし、新しいキーを作成するか、同じディレクトリ内で新しいキーを使用することができます。限り、私が言うことができる限り、私はテキストファイルをエンコードすると、それはうまく動作し、すべての(複数の行で動作します)を書き込みます。しかし、私はそれを解読すると、ライターは最初の行をファイルに書き込みます。それで終わったら、はそれを削除/上書きしてを書き込み、残りの行が最後の行になるまで次の行を書き続けます。これはちょうど私の最初のパスですので、私はそれを少し書き直そうとするつもりですが、別の行を書かないという問題全体についてはまだ混乱しています。ファイルライターは別の行を書いていません

if (selectedOption2 == "Decode a message") //Set by second M-JOP 
 
{ 
 
    String userDefinedEncryptedFileName = JOptionPane.showInputDialog("Please input file name"); //Ask for the name of the file that needs to be decoded 
 
    File UDF = new File(userDefinedEncryptedFileName); //File to be encrypted 
 
    else //Start decrypting 
 
    { 
 
    BufferedReader BReader; 
 
    FileReader FReader; 
 
    String currentLine = ""; 
 
    String currentPiece = ""; 
 
    try (BufferedReader bReader = new BufferedReader(new FileReader(userDefinedEncryptedFileName))) { 
 
     String currentReaderLine; 
 
     while ((currentReaderLine = bReader.readLine()) != null) { 
 
     try (FileWriter decodedWriter = new FileWriter("DecryptedFile.txt")) { 
 
      for (int c = 0; c < currentReaderLine.length(); c++) //Keep going through the entire encoded line as long as c is still in the line 
 
      { 
 
      if (currentReaderLine.charAt(c) == ' ') //The reader sees a space while going through the encoded file line and defines a piece from that information 
 
      { 
 
       if (currentPiece.equals("DAKkdw235")) //Immediate check to see if it is simply a space(I was too lazy to incorporate it into array so far so it's just a standardized piece that's the same regardless of the key) 
 
       { 
 
       decodedWriter.write(' '); 
 
       } else //put this for in a condition statement that asks if it's a character(?) 
 
       { 
 
       for (int rowPosition = 0; rowPosition <= 89; rowPosition++) //Go through table now that sum/currentPiece is an entire piece that is in our key array 
 
       { 
 
        if (currentPiece.equals(KeyTable[0][rowPosition])) //found the case that currentPiece matches a piece in our key array 
 
        { 
 
        decodedWriter.write(KeyTable[1][rowPosition]); 
 
        //System.out.println("Printed to file: " + KeyTable[1][rowPosition]);//Error checking 
 
        decodedWriter.flush(); 
 
        } 
 
       } 
 
       } 
 
       currentPiece = ""; //Reset the currentPiece of the current piece of code to empty so it can be reused for the next piece in the line to be processed and defined by the key       
 
      } else if (!(currentReaderLine.charAt(c) == ' ')) //This is in the case that the reader hasnt finished going through the entire piece and will keep adding to currentPiece till it finds the end of the piece it's on 
 
      { 
 
       currentPiece = currentPiece + currentReaderLine.charAt(c); 
 
      } 
 
      } 
 
      decodedWriter.write(System.lineSeparator()); //End of the line is found and a "return" is entered into the file(doesn't work(?) even though I've also used /r/n as well)    
 
     } catch (IOException ex) { 
 
      System.out.println("An error occured while creating the decoded file"); 
 
     } 
 
     } 
 
    } catch (IOException ex) { 
 
     System.out.println("An error occured while creating the decoded file"); 
 
    } 
 
    } 
 
} //End decode message path

これは私がこれまでに問題を抱えてきましたが、あなたは鍵が作られたか、そのようなものがちょうど尋ねると、私はそれを投げることができているかを確認する必要がある場合にのみ一部であり、そこにも同様に。

答えて

3

まず、キャラクターを書くたびにflush()する必要はありません。

問題は、ループの外側ではなく、whileループ内のファイルを開いていることです。これは、ファイルを再オープンして閉じ、最初の行を書き直すことを意味します! FileWriterとwhileループを作成するtry/catchの順序を入れ替えて、問題を解決してください。あなたがnew FileWriter(filename, true)を使用してFileWriterを作成した場合

、それはファイルにを追加ではなく、(上書き)最初から書きます。ファイルリソースのオープンとクローズはあなたがやろうとしていることではなく、実際には非効率的でもあるので、私はまだそれを勧めています。ここで

は、あなたの修正されたコードは、私はちょうど行って、あなたが言ったことをやった、それは完全にトリックをした

if (selectedOption2 == "Decode a message") //Set by second M-JOP 
{ 
    String userDefinedEncryptedFileName = JOptionPane.showInputDialog("Please input file name"); //Ask for the name of the file that needs to be decoded 
    File UDF = new File(userDefinedEncryptedFileName); //File to be encrypted 
    else //Start decrypting 
    { 
    BufferedReader BReader; 
    FileReader FReader; 
    String currentLine = ""; 
    String currentPiece = ""; 
    try (BufferedReader bReader = new BufferedReader(new FileReader(userDefinedEncryptedFileName))) { 
     try (FileWriter decodedWriter = new FileWriter("DecryptedFile.txt")) { 
     String currentReaderLine; 
     while ((currentReaderLine = bReader.readLine()) != null) { 
      for (int c = 0; c < currentReaderLine.length(); c++) //Keep going through the entire encoded line as long as c is still in the line 
      { 
      if (currentReaderLine.charAt(c) == ' ') //The reader sees a space while going through the encoded file line and defines a piece from that information 
      { 
       if (currentPiece.equals("DAKkdw235")) //Immediate check to see if it is simply a space(I was too lazy to incorporate it into array so far so it's just a standardized piece that's the same regardless of the key) 
       { 
       decodedWriter.write(' '); 
       } else //put this for in a condition statement that asks if it's a character(?) 
       { 
       for (int rowPosition = 0; rowPosition <= 89; rowPosition++) //Go through table now that sum/currentPiece is an entire piece that is in our key array 
       { 
        if (currentPiece.equals(KeyTable[0][rowPosition])) //found the case that currentPiece matches a piece in our key array 
        { 
        decodedWriter.write(KeyTable[1][rowPosition]); 
        //System.out.println("Printed to file: " + KeyTable[1][rowPosition]);//Error checking 
        decodedWriter.flush(); 
        } 
       } 
       } 
       currentPiece = ""; //Reset the currentPiece of the current piece of code to empty so it can be reused for the next piece in the line to be processed and defined by the key       
      } else if (!(currentReaderLine.charAt(c) == ' ')) //This is in the case that the reader hasnt finished going through the entire piece and will keep adding to currentPiece till it finds the end of the piece it's on 
      { 
       currentPiece = currentPiece + currentReaderLine.charAt(c); 
      } 
      } 
      decodedWriter.write(System.lineSeparator()); //End of the line is found and a "return" is entered into the file(doesn't work(?) even though I've also used /r/n as well)    
     } 
     } catch (IOException ex) { 
     System.out.println("An error occured while creating the decoded file"); 
     } 
    } catch (IOException ex) { 
     System.out.println("An error occured while creating the decoded file"); 
    } 
    } 
} //End decode message path 
+0

どのように見えるかです。私はいつもそれを毎回開いているとは思っていませんでした。ありがとう! – Jaskal

関連する問題