• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Including static HTML in a WOComponent [A few useful File IO methods]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Brendan Duddridge <email@hidden>
  • Date: Sat, 07 May 2005 11:43:47 -0600

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Including static HTML in a WOComponent [A few useful File IO methods]
      • From: Mark Morris <email@hidden>
  • Prev by Date: Re: WO 5.2.4 is here!
  • Next by Date: Re: WO 5.2.4 is here!
  • Previous by thread: Re: searching for a word
  • Next by thread: Re: Including static HTML in a WOComponent [A few useful File IO methods]
  • Index(es):
    • Date
    • Thread