Java FileInputStream Read readFileAsResource(String fileName)

Here you can find the source of readFileAsResource(String fileName)

Description

read File As Resource

License

Apache License

Declaration

public static InputStream readFileAsResource(String fileName) throws IOException 

Method Source Code


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

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static InputStream readFileAsResource(String fileName) throws IOException {
        InputStream f;/* w  w  w.  ja  v  a 2s  .com*/
        try {
            FileInputStream fis = new FileInputStream(fileName);
            f = fis;
        } catch (FileNotFoundException ex) {
            // FileNotFound
            f = new Object() {
            }.getClass().getEnclosingClass().getClassLoader().getResourceAsStream(fileName);
        }
        if (f == null) {
            throw new IOException("Cannot load file " + fileName);
        }
        return f;
    }
}

Related

  1. readFile(String sFileName)
  2. readFileAsBinary(File file)
  3. readFileAsInputStream(File file)
  4. readFileAsInputStream(String fileName)
  5. readFileAsProperties(File file)
  6. ReadFileAsUtf8String(String filePath)
  7. readFileAtOnce(final File file)
  8. readFileBinaries(File file)
  9. readFileBinary(String filename)