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

/**
 * Zip a list of file into one zip file.
 * //from ww  w. java 2s  .c  o  m
 * @param files
 *          files to zip
 * @param targetZipFile
 *          target zip file
 * @throws IOException
 *           IO error exception can be thrown when copying ...
 */
public static void zipFile(final File[] files, final File targetZipFile) throws IOException {
    try {
        FileOutputStream fos = new FileOutputStream(targetZipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        byte[] buffer = new byte[128];
        for (int i = 0; i < files.length; i++) {
            File currentFile = files[i];
            if (!currentFile.isDirectory()) {
                ZipEntry entry = new ZipEntry(currentFile.getName());
                FileInputStream fis = new FileInputStream(currentFile);
                zos.putNextEntry(entry);
                int read = 0;
                while ((read = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, read);
                }
                zos.closeEntry();
                fis.close();
            }
        }
        zos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        System.out.println("File not found : " + e);
    }

}

From source file:ZipHelper.java

private static void fileToZip(File file, ZipOutputStream zout, File baseDir) throws Exception {
    String entryName = file.getPath().substring(baseDir.getPath().length() + 1);
    if (File.separatorChar != '/')
        entryName = entryName.replace(File.separator, "/");
    if (file.isDirectory()) {
        zout.putNextEntry(new ZipEntry(entryName + "/"));
        zout.closeEntry();//from  w  w w .  j ava  2s  . c  o m
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++)
            fileToZip(files[i], zout, baseDir);
    } else {
        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            zout.putNextEntry(new ZipEntry(entryName));
            streamCopy(is, zout);
        } finally {
            zout.closeEntry();
            if (is != null)
                is.close();
        }
    }
}

From source file:Main.java

public static File copy(File in, String path, String name) {
    File newFile = null;//www  .j  ava2 s  .  c o m
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(in);
        newFile = writeFromInput(path, name, fis);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return newFile;
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

private static void streamFile(File f, OutputStream os) {
    try {//from w  w w. j  a  v a 2s . c  om
        FileInputStream fin = new FileInputStream(f);
        IOUtils.copyLarge(fin, os);
        fin.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.linkedin.databus.bootstrap.utils.BootstrapDBCleanerMain.java

@SuppressWarnings("static-access")
public static void parseArgs(String[] args) throws IOException {
    CommandLineParser cliParser = new GnuParser();

    Option helpOption = OptionBuilder.withLongOpt(HELP_OPT_LONG_NAME).withDescription("Help screen")
            .create(HELP_OPT_CHAR);// ww  w  .ja v a  2  s  . c o  m

    Option dbOption = OptionBuilder.withLongOpt(BOOTSTRAP_DB_PROPS_OPT_LONG_NAME)
            .withDescription("Bootstrap Cleaner and DB properties to use").hasArg().withArgName("property_file")
            .create(BOOTSTRAP_DB_PROP_OPT_CHAR);

    Option cmdLinePropsOption1 = OptionBuilder.withLongOpt(CLEANER_CMD_LINE_PROPS_OPT_LONG_NAME)
            .withDescription("Cmd line override of cleaner config properties. Semicolon separated.").hasArg()
            .withArgName("Semicolon_separated_properties").create(CLEANER_CMD_LINE_PROPS_OPT_CHAR);

    Option log4jPropsOption = OptionBuilder.withLongOpt(LOG4J_PROPS_OPT_LONG_NAME)
            .withDescription("Log4j properties to use").hasArg().withArgName("property_file")
            .create(LOG4J_PROPS_OPT_CHAR);

    Option sourcesOption = OptionBuilder.withLongOpt(BOOTSTRAP_SOURCES_OPT_LONG_NAME)
            .withDescription(
                    "Comma seperated list of sourceNames. If not provided, no source will be cleaned up")
            .hasArg().withArgName("comma-seperated sources").create(BOOTSTRAP_SOURCES_PROP_OPT_CHAR);

    Options options = new Options();
    options.addOption(helpOption);
    options.addOption(dbOption);
    options.addOption(cmdLinePropsOption1);
    options.addOption(log4jPropsOption);
    options.addOption(sourcesOption);

    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(options, args);
    } catch (ParseException pe) {
        LOG.error("Bootstrap Physical Config: failed to parse command-line options.", pe);
        throw new RuntimeException("Bootstrap Physical Config: failed to parse command-line options.", pe);
    }

    if (cmd.hasOption(LOG4J_PROPS_OPT_CHAR)) {
        String log4jPropFile = cmd.getOptionValue(LOG4J_PROPS_OPT_CHAR);
        PropertyConfigurator.configure(log4jPropFile);
        LOG.info("Using custom logging settings from file " + log4jPropFile);
    } else {
        PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} +%r [%t] (%p) {%c} %m%n");
        ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);

        Logger.getRootLogger().removeAllAppenders();
        Logger.getRootLogger().addAppender(defaultAppender);

        LOG.info("Using default logging settings");
    }

    if (cmd.hasOption(HELP_OPT_CHAR)) {
        printCliHelp(options);
        System.exit(0);
    }

    if (cmd.hasOption(BOOTSTRAP_SOURCES_PROP_OPT_CHAR)) {
        _sSources = new ArrayList<String>();
        String srcListStr = cmd.getOptionValue(BOOTSTRAP_SOURCES_PROP_OPT_CHAR);
        LOG.info("Going to run cleaner for only these sources : " + srcListStr);
        String[] srcList = srcListStr.split(",");
        for (String s : srcList)
            _sSources.add(s);
    }

    if (!cmd.hasOption(BOOTSTRAP_DB_PROP_OPT_CHAR))
        throw new RuntimeException("Bootstrap config is not provided");

    String propFile = cmd.getOptionValue(BOOTSTRAP_DB_PROP_OPT_CHAR);
    LOG.info("Loading bootstrap DB config from properties file " + propFile);

    _sBootstrapConfigProps = new Properties();
    FileInputStream f = new FileInputStream(propFile);
    try {
        _sBootstrapConfigProps.load(f);
    } finally {
        f.close();
    }
    if (cmd.hasOption(CLEANER_CMD_LINE_PROPS_OPT_CHAR)) {
        String cmdLinePropString = cmd.getOptionValue(CLEANER_CMD_LINE_PROPS_OPT_CHAR);
        updatePropsFromCmdLine(_sBootstrapConfigProps, cmdLinePropString);
    }
}

From source file:msec.org.GzipUtil.java

static public void zip(String srcFile) throws Exception {
    GzipCompressorOutputStream out = new GzipCompressorOutputStream(new FileOutputStream(srcFile + ".gz"));
    FileInputStream in = new FileInputStream(srcFile);
    byte[] buf = new byte[10240];
    while (true) {
        int len = in.read(buf);
        if (len <= 0) {
            break;
        }/*  w  w  w. j  a  v  a 2  s. c  o  m*/
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:msec.org.GzipUtil.java

static public void zip(String srcFile, String destFile) throws Exception {
    GzipCompressorOutputStream out = new GzipCompressorOutputStream(new FileOutputStream(destFile));
    FileInputStream in = new FileInputStream(srcFile);
    byte[] buf = new byte[10240];
    while (true) {
        int len = in.read(buf);
        if (len <= 0) {
            break;
        }/* w ww . j a v  a 2s . com*/
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:Main.java

public static byte[] readFileToMemory(File file) throws IOException {
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] data = new byte[(int) file.length()];
    int start = 0;
    int read = 0;
    while ((read = bis.read(data, start, data.length - start)) > 0) {
        start += read;//from   ww  w .  j a  va2 s .c o m
    }
    bis.close();
    fis.close();
    return data;
}

From source file:Main.java

public static byte[] readByteArray(File file) {
    if (!file.exists() || !file.isFile()) {
        return null;
    }// w  ww  .  ja  v a 2  s.c om
    byte[] data = null;
    FileInputStream stream = null;
    try {
        stream = new FileInputStream(file);
        data = new byte[(int) file.length()];
        stream.read(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return data;
}

From source file:Main.java

public static Bitmap getBitmapByFile(File file) {
    FileInputStream fis = null;
    Bitmap bitmap = null;/*from w w  w  .  java  2 s  .c  o  m*/
    try {
        fis = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException | OutOfMemoryError e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}