Java 程序文本文件问题
Java Program TextFile Issue
我有一个程序,其中读入一个文本文件,然后输出文件中的每个单词,然后在整个文件中重复#次。
使用以下代码。
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;
}
}
相关文章:
- 关于引入外部javascript文件的问题&css通过https
- Javascript,文件输入和FormData问题
- Node.js服务器问题-已添加“/"在浏览器中查找文件时
- Express Routes-多个文件的问题
- 使用ajax在html中加载html文件时出现的问题
- 尝试从csv文件中检索单个值时出现问题
- 在javascript中检查文件扩展名的问题
- Ruby on Rails 4:在 Rails Web 应用程序中添加 Javascript 文件时遇到问题
- 使用nodejs和id3打开文件时出现问题
- 将.js文件和jquery链接到html页面时出现问题
- mongodb用户配置文件添加输入时出现问题
- 在androidwebview中为本地文件创建脚本标记时出现问题
- SAPUI5绑定问题:json文件中维护的列表项链接不起作用
- 需要文件时出现问题
- 加载本地json文件问题
- 外部JavaScript文件问题
- 我的网站加载出现问题.这是一个嵌入到index.html中的.SWF文件
- PHP文件的权限问题
- 调用外部javascript文件时出现问题
- AngularJS:图片上传+文件阅读器预览.控制器、指令和作用域之间的绑定出现问题