哪里可以在Eclipse中找到由servlet创建的文本文件 [英] Where i Can find text file created by servlet in Eclipse

查看:465
本文介绍了哪里可以在Eclipse中找到由servlet创建的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我不得不问,因为找不到任何正确的解决方案。

This may be a stupid question, but I have to ask because I couldn't find any proper solution.

我是Eclipse的新手。我在Eclipse中创建了一个动态Web项目,在这里,我写了一个简单的代码来创建一个文本文件,只有文件名被指定不是创建的路径,成功执行后,我找不到我的文档在我的项目文件夹。

I am new to Eclipse. I created a Dynamic Web project in Eclipse, In this, I write a simple code to create a text file, Only file name is specified Not the path that where to create, After successful execution, i could not find my text file in my project folder.

如果代码中指定了路径,我可以在指定的目录中找到文本文件,如果我没有指定,我的问题是我可以找到我的文本文件路径?

If path is specified in the code, I can find the text file in specified directory, My Question is where i can find my text file if i am not specify a path ?

我的代码是

try {
     FileWriter outFile = new FileWriter("user_details.txt", true);
     PrintWriter out1 = new PrintWriter(outFile);
     out1.append(request.getParameter("un"));
     out1.println();
     out1.append(request.getParameter("pw"));
     out1.close();
     outFile.close();
     System.out.println("file created");
} catch(Exception e) {
     System.out.println("error in writing a file"+e);
}

我用以下行编辑了我的代码,

I edited my code with following lines,

String path = new File("user_details.txt").getAbsolutePath(); 

System.out.println(path); 

我获得的路径在

D:\Android\eclipse_JE\eclipse\user_details.txt

为什么我得到它eclipse文件夹?

Why i got it in the eclipse folder ?

然后,
如何创建文本文件,如何创建文本文件? / p>

Then, How can i create a text file in my web app, if this is not the right way to create a textfile ?

推荐答案

该文件位于应用程序服务器的实际工作目录中。做一个

The file is located in the actual working directory of your application server. Do a

System.out.println(new File("").getAbsolutPath());

,你会找到位置。

然而,这不是一个好主意,在这样的Web应用程序中写入文件,因为首先你不知道它在哪里,其次你永远不知道你是否写权限。

However this is not a good idea to write files in web application like this, because first you never know where it is and second you never know whether you write privilege on it.

您需要为您的应用程序指定一些文件系统根,方法是将其作为 init-parameter 传递,并将其用作所需的所有内容在文件系统上执行。检查此答案到类似的问题

You need to specify some filesystem root for your application by passing it as init-parameter and use it as parent for everything you need to do on the filesystem. Check this answer to a similar Question.

你可以这样创建你的文件:

You could then create your file like this:

String fsroot = getServletContext().getInitParameter("fsroot")
File ud = new File(fsroot, "user_details.txt");
FileWriter outFile = new FileWriter(ud, true);

这篇关于哪里可以在Eclipse中找到由servlet创建的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆