Solved

showing contents of a file

Posted on 2007-03-29
13
146 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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:Mayank S
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 56
egit plugin on eclipse 8 33
servlet filter example 37 41
MySqlDump not dumping triggers 1 15
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

911 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

22 Experts available now in Live!

Get 1:1 Help Now