[英]How can I append user input to a new line whilst also avoiding possible duplicates within my .CSV file using FileWriter?
[英]How can I create a file with FileWriter within my project file using relative path
我正在使用NetBean和glassFish开发jsp网站及其服务器。 在服务器端,我有一个Java类,该类使用FileWriter生成文本文件:
FileWriter writer = new FileWriter("the-file-name.txt", true);
但是我发现此文件保存在glassfish的目录中:
C:\\ Program Files(x86)\\ glassfish-4.0 \\ glassfish \\ domains \\ domain1 \\ config
另外,通过代码检查路径:
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);
结果也与上述路径相同。
如何获得正确的相对路径,无论是项目路径还是此java文件的路径?
这将获取您正在运行的类/ jar的位置:
String path = System.getProperty("user.dir");
字符串路径包含绝对目录,并使用类似
path = path.replace("jars", "");
,您可以找到该应用程序的根目录。 例如,如果jar位于目录C:\\Program Files\\Example\\jars
,则将路径设置为
C:\\Program Files\\Example\\
在Servlet中
String filePath = getServletContext().getRealPath("/").replace("\\", "/");
在JSP中
String filePath = session.getServletContext().getRealPath("/").replace("\\", "/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.