Java InputStreamReader Read readFileOrResource(String source)

Here you can find the source of readFileOrResource(String source)

Description

read File Or Resource

License

Apache License

Declaration

private static Reader readFileOrResource(String source) 

Method Source Code


//package com.java2s;
// Licensed to the Software Freedom Conservancy (SFC) under one

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;

public class Main {
    private static Reader readFileOrResource(String source) {
        Stream<Function<String, InputStream>> suppliers = Stream.of((path) -> {
            try {
                return new FileInputStream(path);
            } catch (FileNotFoundException e) {
                return null;
            }/*from  ww w .j  a  v  a2 s .  c o m*/
        }, (path) -> Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("org/openqa/grid/common/" + path),
                (path) -> Thread.currentThread().getContextClassLoader().getResourceAsStream(path),
                (path) -> new ByteArrayInputStream(path.getBytes()));

        InputStream in = suppliers.map(supplier -> supplier.apply(source)).filter(Objects::nonNull).findFirst()
                .orElseThrow(() -> new RuntimeException("Resource or file not found: " + source));

        return new BufferedReader(new InputStreamReader(in));
    }
}

Related

  1. readFileIntoLines(File file)
  2. readFileIntoLinesOfLongs(File file)
  3. readFileLineByLine(final File filePath)
  4. readFileList(File file)
  5. readFileOneLine(String filename, String keyInLine)
  6. readFiles(String[] files, String encoding)
  7. readFileSB(File f)
  8. readFileToLines(File file)
  9. readFileToList(String fileName, String codeing)