测试哈哈哈哈哈

 

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指令!");

}

}

// }

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan、阿兰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值