OutputStreamWriter不将文本写入文本文件

时间:2014-10-07 09:49:10

标签: java web-services webmethod

在我的Java Web服务中,我想将每个传入的请求保存在文本文件中。

这是我的Java网络服务测试方法:

@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
    String strFinal = "Hello " + txt + " !";

    MessageContext mc = wsContext.getMessageContext();
    HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST);
    csGlobal.RequestIP = req.getRemoteAddr();

    csGlobal.Logger(strFinal, csGlobal.RequestIP);

    return strFinal;
}

这是我的csGlobal.Logger功能。 csGlobal是Java Class

public static void Logger(String Message, String strIP)
{
    java.io.OutputStreamWriter SW = null;

    synchronized (exceptionLocker)
    {
        try
        {
            String DirectoryName = new Date().toLocaleString().substring(0, 10);
            String strLogFile = new Date().toLocaleString().substring(0, 13);
            if (!(new java.io.File(RootDir + "\\Exception\\Log\\" + DirectoryName)).isDirectory())
            {
                (new java.io.File(RootDir + "\\Exception\\Log\\" + DirectoryName)).mkdir();
            }
            OutputStream oSW = new FileOutputStream(RootDir + "\\Exception\\Log\\" + DirectoryName + "\\" + strLogFile + ".log");

            SW = new java.io.OutputStreamWriter(oSW);
            SW.write("Date/Time: " + new Date().toLocaleString());
            SW.write(System.getProperty("line.separator"));

            SW.write("IP: " + strIP);
            SW.write(System.getProperty("line.separator"));

            SW.write("Log: " + Message);
            SW.write(System.getProperty("line.separator"));
        }
        catch(IOException ex)
        {

        }
        finally
        {
            if (SW != null)
            {
                SW.close();
            }
        }
    }        
}        

它返回输入字符串,但不保存任何文本到目录中,甚至不创建目录文本文件。

可能出现什么问题?

0 个答案:

没有答案