Java InputStream Create getInputStream(File TheFile)

Here you can find the source of getInputStream(File TheFile)

Description

get Input Stream

License

Apache License

Declaration

public static InputStream getInputStream(File TheFile) 

Method Source Code

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

import java.io.*;

public class Main {
    /**/*www .j  a v a  2 s . com*/
     * @param name non-null name of an existing readable file
     * @return non-null IpuptStream
     * @throws IllegalArgumentException on error
     */
    public static InputStream getInputStream(String name) {
        File TheFile = new File(name);
        return getInputStream(TheFile);
    }

    public static InputStream getInputStream(File TheFile) {
        validateReadableFile(TheFile);
        try {
            InputStream in = new FileInputStream(TheFile);
            return (in);
        } catch (FileNotFoundException ex) {
            String Fullpath = TheFile.getAbsolutePath();
            throw new IllegalStateException(
                    "Requested File '" + Fullpath + "' does not exist -  but has been tested - HUH???");
        }
    }

    public static void validateReadableFile(File TheFile) {
        if (!TheFile.exists()) {
            String Fullpath = TheFile.getAbsolutePath();
            throw new IllegalArgumentException("Requested File '" + Fullpath + "' does not exist");
        }
        if (!TheFile.canRead()) {
            String Fullpath = TheFile.getAbsolutePath();
            throw new IllegalArgumentException("Requested File '" + Fullpath + "' cannot be read");
        }
        if (TheFile.isDirectory()) {
            String Fullpath = TheFile.getAbsolutePath();
            throw new IllegalArgumentException("Requested File '" + Fullpath + "'is a directory");
        }
    }
}

Related

  1. asInputStream(String str)
  2. getInputStream(File f)
  3. getInputStream(File jarFile, String fileName)
  4. getInputStream(File tarFile)
  5. getInputStream(File tarFile)
  6. getInputStream(FileInputStream fileInput)
  7. getInputStream(final File file)
  8. getInputStream(final File file)
  9. getInputStream(final File file, final boolean createFile)