Java 程序文本文件问题

Java Program TextFile Issue

本文关键字:问题 文件 文本 程序 Java      更新时间:2023-09-26

我有一个程序,其中读入一个文本文件,然后输出文件中的每个单词,然后在整个文件中重复#次。

使用以下代码。

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());
    }
  }
}

试试这段代码:

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();
}

如果你不能使用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;
    }
}