Solved

showing contents of a file

Posted on 2007-03-29
13
148 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-position the objects 7 109
Java Timer (static) 9 43
Problem to picture file 20 49
Tomcat: Unable to run tomcat service. 2 18
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

786 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