如何将System.getProperty(" user.dir")更改为项目工作区 [英] How to change System.getProperty("user.dir") to project workspace

查看:418
本文介绍了如何将System.getProperty(" user.dir")更改为项目工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的代码中逐行读取.txt文件,我把它放在 / src / 目录下,当我用test运行它时case或static void main,路径输出正确。但是,当我使用Tomcat服务器运行应用程序时,应用程序根路径指向我下载Eclipse的位置 - D:\ eclipse \ ... ,而正确路径应该是 D:\ workspace \myproject\src \ 。然后,当然,它永远找不到文件。

I try to read a .txt file line by line in my code, which I placed it just right under the /src/ directory, when I run it with test case or with static void main, the path output is correct. However, when I run the application with Tomcat server, the app root path points to where I download my Eclipse - D:\eclipse\..., while the correct path should be D:\workspace\myproject\src\. Then, of course, it can never find the file.

以下是我的代码:

String workDir = System.getProperty("user.dir");
String file = "numFile.txt";
File myFile = new File(workDir + file);
String userPath = myFile.getPath();

所以,我的问题是:


  1. (这可能是愚蠢的)我们通常应该在哪里放置文本文件?

  2. 如何更改[ System.getProperty(user。 dir); ],它会指向我的项目工作区吗?

  1. (this maybe dumb) Where should we normally place a text file?
  2. How can I change [System.getProperty("user.dir");], so it will point to my project workspace?

谢谢!

Sharon

关于你的回复:

添加以下参数-Duser.home ='Your Path'确保在系统变量的开头添加-D。当您在使用tomcat服务器时打开启动配置时,可以将此变量放在参数选项卡下提供的VM参数框中。

add following arguments -Duser.home='Your Path' make sure you add -D at the begining of your system variable. And this variable you can put in the VM Arguments box provided under arguments tab when you Open the Launch Configuration when using tomcat server.

我找不到您要说的地方。是在Eclipse还是Tomcat目录?

I cannot find the place you are talking about. Is it in Eclipse or Tomcat directory?

谢谢

推荐答案

试试文件myFile =新文件(workDir,file);

这篇关于如何将System.getProperty(" user.dir")更改为项目工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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