Filewriter没有写下单独的行

时间:2016-05-27 19:42:46

标签: java io bufferedreader filereader filewriter

我一直在用编码和解码文本文件进行搜索,这些密钥存储为每次程序生成时左侧(字符az,AZ,ect。)生成的数组。运行,以便它可以创建一个新密钥或在同一目录中使用一个。据我所知,当我对文本文件进行编码时,它可以正常工作并写入所有文件(使用多行)。然而,当我解码时,编写器将第一行写入文件,然后当它完成时,它删除/写入并继续写下一行,直到剩下的就是最后一行。这只是我的第一次通过,所以我会过去尝试稍后重写一下但是我仍然对整个问题感到困惑,不写单独的行。



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
&#13;
&#13;
&#13;

这只是我到目前为止遇到麻烦的部分,但是如果你需要看看如何制作钥匙或类似的东西,我也可以把它扔到那里。

1 个答案:

答案 0 :(得分:3)

好吧,首先,我可以告诉你,每次写字都不需要冲洗()。

您遇到的问题是您在while循环内打开文件而不是在其外部。这意味着它将继续重新打开文件并关闭它并重写第一行!切换创建FileWriter和while循环的try / catch顺序,您的问题就会得到解决。

如果您使用FileWriter创建了new FileWriter(filename, true),则追加到文件中,而不是从开头写入(覆盖)。我仍然建议不要这样做,因为打开和关闭文件资源并不是你想要做的事情,而且效率也非常低。

以下是您更正后的代码

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