gpt4 book ai didi

java - 文件编写器不写下单独的行

转载 作者:行者123 更新时间:2023-12-01 21:30:40 25 4
gpt4 key购买 nike

我一直在用一个 key 对文本文件进行编码和解码,该 key 存储为一个数组,每次运行程序时都会生成左侧(字符 a-z、A-Z 等),以便它可以创建一个新 key 或使用同一目录中的 key 。据我所知,当我对文本文件进行编码时,它工作正常并写入所有内容(适用于多行)。然而,当我解码它时,编写器将第一行写入文件,然后当完成后,它会删除/覆盖它并继续写入下一行,直到剩下的都是最后一行。这只是我的第一次,所以我稍后会回顾并尝试重写一些内容,但我仍然对不编写单独的行的整个问题感到困惑。

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

这只是到目前为止我一直遇到麻烦的部分,但如果您需要了解 key 是如何制作的或类似的东西,只需询问,我也可以将其放在那里。

最佳答案

首先,我可以告诉你,你不需要每次写一个字符时都刷新()。

您遇到的问题是您在 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

关于java - 文件编写器不写下单独的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com