Here you can find the source of readToString(final Class nearClass, final String resource, final String encoding)
Parameter | Description |
---|---|
nearClass | class near which file is located |
resource | file location |
encoding | file encoding |
public static String readToString(final Class nearClass, final String resource, final String encoding)
//package com.java2s; /*/*from ww w .j a va 2 s. co m*/ * This file is part of WebLookAndFeel library. * * WebLookAndFeel library 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. * * WebLookAndFeel library 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 WebLookAndFeel library. If not, see <http://www.gnu.org/licenses/>. */ import java.io.*; import java.net.URL; import java.util.*; public class Main { /** * Default encoding used to read files. */ private static final String defaultEncoding = "UTF-8"; /** * Buffer size for text reader. */ private static final int TEXT_BUFFER_SIZE = 65536; /** * Cache for "isFile" method result. */ private static final Map<String, Boolean> isFileCache = new HashMap<String, Boolean>(); /** * Returns text content read from the file located near specified class. * * @param nearClass class near which text file is located * @param resource text file location * @return text file content */ public static String readToString(final Class nearClass, final String resource) { return readToString(nearClass, resource, defaultEncoding); } /** * Returns content read from the file located near specified class. * * @param nearClass class near which file is located * @param resource file location * @param encoding file encoding * @return file content */ public static String readToString(final Class nearClass, final String resource, final String encoding) { try { return readToString(nearClass.getResourceAsStream(resource), encoding); } catch (final Throwable e) { return null; } } /** * Returns content read from the file at the specified url. * * @param url file url * @return file content */ public static String readToString(final URL url) { return readToString(url, defaultEncoding); } /** * Returns content read from the file at the specified url. * * @param url file url * @param encoding file encoding * @return file content */ public static String readToString(final URL url, final String encoding) { try { return readToString(url.openStream(), encoding); } catch (final Throwable e) { return null; } } /** * Returns content read from the specified file. * * @param file file to read * @return file content */ public static String readToString(final File file) { return readToString(file, defaultEncoding); } /** * Returns content read from the specified file. * * @param file file to read * @param encoding file encoding * @return file content */ public static String readToString(final File file, final String encoding) { try { if (file != null && file.exists() && file.isFile()) { return readToString(new FileInputStream(file), encoding); } else { return null; } } catch (final Throwable e) { return null; } } /** * Returns content read from the input stream. * * @param inputStream text content input stream * @return text content */ public static String readToString(final InputStream inputStream) { return readToString(inputStream, defaultEncoding); } /** * Returns content read from the input stream. * * @param inputStream text content input stream * @param encoding stream data encoding * @return content */ public static String readToString(final InputStream inputStream, final String encoding) { try { if (inputStream != null) { return readToString(new InputStreamReader(inputStream, encoding)); } else { return null; } } catch (final Throwable e) { return null; } finally { try { inputStream.close(); } catch (final IOException e) { // Ignore this exception } } } /** * Returns content read from the specified reader. * * @param reader text content reader * @return content */ public static String readToString(final Reader reader) { try { if (reader == null) { return ""; } int charsRead; final char[] buffer = new char[TEXT_BUFFER_SIZE]; final StringBuilder sb = new StringBuilder(); while ((charsRead = reader.read(buffer, 0, TEXT_BUFFER_SIZE)) != -1) { sb.append(buffer, 0, charsRead); } return sb.toString(); } catch (final Throwable e) { return ""; } finally { try { reader.close(); } catch (final IOException e) { // Ignore this exception } } } /** * Returns whether the specified file is actually a file (and not a directory, disk or some system folder) or not. * * @param file file to process * @return true if the specified file is actually a file, false otherwise */ public static boolean isFile(final File file) { if (file == null) { return false; } else if (isFileCache.containsKey(file.getAbsolutePath())) { return isFileCache.get(file.getAbsolutePath()); } else { final boolean isFile = file.isFile(); isFileCache.put(file.getAbsolutePath(), isFile); return isFile; } } }