java 计算文本文件中的字符数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6831938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting the number of characters from a text file
提问by Bubble
I currently have the following code:
我目前有以下代码:
public class Count {
public static void countChar() throws FileNotFoundException {
Scanner scannerFile = null;
try {
scannerFile = new Scanner(new File("file"));
} catch (FileNotFoundException e) {
}
int starNumber = 0; // number of *'s
while (scannerFile.hasNext()) {
String character = scannerFile.next();
int index =0;
char star = '*';
while(index<character.length()) {
if(character.charAt(index)==star){
starNumber++;
}
index++;
}
System.out.println(starNumber);
}
}
I'm trying to find out how many times a * occurs in a textfile. For example given a text file containing Hi * My * name *
我试图找出 * 在文本文件中出现的次数。例如给定一个包含 Hi * My * name * 的文本文件
the method should return with 3
该方法应该返回 3
Currently what happens is with the above example the method would return:
目前发生的事情是上面的例子,该方法将返回:
0 1 1 2 2 3
0 1 1 2 2 3
Thanks in advance.
提前致谢。
回答by Lukas Eder
Use Apache commons-io to read the file into a String
使用 Apache commons-io 将文件读入字符串
String org.apache.commons.io.FileUtils.readFileToString(File file);
And then, use Apache commons-lang to count the matches of *
:
然后,使用 Apache commons-lang 计算以下匹配项*
:
int org.apache.commons.lang.StringUtils.countMatches(String str, String sub)
Result:
结果:
int count = StringUtils.countMatches(FileUtils.readFileToString(file), "*");
回答by marc
int countStars(String fileName) throws IOException {
FileReader fileReader = new FileReader(fileName);
char[] cbuf = new char[1];
int n = 0;
while(fileReader.read(cbuf)) {
if(cbuf[0] == '*') {
n++;
}
}
fileReader.close();
return n;
}
回答by home
Everything in your method works fine, except that you print the count per line:
您的方法中的所有内容都可以正常工作,除了您打印每行的计数:
while (scannerFile.hasNext()) {
String character = scannerFile.next();
int index =0;
char star = '*';
while(index<character.length()) {
if(character.charAt(index)==star){
starNumber++;
}
index++;
}
/* PRINTS the result for each line!!! */
System.out.println(starNumber);
}
回答by Paul
I would stick to the Java libraries at this point, then use other libraries (such as the commons libraries) as you become more familiar with the core Java API. This is off the top of my head, might need to be tweaked to run.
在这一点上,我会坚持使用 Java 库,然后随着您对核心 Java API 越来越熟悉,而使用其他库(例如公共库)。这是我的头顶,可能需要调整才能运行。
StringBuilder sb = new StringBuilder();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while (s != null)
{
sb.append(s);
s = br.readLine();
}
br.close(); // this closes the underlying reader so no need for fr.close()
String fileAsStr = sb.toString();
int count = 0;
int idx = fileAsStr('*')
while (idx > -1)
{
count++;
idx = fileAsStr('*', idx+1);
}