Java Path File Read nio readTextFileFromResources(String filepath, ClassLoader classLoader)

Here you can find the source of readTextFileFromResources(String filepath, ClassLoader classLoader)

Description

read Text File From Resources

License

Apache License

Declaration

public static String readTextFileFromResources(String filepath, ClassLoader classLoader) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Main {
    public static String readTextFileFromResources(String filepath, ClassLoader classLoader) throws IOException {
        InputStream is = null;/*  www .  j a va 2  s . c o m*/
        BufferedReader reader = null;
        try {
            is = classLoader.getResourceAsStream(filepath);
            if (is == null) {
                throw new FileNotFoundException("file not found:" + filepath);
            }

            reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }

            is.close();
            return sb.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (is != null) {
                is.close();
            }
        }
    }
}

Related

  1. readManifest(Path path)
  2. readPropertiesFile(Path path)
  3. readSheBang(Path script)
  4. readStringFromPath(Path path)
  5. readTextFile(Path p)
  6. readTextFromFile(String textFilePath)
  7. setReadable(final Path path, boolean readable)
  8. setReadOnly(Path file)