简体   繁体   English

如何读取文件并计数字符?

[英]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将允许您一次读取数据字符(例如,您可以将InputStreamReaderFileInputStream一起使用以从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.

相关问题 如何在.NET,Java和Sql Server上获得正确的字符数? (在谷歌浏览器中阅读) - How do I get the correct count of characters on .NET, Java and Sql Server? (read this in Google Chrome) 如何使用扫描仪或缓冲读取器读取输入文件,并计算输入文件中特定字符的所有出现次数 - How do I read in an input file with a scanner or buffered reader and count all the occurences of a specific character in the input file 我如何简单地读取我创建的 .txt 文件并计算其行数 - How do I simply read a .txt file that I created and count its lines 我如何比较从 csv 文件中读取的日期,以便我只计算该月的那些帖子? - How do i compare the dates read from csv file, such that I will only count those post with that month? 如何从文本文件中读取单个字符并将它们存储到二维数组中? - How do I get individual characters from read in text file and store them into a 2D array? 如何计算字符串中唯一字符的数量? - 更新 - How do I count the number of unique characters in a string? - Updated 如何计算空格,元音和字符的数量? - How do I count number of spaces, vowels, and characters? 如何计算字符串中的字符数,然后按字母顺序排序? - How do I count the characters in a string and then sort them alphabetically? 如何计算dat文件中的字符? - How to count characters in a dat file? 如何读取 java 中字符之间的多个标记? - How do I read multiple tokens between characters in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM