2014-11-18 3 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()을 호출하지 않습니다.

편집 : 파일의 모든 라인을 배열에 추가하고 배열에서 배열을 반환 할 수 있습니다. 그런 다음 해당 배열을 반복하고 라인별로 해독합니다.

반환 형식을 ArrayList로 변경합니다.

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

return textLines; 

그런 다음 cipherDecipherString()에서 readFile()을 호출하십시오.

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

나는 그것이 필요한 다른 방법입니다. cipherDecipherString()에서 readFile()을 호출하려고합니다. 나는 그것을 시도했지만 제대로 작동하지 않습니다. – DarkBlueMullet

+0

내 대답을 수정했습니다. – fvink