Re: Including static HTML in a WOComponent [A few useful File IO methods]
Re: Including static HTML in a WOComponent [A few useful File IO methods]
- Subject: Re: Including static HTML in a WOComponent [A few useful File IO methods]
- From: Mark Morris <email@hidden>
- Date: Sat, 7 May 2005 19:34:23 -0500
The caveat I would add to this is, unlike db access, file accesses are
not cached automatically, and could therefore impact the performance of
your app. If for some reason you want to read in the html from a file,
I would add another layer to cache the data that's already been read
in. (A dictionary at the Application level would work in the simplest
case. I wrote and use classes I named AppResourceCache and
SessionResourceCache to handle times I want to read from files, such as
CSS and JavaScript.)
-- Mark
On May 7, 2005, at 12:43 PM, Brendan Duddridge wrote:
Hi Nathan,
Here are a few useful methods for reading text files from disk into a
String. They assume the files are in UTF-8 encoding, but you can
change that
to whatever you need or pass it in as a variable.
public static String stringFileContent(String fileName) throws
IOException {
return stringFileContent(new File(fileName));
}
public static String stringFileContent(File file) throws IOException {
if (file == null) {
throw new IOException("null file");
}
BufferedReader br = new BufferedReader(new InputStreamReader(new
FileInputStream(file), "UTF-8"));
String line = br.readLine();
StringBuffer sb = new StringBuffer((line == null) ? "" : line);
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
public static String stringFromInputStream(InputStream is) throws
IOException {
if (is == null) {
throw new IOException("null input stream");
}
String nl = System.getProperty("line.separator");
BufferedReader in = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
StringBuffer content = new StringBuffer();
String line = in.readLine();
while (line != null) {
content.append(line).append(nl);
line = in.readLine();
}
in.close();
String contentString = content.toString();
if (contentString.lastIndexOf(nl) != -1) {
return contentString.substring(0,
contentString.lastIndexOf(nl));
}
return contentString;
}
_______________________________________________________________________
__
Brendan Duddridge | CTO | 403-277-5591 x24 |
email@hidden
ClickSpace Interactive Inc.
Suite L100, 239 - 10th Ave. SE
Calgary, AB T2G 0V9
http://www.clickspace.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden