简体   繁体   English

Eclipse(Java)中的文件路径

[英]File Path in Eclipse (Java)

I am using Eclipse to create a Dynamic Web Project. 我正在使用Eclipse创建动态Web项目。 My eclipse is in 我的月食在

/home/pc/eclipse

and My project is in /home/pc/workspace/MyWebProj 我的项目在/home/pc/workspace/MyWebProj

Now I placed the files in the above Project Directory. 现在,我将文件放在上面的项目目录中。 When I want to read any File from my code, It always searches the files in /home/pc/eclipse folder instead of /home/pc/workspace/MyWebProj , thus I am hardcoding the file path for time being. 当我想从代码中读取任何文件时,它总是在/ home / pc / eclipse文件夹中而不是/home/pc/workspace/MyWebProj搜索文件,因此我暂时对文件路径进行了硬编码。

Is there any configuration setting in Eclipse I am missing? 我缺少的Eclipse中有任何配置设置吗?

Please Suggest. 请提出建议。

Thank You, Tara Singh 谢谢你,塔拉·辛格

I think what you need to do is read the file from the location of the javax.servlet.ServletContext of the Web Application. 我认为您需要做的是从Web应用程序的javax.servlet.ServletContext的位置读取文件。 If you use getContextPath() you should get the working directory of the Web Application rather than the Java VM. 如果使用getContextPath() ,则应获取Web应用程序而不是Java VM的工作目录。

You could also try loading any files using the getResource and getResourceAsStream methods from the same class. 您也可以尝试使用同一类的getResourcegetResourceAsStream方法加载任何文件。

I was a little rushed when I wrote this so I will provide a little more info. 当我写这篇文章时,我有点着急,所以我将提供更多信息。

The best approach is to use the getResourceAsStream method. 最好的方法是使用getResourceAsStream方法。 this allows you to access files within your webapp. 这使您可以访问Webapp中的文件。

To get a file foo.txt from the root directory of the web application and print it to the response, you could do the following. 要从Web应用程序的根目录获取文件foo.txt并将其打印到响应中,可以执行以下操作。

public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws javax.servlet.ServletException, java.io.IOException
    {
        PrintWriter writer = response.getWriter();

        // foo.txt is at the root directory of the web app
        InputStream in = getServletContext().getResourceAsStream("/foo.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String text;
        while ((text = reader.readLine()) != null) {
            writer.println(text);
        }
    }    
}

I'm not sure what these files are, or more specifically, what the most appropriate storage location would be, but traditionally the best method of IO in this case is via ClassLoading . 我不确定这些文件是什么,或更确切的说是最合适的存储位置是什么,但是在这种情况下,传统上最好的IO方法是通过ClassLoading You could create a source folder located at src/main/resources that would be included in the archive (jar, war, ear, etc.) assembly (ie these files would be included in the jar). 您可以在src/main/resources中创建一个源文件夹,该源文件夹将包含在存档(jar,war,ear等)程序集中(即这些文件将包含在jar中)。 You would then load these resources . 然后,您将加载这些资源

If a file is located directly in the package, it will also be included in the artifact assembly, and can be accessed by any class in the package's class loader, or by the default class loader with the package path (packages correspond to folders when an archive is built). 如果文件直接位于包中,则该文件也将包含在工件程序集中,并且可由包的类加载器中的任何类或具有包路径的默认类加载器进行访问(包对应于文件夹存档已建立)。

If you are using a maven assembly plugin, these resources files would be included automatically. 如果您使用的是maven程序集插件,则将自动包含这些资源文件。 If you're using something like Ant, you would probably need to include these files in a script step. 如果您使用的是类似Ant的工具,则可能需要在脚本步骤中包括这些文件。 Otherwise I think if you're simply building in Eclipse there's a wizard for including files. 否则,我认为如果您只是在Eclipse中构建,则可以使用向导来包含文件。

Also, the likely reason for attempting to read files from the /home/pc/eclipse folder is because a system property like user.dir or user.home is being utitlized: 另外,尝试从/ home / pc / eclipse文件夹读取文件的可能原因是因为使用了诸如user.diruser.home类的系统属性

String filename = System.getProperty("user.dir") + System.getProperty("file.separator") + "file.txt";

don't know if it's what you are looking for, but you can set/change the "Working Directory" of each "Running configuration" pretty easily in Eclipse. 不知道它是否在寻找什么,但是您可以在Eclipse中很容易地设置/更改每个“运行配置”的“工作目录”。

  1. Go to the "Run Configurations" Dialog 转到“运行配置”对话框
  2. Select your Project (or the specific running config) 选择您的项目(或特定的运行配置)
  3. Select the "Arguments" tab 选择“参数”标签
  4. In the lower part of the Window, you can now change the "Working Directory" of the running configuration. 现在,在窗口的下部,可以更改正在运行的配置的“工作目录”。
  5. Change it to /home/pc/workspace/MyWebProj and your App should work. 将其更改为/ home / pc / workspace / MyWebProj ,您的应用程序应该可以运行。

Although this might work, i suggest you should go through a SystemProperty variable. 尽管这可能有效,但我建议您应通过SystemProperty变量。 Just get the parent path of your file like this: 只需像这样获取文件的父路径:

File myFile = new File(System.getProperty("com.myapp.myfile.property"));

Now you can reset / move the working dir and filename via the System Properties just by setting this keyword. 现在,只需设置此关键字,即可通过“系统属性”重置/移动工作目录和文件名。

Hope it helps a bit ;) 希望能有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM