Solved

showing contents of a file

Posted on 2007-03-29
13
144 Views
Last Modified: 2010-03-31
hi all

is it possible to show the contents within a file?

e.g. i have a .vm file, i want to get the .vm files content and show them

is anyone able to lead me to an example

any help much appreciated

0
Comment
Question by:dalecon
13 Comments
 
LVL 4

Expert Comment

by:MikeGeig
ID: 18816315
Hello,

What type of file is a .vm?
0
 

Author Comment

by:dalecon
ID: 18816327
velocity template

just contains html type code
0
 

Author Comment

by:dalecon
ID: 18816425

no worries got it
0
 

Author Comment

by:dalecon
ID: 18817417
oh well, spoke too soon
i'm using this and can read the ocntents of my file

FileInputStream fis = new FileInputStream("a.vm");                  
InputStreamReader isr = new InputStreamReader(fis,"UTF8");                  
Reader in = new BufferedReader(isr);                  
int ch;            
while ((ch = in.read()) > -1)
{
stringbuffer.append((char)ch);
}

however when i display the file contents in my broswer it displays it as an html page, i just want it shown as plain text,

can anyone show me how i can display the contents of the .vm file just as palin text without the broswer formatting it back to an html page?

many thanks
0
 
LVL 3

Expert Comment

by:cavey_79
ID: 18817473
If you're using servlets, on your response object you need to call response.setContentType("text/plain")

For jsps, you would use this:
<%@ page contentType="text/plain"%>

If you have other html stuff in your page and want the page to still be an html, but this text to be displayed as it is, you will need to escape it. There are several ways to do that, let me know if you need it.

If you're just opening the file with a browser, there's not much you can do. Easiest way would probably be to generate an escaped version of the file.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:dalecon
ID: 18817528


do you have an example of escaping the file ?

many thanks for your help
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 18818713
>> when i display the file contents in my broswer it displays it as an html page

The code that you posted - is it reading the file in a servlet and then printing the contents using out.println () ?
0
 
LVL 3

Expert Comment

by:cavey_79
ID: 18818903
If you do it in a jsp, you could just do it with <c:out value="${yourVarHere}" />. If you use a servlet, you just need to do these kind of replaces:
& to &amp;
< to &lt;
> to &gt;

Assuming you have a huge String, in JDK5 you would do print(yourString.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;"));
0
 

Author Comment

by:dalecon
ID: 18822124
i have this in my jsp

<jsp:useBean id="template" class="com.utils.getstuff">
<jsp:setProperty name="template" property="*"/>
<%
contents = template.getContents();                        
%>
</jsp:useBean>

<td><%=contents %></td>
0
 

Author Comment

by:dalecon
ID: 18822203


think i've orted it
0
 
LVL 3

Accepted Solution

by:
cavey_79 earned 50 total points
ID: 18823573
Replace this
<td><%=contents %></td>
with this
<td><%=contents.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;") %></td>

I assume contents is a String. If not, replace it with
<td><%=contents.toString().replace("&","&amp;").replace("<","&lt;").replace(">","&gt;") %></td>
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Specific format 21 144
How To Loop - Python 19 67
Java asynchronous logging 4 35
create a gui in perl 3 46
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now