用"javac test.java"无法编译通过,报错
“test.java:41:unreported exception java.io.IOException;must be caught or declared to be thrown”
“fw = new FileWriter("C:\\test\\test.txt",true)”package java.util;import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;public class test extends HttpServlet
        implements Runnable
{
FileWriter fw ;
PrintWriter pw ;
String data;

public void init (ServletConfig config)
        throws ServletException
{
    super.init(config);
    letItBegin(config);
}public synchronized void destroy()
{
    super.destroy();    
}protected void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
{
    letItBegin(null);
}protected void letItBegin(ServletConfig config) throws ServletException
{
me = new Thread(this);
        me.start();
}public void run()
{
    fw = new FileWriter("C:\\test\\test.txt",true);
    pw = new PrintWriter(fw);
    data = "0";
    
    while (true)
    {
     pw.write(data);
     pw.println();
     pw.flush();
     fw.close();
     pw.close();
     Thread.sleep(1000);
    }
}

}

解决方案 »

  1.   

    fw = new FileWriter("C:\\test\\test.txt",true); 需要做异常处理改成try {
    FileWriter fw = new FileWriter("C:\\test\\test.txt",true);
    } catch (IOException e) {
    e.printStackTrace();
    }
      

  2.   

    fw = new FileWriter("C:\\test\\test.txt",true); 需要做异常处理改成try {
    FileWriter fw = new FileWriter("C:\\test\\test.txt",true);
    } catch (IOException e) {
    e.printStackTrace();
    }
      

  3.   

    sorry刚才有点问题,应该是
    try {
    fw = new FileWriter("C:\\test\\test.txt",true);
    } catch (IOException e) {
    e.printStackTrace();
    }
      

  4.   

    ok
    我改了
    但是还有一个错误
    test.java:65:unreported exception java.lang.InterruptedException;must be caught or declared to be thrown”
    “Thread.sleep(1000)”
      

  5.   

    fw = new FileWriter("C:\\test\\test.txt",true);需要检查异常
    把它包含在try块里,catch一个IOException
      

  6.   

    我明白了,要改成
    try
            {
                Thread.sleep(sleep_time*1000);
            } 
            catch (InterruptedException ix)
            {
                ;
            }
    谢谢
      

  7.   

    Thread.sleep(1000);也需要检查异常
    也把它包含在块里,catch一个InterruptedException