如何读取 Java 服务器页面目录中的 TXT 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9716779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Read a TXT file in Java Server Page Directory
提问by farissyariati
I'd like to create an app that requires to read a .txt
file on my project directory.
我想创建一个需要读取.txt
项目目录中文件的应用程序。
This is my code of my index.jsp
:
这是我的代码index.jsp
:
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Read Text</title>
</head>
<body>
<%
BufferedReader reader = new BufferedReader(new FileReader("aFile.txt"));
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine())!= null){
sb.append(line+"\n");
}
out.println(sb.toString());
%>
</body>
</html>
When I execute the above code, my browser tells me that aFile.txt
cannot be found. Then, I've put aFile.txt
in the same directory as this web page runs (index.jsp
).
I wonder, what should I write to locate the directory of aFile.txt
当我执行上面的代码时,我的浏览器告诉我aFile.txt
找不到。然后,我将其放入aFile.txt
与此网页运行相同的目录中 ( index.jsp
)。我想知道,我应该写什么来定位目录aFile.txt
And this is how my problem was solved. Thanks Ahmad hasem
这就是我的问题是如何解决的。感谢艾哈迈德·哈森
<%@page import="java.io.File"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.net.URL"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Read Text</title>
</head>
<body>
<%
String jspPath = session.getServletContext().getRealPath("/res");
String txtFilePath = jspPath+ "/aFile.txt";
BufferedReader reader = new BufferedReader(new FileReader(txtFilePath));
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine())!= null){
sb.append(line+"\n");
}
out.println(sb.toString());
%>
</body>
</html>
采纳答案by Sivasubramaniam Arunachalam
Try to locate it from application's root path.
尝试从应用程序的根路径中找到它。
回答by Rakesh Patel
where is jsp file and text file.are you put both in web-inf folder.please remove txt file from web-inf because you are need servlet if you want to access txt file from web-inf.
jsp 文件和文本文件在哪里。你把它们都放在 web-inf 文件夹中。请从 web-inf 中删除 txt 文件,因为如果你想从 web-inf 访问 txt 文件,你需要 servlet。
use the below code for get the path of the text file.
使用以下代码获取文本文件的路径。
this
.getServlet()
.getServletContext()
.getRealPath(FOLDER NAME)
.concat(System.getProperty("file.separator")
.concat(FILE NAME));
pass the above code in file object.
在文件对象中传递上面的代码。
回答by Ahmed Hashem
To get the directory of the running JSP, you can call the following code :
要获取正在运行的 JSP 的目录,可以调用以下代码:
String jspPath = session.getServletConfig().getServletContext().getRealPath("/");
This code assumes that the JSP resides on the root of your web application. Then, you can append the txt file name to the jspPath
此代码假定 JSP 位于 Web 应用程序的根目录中。然后,您可以将 txt 文件名附加到jspPath
String txtFilePath = jspPath + java.util.File.separator + "aFile.txt";
回答by McDowell
You should not assume that the file can be read using the file system. It is a deployment and implementation detail as to whether the WAR file is "exploded" or not.
您不应假设可以使用文件系统读取文件。它是关于 WAR 文件是否“爆炸”的部署和实现细节。
Assuming that aFile.txt
is in the root of your application, you should be able to open the stream using the servlet context:
假设它aFile.txt
位于应用程序的根目录中,您应该能够使用 servlet 上下文打开流:
<% java.io.InputStream in = application.getResourceAsStream("/aFile.txt"); %>
There are also more appropriate ways to inline other files into a JSP.
还有更合适的方法将其他文件内联到 JSP 中。
The JSP include standard action:
JSP 包括标准操作:
<jsp:include page="aFile.txt" />
The JSTLimport tag:
该JSTL进口标签:
<%-- header --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
....
<c:import url="/aFile.txt" />
I am assuming this code is being written for educational purposes. No modern application should include <% %>
scriptlets.
我假设此代码是出于教育目的而编写的。任何现代应用程序都不应包含<% %>
scriptlet。
回答by mike
As I have experienced it will read from the root directory of your webserver. Actually from the directory you started the webserver. In Tomcat it will be /home/mike/tomcat. Naturally, because it was from this directory you started your java virtual machine and java will consider this its root directory.
正如我所经历的,它将从您的网络服务器的根目录中读取。实际上从您启动网络服务器的目录中。在 Tomcat 中,它将是 /home/mike/tomcat。自然,因为它是从这个目录启动你的 java 虚拟机,java 会认为这是它的根目录。