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:Main.java

static public String getFileContent(String filename) throws IOException {
    File file = new File(filename);
    FileInputStream stream = new FileInputStream(file);
    try {//from  w w  w  .jav a2 s  .c o m
        FileChannel fc = stream.getChannel();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

        return Charset.defaultCharset().decode(mbb).toString();
    } finally {
        stream.close();
    }
}

From source file:uk.org.openeyes.oink.itest.karaf.OinkKarafITSupport.java

public static Properties getPropertiesBySystemProperty(String systemProperty) throws IOException {
    File f = getPropertyFileBySystemProperty(systemProperty);
    if (!f.exists()) {
        throw new FileNotFoundException("No file found at " + f.getAbsolutePath() + " for system property "
                + systemProperty + " is it set correctly?");
    }//from  w ww.  ja  v a2s  . c o m
    FileInputStream fis = new FileInputStream(f);
    Properties p = new Properties();
    p.load(fis);
    fis.close();
    return p;
}

From source file:eu.cloud4soa.c4sgitservice.utils.Util.java

public static String readfile(String filename) throws Exception {
    String ret = "";
    File file = new File(filename);
    int ch;// w w  w  .j a v a  2s.  c om
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
    try {
        fin = new FileInputStream(file);
        while ((ch = fin.read()) != -1)
            strContent.append((char) ch);
        fin.close();
    } catch (Exception e) {
        throw e;
    }
    ret = strContent.toString();
    return ret;
}

From source file:com.sshtools.j2ssh.transport.publickey.SshPublicKeyFile.java

/**
 *
 *
 * @param keyfile/*from  www. j a va2s .c  om*/
 *
 * @return
 *
 * @throws InvalidSshKeyException
 * @throws IOException
 */
public static SshPublicKeyFile parse(File keyfile) throws InvalidSshKeyException, IOException {
    FileInputStream in = new FileInputStream(keyfile);
    byte[] data = new byte[in.available()];
    in.read(data);
    in.close();

    return parse(data);
}

From source file:Main.java

public static String getLastLine(String fname) {
    String lastLine;//from  w w  w  .  j a va  2s .  c  om

    try {
        FileInputStream in = new FileInputStream(fname);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String strLine = null, tmp;

        while ((tmp = br.readLine()) != null) {
            strLine = tmp;
        }

        lastLine = strLine;

        in.close();
    } catch (Exception e) {
        lastLine = "70-01-01 00:00:00";
    }

    return lastLine;
}

From source file:Main.java

/**
 * Copies source file to destination./*from   w w w .  ja v a2s. c o  m*/
 * If destination file exists it will be overwritten silently.
 *
 * @param source file
 * @param target file
 */
public static void copyFile(final File source, final File target) {
    try {
        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(target);
        byte[] buf = new byte[4096];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String setImBin(String fName) {
    FileInputStream fileInputStream = null;

    File file = new File(Environment.getExternalStorageDirectory().getPath(), "Pictures/" + fName);

    byte[] bFile = new byte[(int) file.length()];

    try {/*from  w w  w.  j av a 2 s . c  o  m*/
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(bFile, Base64.DEFAULT);
}

From source file:Main.java

/**
 * Copy file a to b and remove a. //w  w w  .j  a va2s  .c o  m
 * @param source source file
 * @param destination destination file
 * @return true if success, false if fail.
 */
public static boolean move(File source, File destination) {
    try {
        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(destination);
        byte[] buffer = new byte[1024 * 1024];
        int size;
        while ((size = in.read(buffer)) >= 0) {
            out.write(buffer, 0, size);
        }
        in.close();
        out.flush();
        out.close();
        source.delete();
        return true;
    } catch (Exception exp) {
        exp.printStackTrace();
    }
    return false;
}

From source file:ch.entwine.weblounge.common.impl.util.TestUtils.java

/**
 * Loads the <code>XML</code> data from the specified file on the class path
 * and returns it after having stripped off any newlines, line breaks and
 * otherwise disturbing spaces and characters.
 * //w  w  w . ja v  a2s.co  m
 * @param path
 *          the resource path
 * @return the contents of the resource
 */
public static String loadXmlFromResource(String path) {
    File templateFile = new File(TestUtils.class.getResource(path).getPath());
    String template = null;
    try {
        byte[] buffer = new byte[(int) templateFile.length()];
        FileInputStream f = new FileInputStream(templateFile);
        f.read(buffer);
        f.close();
        template = new String(buffer, "utf-8").replaceFirst("<\\?.*?>", "");
        template = template.replaceAll("(>\\s*)+", ">").replaceAll("(\\s*<)+", "<");
    } catch (IOException e) {
        throw new RuntimeException("Error reading test resource at " + path);
    }
    return template;
}

From source file:com.asksven.commandcenter.valueobjects.CommandReaderWriter.java

public static CommandCollection readFile(Context ctx, String strFileName) {
    CommandCollection data = new CommandCollection();

    try {/*from  w ww . j  a v  a 2s  . c om*/
        FileInputStream source = new FileInputStream(
                DataStorage.getExternalStoragePath(ctx) + "/" + strFileName);

        data = readStream(source);

        source.close();

    } catch (Exception e) {
        e.printStackTrace();
        // make sure the returned data is consistent
        data.setEntries(new ArrayList<Command>());
    }

    return data;
}