0

所以我的程序编译但是在测试早期分析时我意识到它不会打印显示所有输出的文件。有人可以告诉我为什么文件根本没有被打印吗?这是我的代码...

import java.io.*;    
import java.util.*;    

public class Analyzer     
{         

private static FileWriter stream;    
        private static BufferedWriter output;

    public static void start() throws IOException

{       
    int i = 0;
    int total = 0;
    int totalError = 0;
    final int SIZE = 40000;
    Response data[] = new Response[SIZE];

    stream = new FileWriter("apacheOut.txt");
    output = new BufferedWriter(stream);

    Scanner scan = new Scanner("access_log1.txt");
    scan.useDelimiter ("[-\t]+");
    while (scan.hasNextLine()) 
    {
        double address = scan.nextDouble();
        String date = scan.next();
        String request = scan.next();
        int status = scan.nextInt();
        int bytes = scan.nextInt();
        String refer = scan.next();
        String agent = scan.next();

        if (Integer.toString(status).substring(0, 1) == "4" || Integer.toString(status).substring(0, 1) == "5")
        {
            ErrorResponse er = new ErrorResponse (address, date, request, status, bytes, refer, agent);
            totalError++;
        }
        else
        {
            SuccessResponse sr = new SuccessResponse (address, date, request, status, bytes, refer, agent);
        }

        total++;
    }

    int numGet = 0;
    int numPost = 0;
    double numBytes = 0;
    for (i = 0; i <= SIZE; i++)
    {   
        double address = data[i].getAddress();
        String date = data[i].getDate();
        String request = data[i].getRequest();
        int status = data[i].getStatus();
        int bytes = data[i].getBytes();
        String refer = data[i].getRefer();
        String agent = data[i].getAgent();

        /** GET/POST count */
        if (request.substring(0,1) == "G")
        {   
            numGet++;
        }
        else if (request.substring(0,1) == "P")
        {
            numPost++;
        }

        /** Number of total bytes */
        numBytes = bytes++;
    }

    output.write("Warren Smith's Results");
    output.write("======================");
    output.write("The total number of requests in the file: " + total);
    output.write("The total number of GET requests: " + numGet);
    output.write("The total number of POST requests: " + numPost);
    output.write("The total number of bytes served: " + numBytes);
    output.write("The number & percentage of pages producing various status categorizations:" );
    output.write("    1xx Informational: ");
    output.write("    2xx Status: ");
    output.write("    3xx Redirection: ");
    output.write("    4xx Client Error: ");
    output.write("    5xx Server Error: ");
    output.write("The percentage and number of Windows-based clients: ");
    output.write("The percentage and number of bad requests: ");
    output.write("The percentage and number of clients that are Mozilla-based: ");
    output.write("The percentage and number of requests from the Googlebot: ");

    output.close();
}
}
4

2 回答 2

2

总是使用 .equals() 方法来检查两个字符串是否相等 改变这个

    if (Integer.toString(status).substring(0, 1) == "4" || Integer.toString(status).substring(0, 1) == "5")

if (Integer.toString(status).substring(0, 1).equals("4") || Integer.toString(status).substring(0, 1).equals( "5"))
于 2012-11-01T23:09:35.213 回答
0

完全没有输出?您正在写入BufferedWriter outputwhich wraps FileWriter stream。但是,您只是关闭(并自动刷新)输出流。FileWriter 可能会自行缓冲,如果您在完成写入之前终止程序,它可能永远不会将更改提交到磁盘。

output.close();尝试添加后:

stream.flush();
stream.close();
于 2012-11-02T03:00:34.917 回答