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

public static String getFileMacEncrypt(File file, SecretKey key) throws IOException {
    String algorithm = key.getAlgorithm();
    Mac mac = null;/*from  w w  w  .  j av a 2  s . c om*/
    try {
        mac = Mac.getInstance(algorithm);
        mac.init(key);
        FileInputStream in = new FileInputStream(file);
        byte[] buffer = new byte[1024 * 1024];
        int len = 0;
        while ((len = in.read(buffer)) > 0) {
            mac.update(buffer, 0, len);
        }
        in.close();
        return bytes2String(mac.doFinal());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditingTestsUtils.java

static String readFile(File file) {
    try {//w w  w  .  ja  v  a 2s  . c  o  m
        FileInputStream input = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        input.read(data);
        input.close();
        return new String(data);
    } catch (Exception exception) {
        throw new AuditingException(exception, "Error reading file '%s' content.", file.getAbsolutePath());
    }
}

From source file:Main.java

private static byte[] getFileBytes(String filePath) {
    byte[] buffer = null;
    try {/*w ww. j a  v  a 2  s .c  o m*/
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
    return buffer;
}

From source file:Compress.java

/** Zip the contents of the directory, and save it in the zipfile */
public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
        throw new IllegalArgumentException("Not a directory:  " + dir);
    String[] entries = d.list();/*from ww w. j ava 2s  . c om*/
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

    for (int i = 0; i < entries.length; i++) {
        File f = new File(d, entries[i]);
        if (f.isDirectory())
            continue;//Ignore directory
        FileInputStream in = new FileInputStream(f); // Stream to read file
        ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
        out.putNextEntry(entry); // Store entry
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
        in.close();
    }
    out.close();
}

From source file:Main.java

public static List<Object> objectXmlDecoder(String source) throws IOException {
    List<Object> objList = new ArrayList<Object>();
    File fin = new File(source);
    FileInputStream fis = new FileInputStream(fin);
    XMLDecoder decoder = new XMLDecoder(fis);
    Object obj = null;/*from   w ww . java2 s.c  om*/
    try {
        while ((obj = decoder.readObject()) != null) {
            objList.add(obj);
        }
    } catch (Exception e) {
    }
    fis.close();
    decoder.close();
    return objList;
}

From source file:Main.java

public static void compressFile(File file, File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    FileInputStream fileInputStream = new FileInputStream(file);
    zipOutputStream.putNextEntry(new ZipEntry(file.getPath()));

    int size;//from  w  w w. j ava2  s  .  c  o m
    while ((size = fileInputStream.read(buffer)) > 0)
        zipOutputStream.write(buffer, 0, size);

    zipOutputStream.closeEntry();
    fileInputStream.close();
    zipOutputStream.close();
}

From source file:Main.java

public static boolean copy(File source, File target) {
    if (source == null || target == null || !source.exists() || source.length() < 100) {
        return false;
    }/*  www . j av a 2  s .  co m*/
    try {
        FileOutputStream fos = new FileOutputStream(target);
        FileInputStream fis = new FileInputStream(source);
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        fos.flush();
        fos.close();
        fis.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void copyFile(File fromFile, File toFile) throws IOException {
    FileInputStream inputStream = new FileInputStream(fromFile);
    FileOutputStream outputStream = new FileOutputStream(toFile);
    byte[] buffer = new byte[4096];
    for (int numRead; (numRead = inputStream.read(buffer)) != -1;)
        outputStream.write(buffer, 0, numRead);
    inputStream.close();
    outputStream.close();/*from w ww.  ja  va 2 s.  c o m*/
}

From source file:Main.java

public static byte[] getBytes(String filePath) {
    byte[] buffer = null;
    try {/*from w w w. j av a 2s .c o  m*/
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:ZipHandler.java

/**
 * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL>
 * @param zipURL/*from w  ww.  jav a2s . c  om*/
 * @param xmlURL
 * @param xmlFileName
 */

public static void makeStructZip(String zipURL, String xmlURL, String xmlFileName) throws IOException {
    FileOutputStream fos = new FileOutputStream(new File(zipURL));
    ZipOutputStream zos = new ZipOutputStream(fos);
    //         zos.setLevel();
    FileInputStream fis = new FileInputStream(xmlURL);
    zos.putNextEntry(new ZipEntry(xmlFileName + ".xml"));
    writeInOutputStream(fis, zos);
    zos.closeEntry();
    fis.close();
    zos.close();
}