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() メソッドを使用して、2 つの文字列が等しいかどうかを確認します。これを変更します。

    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 outputどのラップに書いていますFileWriter stream。ただし、出力ストリームを閉じる (そして自動的にフラッシュする) だけです。FileWriter は独自にバッファリングする場合があり、書き込みが完了する前にプログラムを終了すると、変更がディスクにコミットされない場合があります。

output.close();追加してみてください:

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