Example usage for java.io FileInputStream read

List of usage examples for java.io FileInputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:ngse.org.Tools.java

public static void copyFile(String frm, String to) throws Exception {

    FileInputStream in = new FileInputStream(frm);
    FileOutputStream out = new FileOutputStream(to);
    byte[] buf = new byte[1024 * 8];
    while (true) {
        int len = in.read(buf);
        if (len <= 0) {
            break;
        }//  ww w.  j  ava  2s.  co  m
        out.write(buf, 0, len);
    }
    in.close();
    out.close();

}

From source file:dynamicrefactoring.util.io.FileManager.java

/**
 * Crea una copia de un fichero.//from   w ww.ja  v  a2  s.c  o m
 * 
 * @param in
 *            el fichero original.
 * @param out
 *            el fichero que se deber crear como copia.
 * 
 * @throws IOException
 *             si no se encuentra alguno de los dos ficheros.
 */
public static void copyFile(File in, File out) throws IOException {

    if (!out.exists()) {
        out.createNewFile();
    }

    FileInputStream input = new FileInputStream(in);
    FileOutputStream output = new FileOutputStream(out);

    try {
        byte[] buf = new byte[1024];
        int i = 0;
        while ((i = input.read(buf)) != -1)
            output.write(buf, 0, i);
    } finally {
        if (input != null)
            input.close();
        if (output != null)
            output.close();
    }
}

From source file:SmartEncodingInputStream.java

public static Charset guessEncoding(final File f, final int bufferLength)
        throws FileNotFoundException, IOException {
    final FileInputStream fis = new FileInputStream(f);
    final byte[] buffer = new byte[bufferLength];
    fis.read(buffer);
    fis.close();/*from  ww w.  ja  v  a  2  s  .c om*/
    final CharsetToolkit toolkit = new CharsetToolkit(buffer);
    toolkit.setDefaultCharset(getDefaultSystemCharset());
    return toolkit.guessEncoding();
}

From source file:SmartEncodingInputStream.java

public static Charset guessEncoding(final File f, final int bufferLength, final Charset defaultCharset)
        throws FileNotFoundException, IOException {
    final FileInputStream fis = new FileInputStream(f);
    final byte[] buffer = new byte[bufferLength];
    fis.read(buffer);
    fis.close();/*from w ww. ja  v a  2s  . c  o  m*/
    final CharsetToolkit toolkit = new CharsetToolkit(buffer);
    toolkit.setDefaultCharset(defaultCharset);
    return toolkit.guessEncoding();
}

From source file:org.fcrepo.test.api.TestHTTPStatusCodes.java

private static void copyFile(File sourceFile, File destFile) throws Exception {
    FileInputStream in = new FileInputStream(sourceFile);
    FileOutputStream out = new FileOutputStream(destFile);
    byte[] buf = new byte[4096];
    int len;/*from  ww  w.jav a2  s . co m*/
    try {
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    } finally {
        in.close();
        out.close();
    }
}

From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java

private static final void zip(final File directory, final File base, final ZipOutputStream zipOutputStream)
        throws IOException {
    File[] files = directory.listFiles();
    byte[] buffer = new byte[ZIP_BUFFER_SIZE];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory()) {
            zip(files[i], base, zipOutputStream);
        } else {/*from   w w w.  j  ava 2  s . c  om*/
            FileInputStream fileInputStream = new FileInputStream(files[i]);
            ZipEntry entry = new ZipEntry(getRelativePathOfZipEntry(files[i], base));
            zipOutputStream.putNextEntry(entry);
            while (-1 != (read = fileInputStream.read(buffer))) {
                zipOutputStream.write(buffer, 0, read);
            }
            fileInputStream.close();
        }
    }
}

From source file:fr.moribus.imageonmap.migration.V3Migrator.java

/**
 * Calculates the checksum of a given file
 * @param file The file to calculate the checksum of
 * @param algorithmName The name of the algorithm to use
 * @return The resulting checksum in hexadecimal format
 * @throws IOException //w  ww  . j a va  2 s .co m
 */
static private String fileCheckSum(File file, String algorithmName) throws IOException {
    MessageDigest instance;
    try {
        instance = MessageDigest.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException ex) {
        throw new IOException(
                "Could not check file integrity because of NoSuchAlgorithmException : " + ex.getMessage());
    }

    FileInputStream inputStream = new FileInputStream(file);

    byte[] data = new byte[1024];
    int read = 0;

    while ((read = inputStream.read(data)) != -1) {
        instance.update(data);
    }

    byte[] hashBytes = instance.digest();

    StringBuilder buffer = new StringBuilder();
    char hexChar;
    for (int i = 0; i < hashBytes.length; i++) {
        hexChar = Integer.toHexString((hashBytes[i] & 0xff) + 0x100).charAt(0);
        buffer.append(hexChar);
    }

    return buffer.toString();
}

From source file:net.semanticmetadata.lire.utils.FileUtils.java

public static void zipDirectory(File directory, File base, ZipOutputStream zos) throws IOException {
    File[] files = directory.listFiles();
    byte[] buffer = new byte[8192];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory()) {
            zipDirectory(files[i], base, zos);
        } else {//from  w  ww . ja v  a 2s  .c  o  m
            FileInputStream in = new FileInputStream(files[i]);
            ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
            zos.putNextEntry(entry);
            while (-1 != (read = in.read(buffer))) {
                zos.write(buffer, 0, read);
            }
            in.close();
        }
    }
}

From source file:apim.restful.exportimport.utils.ArchiveGenerator.java

public static void addToZip(File directoryToZip, File file, ZipOutputStream zos)
        throws FileNotFoundException, IOException {

    FileInputStream fis = new FileInputStream(file);

    // we want the zipEntry's path to be a relative path that is relative
    // to the directory being zipped, so chop off the rest of the path
    String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
            file.getCanonicalPath().length());
    System.out.println("Writing '" + zipFilePath + "' to zip file");
    ZipEntry zipEntry = new ZipEntry(zipFilePath);
    zos.putNextEntry(zipEntry);// w  ww.j  a va 2s.  c  o  m

    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

    zos.closeEntry();
    fis.close();
}

From source file:gov.nih.nci.caintegrator.common.Cai2Util.java

private static void addFile(File curFile, ZipOutputStream out, int index) throws IOException {
    byte[] tmpBuf = new byte[BUFFER_SIZE];
    FileInputStream in = new FileInputStream(curFile);
    String relativePathName = curFile.getPath().substring(index);
    out.putNextEntry(new ZipEntry(relativePathName));
    int len;//from  w ww . ja v  a  2s.  c  om
    while ((len = in.read(tmpBuf)) > 0) {
        out.write(tmpBuf, 0, len);
    }
    // Complete the entry
    out.closeEntry();
    in.close();
}