简体   繁体   中英

How do I read in a file and count characters?

I need help on how to start a Java class.

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 ' ', ',', '.' 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

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 )

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM