2016-03-08 58 views
-1

我有一個程序,其中讀入一個文本文件,然後輸出文件中的每個單詞,然後是在整個文件中重複的次數。Java程序文本文件問題

回答

1

使用下面的代碼。

import java.io.*; 

class FileRead { 

public static void main(String args[]) { 
    try { 
     // Open the file that is the first 
     // command line parameter 
     FileInputStream fstream = new FileInputStream("C:\\Users\\Desktop\\formate.txt"); 
     // Get the object of DataInputStream 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 
     //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
      // Print the content on the console 
      System.out.println(strLine); 
     } 
     //Close the input stream 
     in.close(); 
    } catch (Exception e) {//Catch exception if any 
     System.err.println("Error: " + e.getMessage()); 
    } 
    } 
} 
+0

在我不能用一個BufferedReader文件。我相信這是我的while循環。它逐行閱讀而不是閱讀整個文件。 – Mike

0

試試這個代碼:

public static void main(String[] args) throws Throwable 
{ 
    File inputFile = new File("input.txt"); 
    File outputFile = new File("output.txt"); 
    Scanner scanner = new Scanner(inputFile); 
    HashMap<String, Integer> count = new HashMap<String, Integer>(); 

    while (scanner.hasNext()) 
    { 
     String word = scanner.next(); 
     if (count.containsKey(word)) 
     { 
      count.put(word, count.get(word) + 1); 
     } 
     else 
     { 
      count.put(word, 1); 
     } 
    } 
    scanner.close(); 

    BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile)); 
    for (Entry<String, Integer> entry : count.entrySet()) 
    { 
     writer.write("#" + entry.getKey() + " " + entry.getValue()+"\r\n"); 
    } 
    writer.close(); 
} 
+0

不能使用緩衝讀取器或HashMap :(我知道它會更容易,但它是一個任務 – Mike

+0

此代碼不使用BufferedReader,它只使用BufferedWriter,唯一的問題就是HashMap。掃描儀班是理想的,你可以使用它,它逐字讀 –

0

這也,這是一個簡單多了,如果你不能使用HashMap的或BufferedReader中:

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.LinkedList; 
import java.util.Scanner; 

public class WordCounter 
{ 
    public static void main(String[] args) throws Throwable 
    { 
     File inputFile = new File("input.txt"); 
     File outputFile = new File("output.txt"); 
     Scanner scanner = new Scanner(inputFile); 
     LinkedList<Word> words = new LinkedList<Word>(); 

     while (scanner.hasNext()) 
     { 
      String word = scanner.next(); 
      addWord(words, word); 
     } 
     scanner.close(); 

     WriteToFile(outputFile, words); 
    } 

    private static void WriteToFile(File outputFile, LinkedList<Word> words) throws IOException 
    { 
     BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile)); 
     for (Word word : words) 
     { 
      writer.write("#" + word.getWord() + " " + word.getCount() + "\r\n"); 
     } 
     writer.close(); 
    } 

    private static void addWord(LinkedList<Word> words, String word) 
    { 
     for (Word aWord : words) 
     { 
      if (aWord.getWord().equals(word)) 
      { 
       aWord.incrementCount(); 
       return; 
      } 
     } 
     words.add(new Word(word, 1)); 
    } 
} 

class Word 
{ 
    private String word; 
    private int count; 

    public Word(String word, int count) 
    { 
     this.word = word; 
     this.count = count; 
    } 

    public String getWord() 
    { 
     return word; 
    } 

    public void setWord(String word) 
    { 
     this.word = word; 
    } 

    public int getCount() 
    { 
     return count; 
    } 

    public void setCount(int count) 
    { 
     this.count = count; 
    } 

    public void incrementCount() 
    { 
     count++; 
    } 

    @Override 
    public String toString() 
    { 
     return "Word: " + word + " Count: " + count; 

    } 

}