[英]How do I read in a file and count characters?
I need help on how to start a Java class. 我需要有关如何启动Java类的帮助。
The class defines a method or methods that first reads in a file then reads its characters one by one. 该类定义一个或多个方法,该方法首先读取文件,然后一个一个地读取其字符。 After doing this it is to see how many of those characters form words by looking for things like '.'
完成此操作后,可以通过查找“。”之类的字符来查看其中有多少个字符构成单词。 and ' '.
和''。 After all that, keep count of how many words and put them on a hashmap.
毕竟,记下有多少个单词并将其放在哈希图中。
I know how to use a for loop to look for the ' ', ',', '.' 我知道如何使用for循环查找“”,“”,“”。 etc. and keeping count of words
等,并保持字数
But I don't know how to read in a file that reads character by character a file. 但是我不知道如何读取一个字符一个文件的文件。
My TA said something about the 'put' and 'get' method? 我的助教说了一些关于“放置”和“获取”方法的事情? no idea what they are :S
不知道它们是什么:S
Therefore I know how to do pretty much everything except the beginning. 因此,我知道除了开始之初,几乎所有事情都要做。 sorry I can't show you a method of the second half before knowing how to do the first half.
抱歉,在知道如何进行上半年之前,我无法向您展示下半年的方法。
Any help to get this started .. thanks! 任何帮助开始这个..谢谢!
A Reader
will allow you to read data character at a time (eg you could use an InputStreamReader
together with a FileInputStream
to read from a File
) Reader
将允许您一次读取数据字符(例如,您可以将InputStreamReader
与FileInputStream
一起使用以从File
读取)
Also make sure you specify the character encoding of the file you are reading, this ensures correct conversion from bytes to chars. 还要确保指定要读取的文件的字符编码,以确保从字节到字符的正确转换。
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("path/to/file"), "charset e.g. utf-8"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.