2016-11-22 1 views
0

나는 호프만 프로젝트에 종사하고 있으며 기본적인 작업을하고 있습니다. 주어진 문자열의 문자를 집계하여 표시합니다. 그러나 나는 파일에 수동으로 문자열을주는 것이 아니라 파일에서 읽길 원합니다.이 허프만 프로젝트를 파일에서 읽게하려면 어떻게해야합니까?

아래 코드는 텍스트 파일의 내용을 인쇄하는 방법에 대한 조언입니다. 난 당신이 사용하는 자바의 버전을 모르는 것처럼

import java.util.ArrayList; 

public class Main { 

public static void main(String[] args) { 

    String message = "Hello"; 

    // Convert the string to char array 

    char[] msgChar = message.toCharArray(); 
    ArrayList<Character> characters = new ArrayList<Character>(); 

    /* 
    * Get a List of all the chars which are present in the string No 
    * repeating the characters! 
    */ 
    for (int i = 0; i < msgChar.length; i++) { 
     if (!(characters.contains(msgChar[i]))) { 
      characters.add(msgChar[i]); 
     } 
    } 

    System.out.println(message); 
    System.out.println(""); 

    /* Count the number of occurrences of Characters */ 
    int[] countOfChar = new int[characters.size()]; 

    /* Fill The Array Of Counts with one as base value */ 
    for (int x = 0; x < countOfChar.length; x++) { 
     countOfChar[x] = 0; 
    } 

    /* Do Actual Counting! */ 
    for (int i = 0; i < characters.size(); i++) { 
     char checker = characters.get(i); 
     for (int x = 0; x < msgChar.length; x++) { 
      if (checker == msgChar[x]) { 
       countOfChar[i]++; 
      } 
     } 
    } 

    /* Sort the arrays is descending order */ 
    for (int i = 0; i < countOfChar.length - 1; i++) { 
     for (int j = 0; j < countOfChar.length - 1; j++) { 
      if (countOfChar[j] < countOfChar[j + 1]) { 
       int temp = countOfChar[j]; 
       countOfChar[j] = countOfChar[j + 1]; 
       countOfChar[j + 1] = temp; 

       char tempChar = characters.get(j); 
       characters.set(j, characters.get(j + 1)); 
       characters.set(j + 1, tempChar); 
      } 
     } 
    } 

    /* Print Out The Frequencies of the Characters */ 
    for (int x = 0; x < countOfChar.length; x++) { 
     System.out.println(characters.get(x) + " - " + countOfChar[x]); 
    } 

} 
} 
+0

[파일의 내용에서 Java 문자열을 작성하려면 어떻게합니까?] (http://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from- the-contents-of-a-file) – Slartibartfast

답변

0
String line; 
try (
    InputStream fis = new FileInputStream("file_name"); 
    InputStreamReader isr = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(isr); 
) { 
while ((line = br.readLine()) != null) { 
    // Deal with the line 
} 
} 

클리너 방법은
List<String> lines = Files.readAllLines(path);

0

이 될 것입니다,이 방법은 해결할 수 :

private static String readFromFile(String filename) { 
    try { 
     StringBuilder sb = new StringBuilder(); 
     BufferedReader reader = new BufferedReader(new FileReader(filename)); 
     String line; 
     while ((line = reader.readLine()) != null) 
      sb.append(line).append("\n"); 
     reader.close(); 
     return sb.toString(); 
    } catch (IOException e) { 
     System.err.println("File " + filename + " cannot be read: " + e.getMessage()); 
     return null; 
    } 
} 

와의를 코드를 다음과 같이 사용할 수 있습니다.

public static void main(String[] args) { 
    String message = readString("testfile.txt"); 
    if(message == null) 
     return; // cannot read!