2012-06-30 112 views
1

我使用提交按钮创建HTML输入表单。表单动作将我们带到一个jsp页面。 我写这个我JSP页面上JSP写入文本文件

 ServletContext context = this.getServletContext(); 

     String path = context.getRealPath("WEB-INF/list.txt"); 


     User user = new User(fName, lName, eAddress, phone, company, webinar,dateObj); 
     UserIO.add(user, path); 

然后,我创建了一个名为UserIO

public class UserIO { 

    public static void add(User user, String path) throws IOException 
    { 
     FileOutputStream fos= new FileOutputStream(path, true); 

     PrintWriter out = new PrintWriter(fos);//, true)); 

     out.println(user.getFname() + "|" + user.getLname() + "|" + user.getEmail() + "|" ); 
     out.println(user.getPhone() + "|" + user.getCompany() + "|" + user.getWebinar() + "|" + user.getDate()); 

     out.close(); 
    } 

} 

现在我的问题是,输出显示在JSP页面中,但不保存到文本文件中的Java类。

我已经在Netbeans中完成了这个程序并保存了<projectname>/web/WEB-INF下的文件。

我试图将路径更改为<projectname>/web/WEB-INF/list.txt,但放弃了错误信息。所以坚持像上面那样改变它。

告诉我你的专家opion如何解决这个问题?

回答

0

您必须提供FileOutputStream正确的路径!
是否有任何文件夹,您的eclipse目录中名为WEB-INF。我想不是!
尝试使用这些线路来了解正在创建的文件,其中:

File file = new File("list.txt"); 
System.out.println(file.getCanonicalFile()); 


+0

我在netbeans中这样做。当我创建Java Web应用程序时 - 创建WEB-INF文件夹。 –

+0

但我不认为它在NetBeans(应用程序)文件夹中。我相信WEB-INF是在你的工作空间的某个地方创建的,完全不同。照我说的去做,你会看到创建文件的位置。 –

0

我想,我找到你的答案:
尝试使用这样的:

(我假设该文件被找到)
要写在你的文件,你应该用这些代码替换你的代码:

FileOutputStream fos= new FileOutputStream(path, true); 
fos.write((user.getFname() + "|" + user.getLname() + "|" + user.getEmail() + "|").getBytes()); 
fors.write((user.getPhone() + "|" + user.getCompany() + "|" + user.getWebinar() + "|" + user.getDate()).getBytes()); 


完成!

1

查找项目build \ web \ WEB-INF \'目录()中的txt文件。 我面临同样的问题,然后我在给定的目录中找到了文本文件。 :-)顺便说一句,我说的是,如果你使用Netbeans IDE ..