У меня есть способ дешифрования, который должен открыть тестовый файл с зашифрованным текстом, а затем прочитать и расшифровать каждую строку текста, которую я прочитал из входного файла. Текстовый файл называется mystery.txt. Я могу заставить этот метод работать при вводе только одного символа, но я не могу заставить его работать, где я открываю файл .txt и расшифровываю строки за строкой. МетодРасшифровать зашифрованный текстовый файл
Dechiphering:
public static String cipherDecipherString(String text)
{
// These are global. Put here for space saving
private static final String crypt1 = "cipherabdfgjk";
private static final String crypt2 = "lmnoqstuvwxyz";
// declare variables
int i, j;
boolean found = false;
String temp="" ; // empty String to hold converted text
readFile();
for (i = 0; i < text.length(); i++) // look at every chracter in text
{
found = false;
if ((j = crypt1.indexOf(text.charAt(i))) > -1) // is char in crypt1?
{
found = true; // yes!
temp = temp + crypt2.charAt(j); // add the cipher character to temp
}
else if ((j = crypt2.indexOf(text.charAt(i))) > -1) // and so on
{
found = true;
temp = temp + crypt1.charAt(j);
}
if (! found) // to deal with cases where char is NOT in crypt2 or 2
{
temp = temp + text.charAt(i); // just copy across the character
}
}
return temp;
}
Мой ReadFile метод:
public static void readFile()
{
FileReader fileReader = null;
BufferedReader bufferedReader = null;
String InputFileName;
String nextLine;
clrscr();
System.out.println("Please enter the name of the file that is to be READ (e.g. aFile.txt: ");
InputFileName = Genio.getString();
try
{
fileReader = new FileReader(InputFileName);
bufferedReader = new BufferedReader(fileReader);
nextLine = bufferedReader.readLine();
while (nextLine != null)
{
System.out.println(nextLine);
nextLine = bufferedReader.readLine();
}
}
catch (IOException e)
{
System.out.println("Sorry, there has been a problem opening or reading from the file");
}
finally
{
if (bufferedReader != null)
{
try
{
bufferedReader.close();
}
catch (IOException e)
{
System.out.println("An error occurred when attempting to close the file");
}
}
}
}
Теперь я думал, что я бы просто быть в состоянии назвать свой метод ReadFile(), а затем перейти в код расшифровывать и пусть работайте через файл, но я не могу заставить его работать вообще.
Это наоборот, что мне это нужно. Я хочу вызвать readFile() в cipherDecipherString(). Я пробовал это, но я не могу заставить его работать правильно. – DarkBlueMullet
Отредактировал свой ответ. – fvink