为什么我的java项目运行得如此之慢?

时间:2016-09-10 16:44:58

标签: java

EDITED 我写了一个程序来读取文件并计算行,单词和字符数。如果有,则需要命令行参数,否则请求用户输入文件名。但是当我尝试运行它时,它需要永远;它虽然建立成功。我不知道为什么这样做。有人可以帮忙吗?谢谢!这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class project2 {

static int lines = 0, words = 0, bytes = 0,
        totalLines = 0, totalWords = 0, totalBytes = 0;
static String[] filename;
static int i = 0;
static BufferedReader inFile = null;
//static File currentFile = new File(filename[i]);

public static void main(String[] args) throws IOException {

    if (args.length > 0) {  // if there are arguments on commandline
        for (int j = 0; j < args.length; j++) {
            filename[i] = args[j];
        }

    } else {  // prompt user to input file names
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String userInput = input.readLine();  // read the user inputs as a string
        System.out.println("Please enter one or more file names, comma-separated: ");

        userInput = userInput.replaceAll(" ", ""); // delete all the whitespaces
        filename = userInput.split(",", -1);  // split the line into sub-strings by comma
    }

    System.out.println("This program determines the quantity of lines, "
            + "words, and bytes in a file or files that you specify.");
    System.out.println("%n");

    for (i = 0; i < filename.length; i++) {
        Count();
        totalLines = totalLines + lines;
        totalWords = totalWords + words;
        totalBytes = totalBytes + bytes;
    }

    System.out.format("%10s%10s%10%n", "Lines", "Words", "Bytes");
    System.out.format("%10s%10s%10%n", "--------", "--------", "--------");

    Print();
    for (i = 0; i < filename.length; i++) {
        inFile.close();
    }

    //return;
}

public static void Count() throws IOException {
    inFile = new BufferedReader(new FileReader(filename[i]));
    String currentLine = inFile.readLine();
    while (currentLine != null) {
        lines++;
        String[] WORDS = currentLine.split(" "); // split the string into sub-string by whitespace
        // to separate each words and store them into an array
        words = words + WORDS.length;
        for (String word : WORDS) {
            bytes = bytes + word.length();
        }
        currentLine = inFile.readLine();

    }

}

public static void Print() {
    for (i = 0; i < filename.length; i++) {
        System.out.format("%10d%10d%10d%-15s%n", lines,
                words, bytes, filename[i]);
    }
    if (filename.length < 2) {  // if there's only one file, do not print out anything else

    } else {  
        for (int j = 0; j < 45; j++) {
            System.out.print("-");
        }
        System.out.format("%10d%10d%10d%-15s", totalLines, totalWords, totalBytes, "Totals");

    }
}

1 个答案:

答案 0 :(得分:1)

问题在于这个块:

for (i=0;i<filename.length;i++){

        totalLines = totalLines + lineCount();
        totalWords = totalWords + wordCount();
        totalBytes = totalBytes + byteCount();
}

您的每个lineCount()wordCount()byteCount()方法都会分别读取filename数组中的每个文件,这意味着1个文件正在读取行 - 按行3次。这种IO开销是不必要的。尝试重构代码,以便您读取每个一次,并计算该行的wordCount和byteCount,直到该行在内存中。

这样的事情:

while ((line = inFile.readLine()) != null){
    lineCount++;
    wordCount += countWords(line);
    byteCount += countBytes(line);
}

当然,您必须实施countWordscountBytes来操作一行。

希望这有帮助,祝你好运!