package com.hyj.Text;
import java.io.File;
import java.util.Scanner;
public class TextDeal {
    /**
    * @param args
    */
    //设定的最大创建线程的数量
    public final static int THREAD_NUMBER=100;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File file = new GetFileInformation().getFile();// 获取文件
        int threadNum = getThreadNum();// 获取要创建的线程个数
        new DealText(file, threadNum).dealFile();// 处理文件
    }
    // 获得要创建的线程数量
    private static int getThreadNum() {
        // TODO Auto-generated method stub
        int num = 0;
        Scanner input = new Scanner(System.in);
        try {
            System.out.print("请输入您要创建的处理文本线程数(不能超过" +THREAD_NUMBER+"个):");
            num = Int();
            while (num > THREAD_NUMBER || num <= 0) {
                System.out.print("您要要创建的线程数量不正确,请重输入(不能超过"+THREAD_NUMBER+"个):");
                num = Int();
            }
        } catch (Exception e) {
            System.out.print("您输入的线程个数的类型不正确!系统将终止运行!");
            e.printStackTrace();
            it(-1);// 退出系统
        }
        return num;
    }
}
package com.hyj.Text;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class GetFileInformation {
    // 通过控制台获得要处理的文件
    public final File getFile() {
        String filePath = null;
        File file = null;
        System.out.print("请输入您要统计的文件的路径:");
        //从控制台读入文件路径
        BufferedReader stdin = new BufferedReader(new InputStreamReader(
                System.in));
        try {
            //将路径转换成小写的形式
            filePath = adLine().toLowerCase();
            // 判断输入的文件是否存在,以及文件类型(只识别txt)
            while (!new File(filePath).exists() || !dsWith(".txt")) {
                System.out.print("您输入的文件路径或者文件类型不正确!请重新输入:");
                filePath = adLine();
            }
            file = new File(filePath);// 还可以在判断文件类型等等
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("系统出现错误!");
            it(-1);// 直接退出系统
        }
        return file;
    }
}
package com.hyj.Text;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
/*
* 注意最终只用了result数组的一维数组存储结果,数组的下标表示
* 该字母,文字,符号等等在unicode编码中的位置,数组的内容为出现次数
*/
public class DealText {
    // 定义数组长度
    final int RESULT_LENGTH = 100000;
    // 要处理的文件
    private File file;
    // 线程数数组
    private DealThread[] threads;
    // 存储各线程的处理结果
    private int[][] statistics;
    // 统计各线程处理结果的总和
    private int[] result = new int[RESULT_LENGTH];
    public DealText(File file, int threadNum) {
        // TODO Auto-generated constructor stub
        this.file = file;
        this.threads = new DealThread[threadNum];
        this.statistics = new int[threadNum][];
    }
    // 文件处理
    public final void dealFile() {
        if (this.file.length() < 0) {
            System.out.println("文件大小无法获取!");字符串长度统计
            System.out.println("文件可能已经损坏而无法操作,因此系统将退出");
            it(-1);// 退出系统
        } else {
            long startTime = new Date().getTime();// 获得文件处理开始时间
            createThreads();// 创建线程并启动
            judgeTheThreads(startTime);// 判断各线程是否完成
            resultOfDealText();// 统计结果
            printResultToFile();// 输出统计结果
        }
    }
    // 创建线程并启动
    public void createThreads() {
        // 创建多个输入流,每个线程一个
        InputStream[] inputStream = new InputStream[this.threads.length];
        // 每线程应该读取的字节数
        long numPerThred = this.file.length() / this.threads.length;
        // 整个文件整除后剩下的余数
        long left = this.file.length() % this.threads.length;
        for (int i = 0; i < this.threads.length; i++) {
            // 为每个线程打开一个输入流
            // 让每个线程分别负责读取文件的不同部分。
            try {
                inputStream[i] = new FileInputStream(this.file);
                if (i == this.threads.length - 1) {
                    // 最后一个线程读取指定numPerThred+left个字节
                    this.threads[i] = new DealThread(i * numPerThred, (i + 1)
                            * numPerThred + left, inputStream[i]);
                    this.threads[i].setPriority(7);
                    this.threads[i].start();
                } else {
                    // 每个线程负责读取一定的numPerThred个字节
                    this.threads[i] = new DealThread(i * numPerThred, (i + 1)
                            * numPerThred, inputStream[i]);
                    this.threads[i].setPriority(7);
                    this.threads[i].start();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("系统出现严重错误!系统将终止运行!");
                e.printStackTrace();
                it(-1);// 系统退出
            }
        }
    }
    // 判断各线程是否完成
    public void judgeTheThreads(long startTime) {
        boolean notFinish = true;// 文件处理未完成
        while (notFinish) {// 循环判断是否处理完毕
            try {
                Thread.sleep(10);
                notFinish = false;// 假定各线程处理完成
                for (int i = 0; i < this.threads.length; i++) {
                    if (!this.threads[i].isFinish()) {
                        notFinish = true;// 文件处理没有完成
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("系统出现严重错误!系统将终止运行!");
                e.printStackTrace();
                it(-1);// 系统退出
            }
        }
        long endTime = new Date().getTime();// 获得文件处理结束的时间
        long time = endTime - startTime;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。