0 Star 0 Fork 0

kong1987/WC的基础功能实现

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test1.java 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
package com.kong.base;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class BaseCount {
private int c = -1;
private int w = -1;
private int l = -1;
private File file;
public int countChar(String file) throws IOException {
this.file = new File(file);
FileInputStream fip = new FileInputStream(this.file);
InputStreamReader reader = new InputStreamReader(fip);
StringBuffer sBuffer = new StringBuffer();
while (reader.ready()) {
sBuffer.append((char) reader.read());
}
c = sBuffer.length();
reader.close();
fip.close();
return c;
}
public int countLine(String file) throws IOException {
this.file = new File(file);
FileInputStream fip = new FileInputStream(this.file);
InputStreamReader reader = new InputStreamReader(fip);
StringBuffer sBuffer = new StringBuffer();
while (reader.ready()) {
sBuffer.append((char) reader.read());
}
String str = sBuffer.toString();
char[] ch = str.toCharArray();
l = 0;
for (char c : ch) {
if (c == '\n') {
l++;
}
}
reader.close();
fip.close();
return l;
}
public int countWord(String file) throws IOException {
this.file = new File(file);
FileInputStream fip = new FileInputStream(this.file);
InputStreamReader reader = new InputStreamReader(fip);
StringBuffer sBuffer = new StringBuffer();
while (reader.ready()) {
sBuffer.append((char) reader.read());
}
String str = sBuffer.toString();
char[] ch = str.toCharArray();
w = 0;
for (char c : ch) {
if (c == '\n' || c == '\r' || c == ' ' || c == ',' || c == ';' || c=='.') {
w++;
}
}
reader.close();
fip.close();
return w;
}
public void outputFile(String file) throws IOException {
File f = new File(file);
FileOutputStream fop = new FileOutputStream(f);
OutputStreamWriter writer = new OutputStreamWriter(fop);
if (c > 0) {
writer.append(this.file.getName() + ",字符数:" + c+"\r\n");
}
if (w > 0) {
writer.append(this.file.getName() + ",单词数:" + w+"\r\n");
}
if (l > 0) {
writer.append(this.file.getName() + ",行数:" + l+"\r\n");
}
writer.close();
fop.close();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/kong1987/wc.git
git@gitee.com:kong1987/wc.git
kong1987
wc
WC的基础功能实现
master

搜索帮助