Example usage for java.io FileInputStream close

List of usage examples for java.io FileInputStream close

Introduction

In this page you can find the example usage for java.io FileInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file input stream and releases any system resources associated with the stream.

Usage

From source file:MainWindowLogic.java

static void isTextFile(File fil_hnd) throws IncorrectFileTypeException, FileNotFoundException, IOException {
    FileInputStream in = new FileInputStream(fil_hnd);
    int size = in.available();
    if (size > 1000)
        size = 1000;//  ww  w  .jav a2 s . c o m
    byte[] data = new byte[size];
    in.read(data);
    in.close();
    String s = new String(data, "ISO-8859-1");
    String s2 = s.replaceAll("[a-zA-Z0-9\\.\\*!\"\\$\\%&/()=\\?@~'#:,;\\"
            + "+><\\|\\[\\]\\{\\}\\^\\\\ \\n\\r\\t_\\-`"
            + "??]", "");
    // will delete all text signs

    double d = (double) (s.length() - s2.length()) / (double) (s.length());
    // percentage of text signs in the text
    if (!(d > 0.950))
        throw new IncorrectFileTypeException();

}

From source file:com.btoddb.chronicle.Config.java

public static Config create(String configFilename) throws FileNotFoundException {
    Yaml yaml = new Yaml(new Constructor(Config.class));
    Config config;/*from   ww w .  j  a  v  a2 s  .c  o m*/
    FileInputStream inStream = new FileInputStream(configFilename);
    try {
        config = (Config) yaml.load(inStream);
        config.configFilename = configFilename;
    } finally {
        try {
            inStream.close();
        } catch (IOException e) {
            logger.error("exception while closing config file", e);
        }
    }

    return config;
}

From source file:Main.java

public static byte[] getBytes(File file) throws Exception {
    FileInputStream fis = null;
    ByteArrayOutputStream baos = null;
    try {// w ww.j av  a2s  .co m
        fis = new FileInputStream(file);
        baos = new ByteArrayOutputStream();
        copyStream(fis, baos);
        return baos.toByteArray();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/** Read an XML document from a file */
public static Document readXmlDocumentFromFile(File file) throws Exception {
    if (file.exists()) {
        FileInputStream stream = null;
        try {// w ww .  j av  a2  s  .  co  m
            stream = new FileInputStream(file);
            return readXmlDocumentFromStream(stream);
        } catch (Exception e) {
            throw e;
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (Exception e) {
                }
            }
        }
    } else {
        throw new Exception("FileNotFound: " + file.getPath());
    }
}

From source file:Utils.java

public static byte[] readAsByteArray(File file, long length) {
    try {/*from   ww  w. j  a va  2 s . co  m*/
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        FileInputStream is = new FileInputStream(file);
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        is.close();
        return bytes;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getFileHeader(String filePath) {
    FileInputStream is = null;
    String value = null;/*from w w w. ja  va  2  s  . c om*/
    try {
        is = new FileInputStream(filePath);
        byte[] b = new byte[3];
        is.read(b, 0, b.length);
        value = bytesToHexString(b);
    } catch (Exception e) {
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return value;
}

From source file:Main.java

public static Bitmap getBitmapByPath(String filePath, BitmapFactory.Options opts) {
    FileInputStream fis = null;
    Bitmap bitmap = null;//from   w ww.  ja  v  a2s .  c  o  m
    try {
        File file = new File(filePath);
        fis = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fis, null, opts);
    } catch (FileNotFoundException e) {
    } catch (OutOfMemoryError e) {
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
        }
    }
    return bitmap;
}

From source file:Main.java

public static byte[] readFileToByteArray2(File file) {
    try {//from w w w. ja  va  2s  .com
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        bos.flush();
        byte[] bytes = bos.toByteArray();
        fis.close();
        bos.close();
        return bytes;

    } catch (IOException e) {
        return null;
    }
}

From source file:net.geoprism.SystemLogoSingletonDTO.java

/**
 * Uploads a banner file to the server for persistance. Calling this method will also populate the client-side cache
 * for future calls to getBannerFilePath.
 * /*from  ww w. j  a  v a2  s.co  m*/
 * @param clientRequest
 * @param fileStream
 * @param fileName
 */
public static void uploadBannerAndCache(com.runwaysdk.constants.ClientRequestIF clientRequest,
        java.io.InputStream fileStream, java.lang.String fileName) {
    String tempDir = LocalProperties.getJspDir() + "/../uploaded_images";
    new File(tempDir).mkdir();
    bannerCache = new File(tempDir, fileName);

    try {
        // Write the file locally to our cache
        FileOutputStream fos = new FileOutputStream(bannerCache);
        BufferedOutputStream buffer = new BufferedOutputStream(fos);
        IOUtils.copy(fileStream, buffer);
        buffer.close();
        fos.close();

        // Send the cache file to the server for vault persistance.
        FileInputStream serverInput = new FileInputStream(bannerCache);
        SystemLogoSingletonDTOBase.uploadBanner(clientRequest, serverInput, fileName);
        serverInput.close();
    } catch (IOException e) {
        logger.error("Error creating image file [" + fileName + "].", e);
        return;
    }
}

From source file:ca.hec.commons.utils.MergePropertiesUtils.java

/**
 * Load a Properties File/*  w  w  w.  j av a  2s. c  o m*/
 * 
 * @param propsFile
 * @return Properties
 * @throws IOException
 */
public static Properties load(File propsFile) throws IOException {
    Properties props = new Properties();
    FileInputStream fis = new FileInputStream(propsFile);
    props.load(fis);
    fis.close();
    return props;
}