简体   繁体   English

如何在liferay中读取文本文件?

[英]How Can I read text file in liferay?

I have tried to read a file from controller class with this code 我试图用此代码从控制器类中读取文件

    ReadFile readFile = new ReadFile();
    String text = readFile.readFile("\resources\testing.txt");
    renderRequest.setAttribute("text", text);

I have fetched it from view.jsp as 我已经从view.jsp中获取了它

    <%
    String content = (String)request.getAttribute("text");
    %>

But I am getting file not found exception. 但是我收到文件找不到异常。 What is the way to get file content. 获取文件内容的方法是什么。

In a Java string, \\ starts an escape sequence and "\\r" and "\\t" are escaped characters. 在Java字符串中, \\开始一个转义序列,并且“ \\ r”和“ \\ t”是转义字符。 For example "\\t" is a string with the tab character. 例如,“ \\ t”是带有制表符的字符串。 If you literally need \\t in a string, you'll have to escape the backslash 如果字面上需要\\t ,则必须转义反斜杠

doSomething("\\resources\\testing.txt");

or just eliminate the hassle: Java operates well when you use the forward slash as directory separator 或只是消除麻烦:当您使用正斜杠作为目录分隔符时,Java运行良好

doSomething("/resources/testing.txt");

Note that this refers to a file in the root directory of whatever drive the current path is on, it might be C:\\resources\\testing.txt or D:\\resources\\testing.txt - unless your ReadFile implementation manipulates the path somehow (which I leave up to your judgement). 请注意,这是指当前路径所在的任何驱动器的根目录中的文件,它可能是C:\\ resources \\ testing.txt或D:\\ resources \\ testing.txt-除非您的ReadFile实现以某种方式操纵了该路径(这取决于您的判断)。 You can test this independent of Liferay, just in a command line application. 您可以在命令行应用程序中独立于Liferay进行测试。 The exception gets thrown way before your jsp gets displayed (I've changed the tags to flag the relevance) 在显示jsp之前会抛出异常(我已经更改了标签以标记相关性)

This is pure Java, completely independent of Liferay. 这是纯Java,完全独立于Liferay。

The problem is with your relative path of resources file ( testing.txt ), that needs to be absolute. 问题出在资源文件的相对路径( testing.txt )上,它必须是绝对路径。 And to create absolute path, first you would require, portletContext which you can get from request , as following: 为了创建绝对路径,首先需要提供portletContext ,您可以从request那里获取它,如下所示:

MVC Portlet: MVC Portlet:

PortletContext portletContext = request.getPortletSession().getPortletContext();

Where request is either renderRequest / actionRequest object. requestrenderRequest / actionRequest对象。 While in JSF, you can get request from externalContext , as following: 在JSF中,您可以从externalContext获取request ,如下所示:

JSF Portlet: JSF Portlet:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
PortletRequest request = (PortletRequest) externalContext.getRequest();
PortletContext portletContext = request.getPortletSession().getPortletContext();

Also, as suggested by Olaf Kock , if you use \\ , you would require to escape it, otherwise use / . 另外,按照Olaf Kock的建议,如果使用\\ ,则需要对其进行转义,否则请使用/ I prefer to use File.separator , as it returns slash as per OS (I think!). 我更喜欢使用File.separator ,因为它根据操作系统返回斜线(我认为!)。

String relativeFilePath = File.separator + "resources" + File.separator + "testing.txt";

Now, you can get absolute path using portletContext as, 现在,您可以使用portletContext作为获取绝对路径,

String absoluteFilePath = portletContext.getRealPath(relativeFilePath);

And then, rest of your code goes, as: 然后,其余代码如下:

ReadFile readFile = new ReadFile();
String text = readFile.readFile(absoluteFilePath);
renderRequest.setAttribute("text", text);

Since you're getting a FileNotFoundException, Are you sure you're pointing to the proper file route? 由于您收到FileNotFoundException,因此确定要指向正确的文件路径吗? Have you tried with the absolute path? 您是否尝试过绝对路径? What SO are you using? 您正在使用什么? Because maybe the file separators ("\\") are the cause of your problem. 因为也许文件分隔符(“ \\”)是造成问题的原因。

HIH. HIH。

Regards. 问候。

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

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