2014-11-18 4 views
0

У меня есть способ дешифрования, который должен открыть тестовый файл с зашифрованным текстом, а затем прочитать и расшифровать каждую строку текста, которую я прочитал из входного файла. Текстовый файл называется 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(), а затем перейти в код расшифровывать и пусть работайте через файл, но я не могу заставить его работать вообще.

ответ

0

В readFile() вы ничего не делаете с прочитанными вами строками, вы не вызываете cipherDecipherString() в любом месте.

Редактировать: вы можете добавить все строки из файла в массив и вернуть массив из fuction. Затем выполните итерацию по этому массиву и расшифруйте строку по строке

Измените возвращаемый тип readFile() на ArrayList;

ArrayList<String> textLines = new ArrayList<>(); 
while(nextLine != null) { 
    textLines.add(nextLine); 
    nextLine = bufferedReader.readLine(); 
} 

return textLines; 

Затем в cipherDecipherString() вызов readFile().

ArrayList<String> textLines = readFile(); 
+0

Это наоборот, что мне это нужно. Я хочу вызвать readFile() в cipherDecipherString(). Я пробовал это, но я не могу заставить его работать правильно. – DarkBlueMullet

+0

Отредактировал свой ответ. – fvink

Смежные вопросы