import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class wc {
//待统计文件名
private static String filename = null;
//待写入内容,因为类的实例化放在while循环内,所以每次都会新建一个对象,即需用静态
// private static String content = "";
public wc(String filename) {
this.filename = filename;
}
//把结果写入文件
private static void print(String resultFile,String content) {
try {
File file = new File(resultFile);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "gbk"));
System.out.println(content);
writer.write(content);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//得到文件的字符数
private static String getCharnum() {
File file = new File(filename);
long charNum = 0;
int isend = 0;
String content = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
while ((isend = reader.read()) != -1) {
char temp = (char) isend;
if (temp != '/') {
charNum++;
} else { //当字符为/时,如果下一个字符依然为/,则后续内容为注释,不再计数,若不是则字符数增2
char tempSec = (char) reader.read();
if (tempSec == '/') {
String note = reader.readLine();
} else {
charNum += 2;
}
}
}
content = "字符数:" + charNum + "\r\n";
reader.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
//获取文件的行数
private static String getLinenum() {
File file = new File(filename);
int linenum = 0;
String temp = null;
String content = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
while ((temp = reader.readLine()) != null) {
linenum++;
}
content = "行数:" + linenum + "\r\n";
reader.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
//获取文件的字符串数
private static String getWordnum() {
File file = new File(filename);
long wordnum = 0;
String[] array = null;
String content = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
String readline = null;
while ((readline = reader.readLine()) != null) {
array = readline.split("\\s+|,"); //以空格或者逗号作为分隔符
for (String str : array) {
if (str.startsWith("//")) { //剔除内容中的注释
break;
} else {
if (str.trim().equals("")) ; //剔除空行
else {
wordnum++;
}
}
}
}
content = "单词数:" + wordnum + "\r\n";
reader.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
public static void main(String[] args) {
int txtnum = 0; //统计命令中的.txt文件数量,便于获取待处理文件名
for (String str:args){
if (str.endsWith(".txt")){
txtnum++;
}
}
if (txtnum>1){
for (int i=0;i<args.length;i++){
//从指令中获取待处理文件名
if (args[i].endsWith(".txt")&&args[i]!="stop.txt"&&args[i]!=args[args.length-1]){
filename = args[i];
}
}
}
else{
for (String str:args){
if (str.endsWith(".txt")){
filename = str;
}
}
}
String content = ""; //多指令时的内容
wc test = new wc(filename);
for (int i=0;i<args.length-1;i++){
if (args[i].equals("-c")){
content += test.getCharnum();
if (args[i+1].endsWith(args[args.length-1]))
print("result.txt",content);
}
else if (args[i].equals("-w")){
content += test.getWordnum();
if (args[i+1].endsWith(args[args.length-1]))
print("result.txt",content);
}
else if (args[i].equals("-|")){
content += test.getLinenum();
if (args[i+1].endsWith(args[args.length-1]))
print("result.txt",content);
}
else if (args[i].equals("-o")){
if (args[i+1].endsWith(".txt")){
test.print(args[i+1],content);
}
else
System.out.println("不允许单独使用-o指令!");
}
}
// }
}
}