Reputation: 37
I've been rifling around with encoding and decoding text files with a key that's stored as an array that has the left hand side(characters a-z, A-Z, ect.) generated each time the program is run so that it can either make a new key or use one within the same directory. As far as I can tell, when I encode a text file it works fine and writes all of it (works with multiple lines). When I decode it however, the writer writes to the file the first line, then when it's done with that, it deletes/writes over it and continues writing the next line which keeps going until all that's left is the last line. This is just my first pass at it so i'm going to go over and try to rewrite a bit of it later but I am still confused as to the whole problem with not writing separate lines.
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
This is only the part that I've been having trouble with so far but if you need to see how the key is made or anything like that just ask and I can throw it up there as well.
Upvotes: 0
Views: 116
Reputation: 1113
Well, first off, I can tell you that you don't need to flush() every time you write a character.
The problem you are having is that you are opening the file inside your while
loop instead of outside it. This means it will keep reopening the file and closing it and rewriting the first line! Switch the order of your try/catch where you create the FileWriter and your while
loop and your problem will be solved.
If you created your FileWriter
using new FileWriter(filename, true)
, then it would append to the file rather than write from the start (which overwrites). I'd still recommend against that because opening and closing the file resource is not what you are trying to do and it's also really inefficient.
Here is what your corrected code would look like
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
Upvotes: 3