输出未打印到文件中,假设使用FileWriter

时间:2012-11-01 23:06:40

标签: java filewriter bufferedwriter

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

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();
}
}

2 个答案:

答案 0 :(得分: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"))

答案 1 :(得分:0)

根本没有输出?您正在写信BufferedWriter output,其中包含FileWriter stream。但是,您只关闭(并自动刷新)输出流。 FileWriter可以自己缓冲,如果在完成编写之前终止程序,它可能永远不会将更改提交到磁盘。

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

stream.flush();
stream.close();