You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2009/02/17 00:25:10 UTC

[Tapestry Wiki] Update of "Tapestry5HowToReadAFileFromContext" by BorutBolcina

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.

The following page has been changed by BorutBolcina:
http://wiki.apache.org/tapestry/Tapestry5HowToReadAFileFromContext

New page:
One of your services needs a content of a file located at your web application's context folder. More specifically, the file we want to read is in the config folder under the context directory.

AppModule.java
{{{
#!java
public static TownNames buildTownNames(Context context, final Logger log) {
    return new TownNamesImpl(context, log);
}
}}}

TownNames.java is a very simple interface:
{{{
#!java
public interface TownNames {
    List<String> getAll();
}
}}}

And the implementation (TownNamesImpl.java) which demonstrates reading a file named towns.txt from the config folder under the web app context folder:
{{{
#!java
public class TownNamesImpl implements TownNames {

    private List<String> townNames = new ArrayList<String>();

    public TownNamesImpl(Context context, Logger log) {
        String town = null;
        Resource configFile = new ContextResource(context, "config/towns.txt");
        try {
            InputStream is = configFile.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf8"));

            while ((town = br.readLine()) != null) {
                townNames.add(town);
                System.out.println(town);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public List<String> getAll() {
        Collections.sort(townNames);
        return Collections.unmodifiableList(townNames);
    }

}
}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org