2016-11-22 29 views
0

我正在做一個霍夫曼項目,我有一些基本的工作。它計算給定字符串中的字符並顯示它們;不過,我希望它從文件中讀取,而不是手動給它打印一個字符串。如何讓這個Huffman項目從文件中讀取?

我的代碼如下,任何關於使它打印文本文件的內容的建議將是非常有用的。

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-的 - 內容 - 的 - 一個文件) – 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

由於我不知道你正在使用的Java版本,這種方法可以解決:

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! 
相關問題