Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I read text file in liferay?

Tags:

java

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

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

But I am getting file not found exception. What is the way to get file content.

like image 953
Ravi Gupta Avatar asked Oct 28 '25 10:10

Ravi Gupta


1 Answers

In a Java string, \ starts an escape sequence and "\r" and "\t" are escaped characters. For example "\t" is a string with the tab character. If you literally need \t in a string, you'll have to escape the backslash

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

or just eliminate the hassle: Java operates well when you use the forward slash as directory separator

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). You can test this independent of Liferay, just in a command line application. The exception gets thrown way before your jsp gets displayed (I've changed the tags to flag the relevance)

This is pure Java, completely independent of Liferay.

like image 129
Olaf Kock Avatar answered Oct 30 '25 00:10

Olaf Kock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!