Ai
1 Star 0 Fork 0

万朋鑫/Word Count

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Test.java 10.87 KB
一键复制 编辑 原始数据 按行查看 历史
万朋鑫 提交于 2018-09-24 15:11 +08:00 . Upload Test.java
/**
* @param
* @param
* @Author
* @return
*/
import java.io.*;
import java.util.ArrayList;
public class Test {
static boolean c_flag=false;//字符数
static boolean w_flag=false;//单词数
static boolean l_flag=false;//文件行数
static boolean o_flag=false;//指定输出文件
static boolean a_flag=false;//代码行、空行、注释行
static boolean s_flag=false;//路径指令判断
static boolean file_flag=false;
static boolean all_flag=false;
static boolean star_flag=false;
static boolean path_flag=false;
static String txt_name;//输入文件名
static String pathname;//指定路径名
static String file_name;//输出文件名
static String output_name;
public static void main(String[] args){
if(args[0].equals("-c")||args[0].equals("-w")||args[0].equals("-l")||args[0].equals("-*")||args[0].equals("-a")||args[0].equals("-s")){
for(int i=0;i<args.length;i++)
{
if(args[i].contains("/")||args[i].contains("\\")){
pathname=args[i];
path_flag=true;
file_flag=true;
}
else{
judge(args[i]);
}
}
}
else {
System.out.println("请确认指令不为空或者命令错误");
}
if(s_flag==true){//有路径指令的情况下
if(path_flag==false){
System.out.println("命令格式不正确:有路径指令但是没有指定路径");
}
else{
files_output(pathname);
}
}
else if(path_flag==true&&s_flag==false){
System.out.println("输入的指令不正确:有路径名但是没有路径指令");
}
else{
work();
}
}
private static void work() {
// TODO Auto-generated method stub
if(o_flag==true){
if(all_flag==false)
System.out.println("命令格式不正确:有输出指令但是没有指定输出文件");
}
if(a_flag==true){
String Command="代码行/空行/注释行";
int []num=code_ana(file_name);
System.out.print(txt_name+","+Command+":"+num[0]+"/"+num[1]+"/"+num[2]+"\r\n");
}
if(c_flag==true){
String Command="字符数";
System.out.print(txt_name+","+Command+":"+num_of_char(file_name)+"\r\n");
}
if(l_flag==true){
String Command="行数";
System.out.print(txt_name+","+Command+":"+num_of_line(file_name)+"\r\n");
}
if(all_flag==true){
if(a_flag==true){
String Command="代码行/空行/注释行";
code_output(output_name,Command,code_ana(file_name));
}
if(c_flag==true){
String Command="字符数";
output(output_name,Command,num_of_char(file_name));
}
if(w_flag==true){
String Command="单词数";
output(output_name,Command,num_of_word(file_name));
}
if(l_flag==true){
String Command="行数";
output(output_name,Command,num_of_line(file_name));
}
System.out.println("运行并且输出内容保存成功");
}
else {
System.out.println("本指令没有指定输出文件,所以输出内容没有保存");
}
}
private static void judge(String str) {
// TODO Auto-generated method stub
if(str.equals("-s")&&s_flag==false){
s_flag=true;
}
else if(str.equals("-*")&&star_flag==false){
star_flag=true;
c_flag=true;
w_flag=true;
l_flag=true;
a_flag=true;
}
else if(str.equals("-a")&&a_flag==false){
a_flag=true;
}
else if(str.equals("-c")&&c_flag==false){
c_flag=true;
}
else if(str.equals("-w")&&w_flag==false){
w_flag=true;
}
else if(str.equals("-l")&&l_flag==false){
l_flag=true;
}
else if(file_flag==false)//还没有指定输入文件
{
if(s_flag==false){//如果输入的不是路径
file_name=str;
txt_name=str;
}
file_flag=true;
}
else if(str.equals("-o")&&o_flag==false){
o_flag=true;
}
else if(o_flag==true&&file_flag==true){//已经有输入文件且o_flag为真但是没有指定输出文件
output_name+=str;
all_flag=true;
}
else{
System.out.println("指令判断完成");
}
}
//字符统计
private static int num_of_char(String filename) {
// TODO Auto-generated method stub
System.out.println(filename);
File file=new File(filename);
Reader readfile=null;
int c_num=0;
try{
readfile = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar=readfile.read()) != -1) {
if((char)tempchar!='\r'&&(char)tempchar!='\n'){
c_num++;
}
}
readfile.close();
}
catch(Exception e){
e.printStackTrace();
System.out.println("指定输入文件不存在");
}
return c_num;
}
//单词统计
private static int num_of_word(String filename){
File file=new File(filename);
Reader readfile=null;
boolean letter_flag=false;
int w_num=0;
try{
readfile = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar=readfile.read()) != -1) {
if(IsLetter((char)tempchar)){
letter_flag=true;
}
else if(letter_flag==true){
letter_flag=false;
w_num++;
}
}
readfile.close();
}
catch(Exception e){
System.out.println("指定输入文件不存在");
}
return w_num;
}
//行数统计
private static int num_of_line(String filename) {
// TODO Auto-generated method stub
File file=new File(filename);
Reader readfile=null;
int l_num=1;
try{
readfile = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar=readfile.read()) != -1) {
if((char)tempchar=='\n'){
l_num++;
}
}
readfile.close();
}
catch(Exception e){
System.out.println("指定输入文件不存在");
}
return l_num;
}
//扩展功能——空行、代码行、注释行统计
private static int [] code_ana(String filename){
File file=new File(filename);
int nothing=0;
int line=0;
int note=0;
int code_line=0;
boolean note_flag=false;
BufferedReader readfile = null;
try{
readfile = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = readfile.readLine()) != null) {
line++;
tempString=tempString.replaceAll("\r\n"," ");//去掉所有换行符和空格
if(note_flag==true){
note++;
if(tempString.endsWith("*/")){
note_flag=false;//代表注释内容在本行结束
}
}
if(tempString.equals(" ")||tempString.equals("{")||tempString.equals("}")){
nothing++;
}
if(tempString.startsWith("//")||tempString.startsWith("{//")){
note++;
}
if(tempString.startsWith("/*")||tempString.startsWith("{/*")){
if(tempString.endsWith("*/")){
note++;
}
else{
note++;
note_flag=true;//代表注释的内容在本行还没结束
}
}
code_line=line-note-nothing;
}
readfile.close();
}
catch(Exception e){
System.out.println("指定输入文件不存在");
}
int []num =new int[3];
num[0]=code_line;
num[1]=nothing;
num[2]=note;
return num;
}
private static void output(String output_file, String com,int num) {
// TODO Auto-generated method stub
File txt=new File(output_file);
try{
FileWriter fw=new FileWriter(txt,true);//参数为true代表可以追加写入
BufferedWriter out = new BufferedWriter(fw);
out.write(txt_name+","+com+":"+num+"\r\n");
out.close();
}
catch(IOException e){
System.out.println("指定输出文件不存在");
}
}
private static void code_output(String output_file, String com,int []num) {
// TODO Auto-generated method stub
File txt=new File(output_file);
try{
FileWriter fw=new FileWriter(txt,true);//参数为true代表可以追加写入
BufferedWriter out = new BufferedWriter(fw);
out.write(txt_name+","+com+":"+num[0]+"/"+num[1]+"/"+num[2]+"\r\n");
out.close();
}
catch(IOException e){
System.out.println("指定输出文件不存在");
}
}
private static void files_output(String path_name){
String []path=path_name.split("\\*");
File file=new File(path[0]);
if(file.exists()){
File[]files=file.listFiles();
if(files!=null){
for(File f:files){
if(f.getName().endsWith(path[1])){
txt_name=f.getName();
file_name=f.getAbsolutePath();
work();
}
}
}
else{
System.out.println("文件夹内容为空");
}
}
else{
System.out.println(path[0]+":指定路径或文件不存在");
}
}
//选项越界判断
private static boolean IsLetter(char c){
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
return true;
}
return false;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/godcoder979/work.git
git@gitee.com:godcoder979/work.git
godcoder979
work
Word Count
master

搜索帮助