Here you can find the source of loadTextFile(Class relToThisClass, String relFileName)
Parameter | Description |
---|---|
relToThisClass | the class location is prepend to the relImageName to an absolute path. |
relFileName | the relative text file path. |
public static String loadTextFile(Class relToThisClass, String relFileName) throws IOException
//package com.java2s; /************************************************************************* //from w w w. j a va2s. c o m Copyright (C) 2009 Grandite This file is part of Open ModelSphere. Open ModelSphere is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see http://www.gnu.org/licenses/. You can redistribute and/or modify this particular file even under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. You should have received a copy of the GNU Lesser General Public License (LGPL) along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see http://www.gnu.org/licenses/. You can reach Grandite at: 20-1220 Lebourgneuf Blvd. Quebec, QC Canada G2K 2G4 or open-modelsphere@grandite.com **********************************************************************/ import java.io.*; import java.net.URL; public class Main { /** * load a text from a text file from a class relative path. * * @param relToThisClass * the class location is prepend to the relImageName to an absolute path. * @param relFileName * the relative text file path. * @return the text if found null otherwise. */ public static String loadTextFile(Class relToThisClass, String relFileName) throws IOException { URL url = getURL(relToThisClass, relFileName); String eol = "\n"; // NOT LOCALIZABLE, for GUI display, no need to get // the OS-specific EOL String text; if (url != null) { StringBuffer buffer = new StringBuffer(); InputStream input = url.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line; do { line = reader.readLine(); if (line != null) { buffer.append(line); buffer.append(eol); } } while (line != null); text = buffer.toString(); } else { text = null; } // end if if (text == null) { IOException ex = new FileNotFoundException(relFileName); throw ex; } // end if return text; } /** * get the URL of a file from a class relative path. * * @param relToThisClass * the class location is prepend to the relImageName to an absolute path. * @param relFileName * the relative text file path. * @return the url of this file. */ public static URL getURL(Class relToThisClass, String resourceName) { URL url = relToThisClass.getResource(resourceName); return url; } }