Here you can find the source of readToString(final Class nearClass, final String resource)
Parameter | Description |
---|---|
nearClass | class near which text file is located |
resource | text file location |
public static String readToString(final Class nearClass, final String resource)
//package com.java2s; /*//w w w. ja v a 2s . c o 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.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.util.HashMap; import java.util.Map; public class Main { /** * 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) { try { return readToString(nearClass.getResourceAsStream(resource)); } catch (final Throwable e) { return null; } } /** * Returns text content read from the file at the specified url. * * @param url * text file url * @return text file content */ public static String readToString(final URL url) { try { return readToString(url.openConnection().getInputStream()); } catch (final Throwable e) { return null; } } /** * Returns text content read from the input stream. * * @param inputStream * text content input stream * @return text content */ public static String readToString(final InputStream inputStream) { try { if (inputStream != null) { return readToString(new InputStreamReader(inputStream, "UTF-8")); } else { return null; } } catch (final Throwable e) { return null; } finally { try { inputStream.close(); } catch (final IOException e) { // Ignore this exception } } } /** * Returns text content read from the specified file. * * @param file * file to read * @return text file content */ public static String readToString(final File file) { try { if (file != null && file.exists() && file.isFile()) { return readToString(new FileReader(file)); } else { return null; } } catch (final Throwable e) { return null; } } /** * Returns text content read from the specified reader. * * @param reader * text content reader * @return text 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; } } }