Java InputStreamReader Read readFileFromClassPath(String file)

Here you can find the source of readFileFromClassPath(String file)

Description

Read file from class path

License

Apache License

Parameter

Parameter Description
file a parameter

Declaration

public static String readFileFromClassPath(String file) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**// w  w w  . j  a  va  2s  .c  o  m
     * Read file from class path
     *
     * @param file
     * @return
     */
    public static String readFileFromClassPath(String file) {
        InputStream is = ClassLoader.getSystemResourceAsStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String content = "";
        String line;
        try {
            while ((line = br.readLine()) != null)
                content = content + line + "\n";
        } catch (IOException e) {
            throw new RuntimeException("Unable to read file from templates");
        } finally {
            try {
                is.close();
                br.close();
            } catch (IOException e) {
                // Ignore
            }
        }
        return content;
    }
}

Related

  1. readFileAsList(String path)
  2. readFileByCharAsString(String path, String encode)
  3. readFileCharacters(File file, boolean fix)
  4. readFileData(File file)
  5. readFileData(String directoryLocation, String fileName)
  6. readFileFromDisk(String filepath)
  7. readFileFromResource(@SuppressWarnings("rawtypes") Class refClass, String filePath)
  8. readFileFromResource(String filename)
  9. readFileIntoLines(File file)