2016-05-27 20 views
0

Ich habe herum mit Codieren und Decodieren von Textdateien mit einem Schlüssel, der als ein Array gespeichert ist, die die linke Seite (Zeichen az, AZ, ect.) Jedes Mal generiert wird, das Programm ist Führen Sie den Befehl aus, damit er entweder einen neuen Schlüssel erstellen oder einen im selben Verzeichnis verwenden kann. Soweit ich das beurteilen kann, funktioniert es, wenn ich eine Textdatei kodiere, gut und schreibt alles (arbeitet mit mehreren Zeilen). Wenn ich es jedoch decodiere, schreibt der Schreiber in die Datei die erste Zeile, und wenn es damit fertig ist, löscht/schreibt es darüber und schreibt weiter die nächste Zeile, die weitergeht, bis nur noch die letzte Zeile übrig ist. Dies ist nur mein erster Durchlauf, also werde ich später darüber nachdenken und versuchen, etwas davon neu zu schreiben, aber ich bin immer noch verwirrt, was das Problem betrifft, wenn ich keine separaten Zeilen schreibe.Filewriter nicht separate Zeilen schreiben

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

Dies ist nur der Teil, den ich habe mit so viel Mühe, aber wenn Sie benötigen, um zu sehen, wie der Schlüssel gemacht wird oder so etwas nur fragen, und ich kann es werfen da oben auch.

Antwort

3

Nun, zunächst einmal kann ich Ihnen sagen, dass Sie nicht jedes Mal, wenn Sie ein Zeichen schreiben, flush() müssen.

Das Problem, das Sie haben, ist, dass Sie die Datei innerhalb Ihrer while Schleife statt außerhalb öffnen. Das bedeutet, dass es die Datei immer wieder öffnet und schließt und die erste Zeile neu schreibt! Ändern Sie die Reihenfolge Ihrer Versuche/Fang, wo Sie den FileWriter und Ihre while Schleife erstellen und Ihr Problem wird gelöst.

Wenn Sie erstellt Ihre FileWriternew FileWriter(filename, true) verwenden, dann wäre es an die Datei anhängen, anstatt von Anfang an schreiben (was überschreibt). Ich würde immer noch empfehlen, denn das Öffnen und Schließen der Datei-Ressource ist nicht das, was Sie versuchen, und es ist auch wirklich ineffizient.

Hier ist, was Ihre korrigierten Code aussehen würde

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

Ich ging und tat, was Sie gesagt haben und das hat ganz den Trick. Ich kann nicht glauben, dass ich nicht gesehen habe, dass ich es jedes Mal früher geöffnet habe. Vielen Dank! – Jaskal