java 写到文件_java如何把输出写到文件中?

importjava.io.*;importjava.io.IOException;importjavax.servlet.ServletConfig;importjavax.servlet.ServletException;importjavax.servlet.http.*;publicclassTestLiftCycleServle...

import java.io.*;

import java.io.IOException;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.*;

public class TestLiftCycleServlet extends HttpServlet {

@Override

public void destroy() {

System.out.println("destroy");

}

@Override

public void init(ServletConfig config) throws ServletException {

System.out.println("init");

}

public TestLiftCycleServlet() {

System.out.println("constructor!");

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

System.out.println("doGet");

}

}

就是把system.out.println()的内容输出到文件里去,我想用java的方法实现,不是想记录日志,能不能写具体一点,File file =null;

PrintWriter pw = null;

try{

file = new File("c:\\6.txt");

pw = new PrintWriter(file);

pw.println("destroy");

} catch (Exception e ){

e.printStackTrace();

}

pw.close();

这个为什么不行?

zwxlovellm的方法不行

展开


版权声明:本文为weixin_36156538原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。