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

/**
 * Copy file a to b and remove a. /*  ww  w  .  j av a 2 s  .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:Main.java

static void addDir(File dirObj, ZipOutputStream out) throws IOException {
    File[] files = dirObj.listFiles();
    byte[] tmpBuf = new byte[1024];

    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            addDir(files[i], out);//w  w w. j a  v a  2s  .c o  m
            continue;
        }
        FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
        System.out.println(" Adding: " + files[i].getAbsolutePath());
        out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
        int len;
        while ((len = in.read(tmpBuf)) > 0) {
            out.write(tmpBuf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
}

From source file:Main.java

/**
 * Copy file from oldPath to newPath//from w w w .  j  av  a2s .c om
 *
 * @param oldPath
 * @param newPath
 */
public static void copyFile(String oldPath, String newPath) {
    try {
        int byteread = 0;
        File oldfile = new File(oldPath);
        File newfile = new File(newPath);
        if (newfile.exists()) {
            newfile.delete();
        }
        newfile.createNewFile();
        if (oldfile.exists()) {
            FileInputStream inStream = new FileInputStream(oldPath);
            FileOutputStream outStream = new FileOutputStream(newPath);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, byteread);
            }
            inStream.close();
            outStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String[] GetSupportedFileSystems() {
    try {/*  w  ww . j ava  2  s  . c o m*/
        FileInputStream fProcFS = new FileInputStream(new File("/proc/filesystems"));
        ArrayList<String> filesystems = new ArrayList<String>();
        byte[] data1 = new byte[1024];
        int len = fProcFS.read(data1);
        fProcFS.close();
        String fs = new String(data1, 0, len);
        if (fs.contains("rfs"))
            filesystems.add("rfs");
        if (fs.contains("jfs"))
            filesystems.add("jfs");
        if (fs.contains("ext2"))
            filesystems.add("ext2");
        if (fs.contains("ext3"))
            filesystems.add("ext3");
        if (fs.contains("ext4"))
            filesystems.add("ext4");
        String[] List = (String[]) filesystems.toArray(new String[filesystems.size()]);
        return List;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:core.SHA256Hash.java

public static String CalculateCheckSumBase64(String FileName, String url) throws Exception {
    MessageDigest md = MessageDigest.getInstance(SHA256);
    FileInputStream fileHandle = new FileInputStream(FileName);

    byte[] dataBytes = new byte[1024];

    int nread = 0;
    while ((nread = fileHandle.read(dataBytes)) != -1) {
        md.update(dataBytes, 0, nread);/*from w w w.  ja  v a  2  s .  co m*/
    }
    ;
    byte[] mdbytes = md.digest();

    byte[] bytes = null;
    if (SHA256 == getHashAlgType(url)) {
        // Do base64 encoding
        bytes = Base64.encodeBase64URLSafe(mdbytes);
    } else {
        if (SHA256_16 == getHashAlgType(url))
            bytes = Base64.encodeBase64(new byte[] { mdbytes[0], mdbytes[1] }, false, true, 4);
    }

    // Store byte array as string and return it
    String value = new String(bytes);
    System.out.println("Base64 encoded " + getHashAlgType(url) + " hash: " + value);

    return value;
}

From source file:Main.java

public static String readFile(String path) {
    try {/*from  w  w w.j  a  va  2  s  .com*/
        FileInputStream in = new FileInputStream(path);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte buffer[] = new byte[1024];
        int count = 0;

        while ((count = in.read(buffer)) != -1) {
            if (out != null) {
                out.write(buffer, 0, count);
            }
        }

        return out.toString();

    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found: " + path, e);

    } catch (IOException e) {
        Log.e(TAG, "Error while reading file: " + path, e);
    }

    return null;
}

From source file:Main.java

private static final void zip(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()) {
            zip(files[i], base, zos);//  w  ww .  j  a va2s.co m
        } else {
            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:Main.java

public static String getFileMd5String(File file) {
    if (!file.isFile())
        throw new IllegalArgumentException("only file can calculate MD5 value!");

    MessageDigest digest;//  ww  w.  j av a2 s .c  om
    FileInputStream in = null;
    byte buffer[] = new byte[8192];
    int len;
    try {
        digest = MessageDigest.getInstance("MD5");
        in = new FileInputStream(file);
        while ((len = in.read(buffer)) != -1) {
            digest.update(buffer, 0, len);
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void copyfile(String fromFilePath, String toFilePath, Boolean rewrite) {
    File fromFile = new File(fromFilePath);
    File toFile = new File(toFilePath);

    if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
        return;//w  w  w .j  a v  a2  s . co  m
    }

    if (!toFile.getParentFile().exists()) {
        toFile.getParentFile().mkdirs();
    }

    if (toFile.exists() && rewrite) {
        toFile.delete();
    }

    try {
        FileInputStream fosfrom = new FileInputStream(fromFile);
        FileOutputStream fosto = new FileOutputStream(toFile);

        byte[] bt = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void copyFile(File targetFile, File file) {
    if (targetFile.exists()) {
        return;//from  w w w. j  a va  2s.  c o m
    } else {
        createFile(targetFile, true);
    }
    try {
        FileInputStream is = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(targetFile);
        byte[] buffer = new byte[1024 * 5];
        int len;
        while ((len = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        is.close();
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}