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

public static boolean saveApk(File apk, String savePath) {
    FileInputStream in = null;
    RandomAccessFile accessFile = null;
    try {//ww w . j a  v  a  2 s.c o m
        in = new FileInputStream(apk);
        byte[] buf = new byte[1024 * 4];
        int len;
        File file = new File(savePath);
        accessFile = new RandomAccessFile(file, "rw");
        FileDescriptor fd = accessFile.getFD();
        while ((len = in.read(buf)) != -1) {
            accessFile.write(buf, 0, len);
        }
        fd.sync();
        accessFile.close();
        in.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        try {
            if (in != null) {
                in.close();
            }
            if (accessFile != null) {
                accessFile.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return false;
    }
}

From source file:de.flapdoodle.embedmongo.Files.java

private static void copyFile(File source, File destination) throws IOException {
    FileInputStream reader = null;
    FileOutputStream writer = null;
    try {//from   ww w .j  a v a 2  s .c  o m
        reader = new FileInputStream(source);
        writer = new FileOutputStream(destination);

        int read;
        byte[] buf = new byte[512];
        while ((read = reader.read(buf)) != -1) {
            writer.write(buf, 0, read);
        }

    } finally {
        if (reader != null)
            reader.close();
        if (writer != null)
            writer.close();
    }
}

From source file:Compress.java

/** Gzip the contents of the from file and save in the to file. */
public static void gzipFile(String from, String to) throws IOException {
    // Create stream to read from the from file
    FileInputStream in = new FileInputStream(from);
    // Create stream to compress data and write it to the to file.
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    // Copy bytes from one stream to the other
    byte[] buffer = new byte[4096];
    int bytes_read;
    while ((bytes_read = in.read(buffer)) != -1)
        out.write(buffer, 0, bytes_read);
    // And close the streams
    in.close();//from www  .ja v a2  s. c om
    out.close();
}

From source file:com.anyi.gp.license.RegisterTools.java

public static String readKeyFromFile(String fileName) {
    String result = "";
    File tempFile = new File(fileName);
    FileInputStream io = null;
    try {/*from w ww  .  ja  v a 2 s  .c  o  m*/
        io = new FileInputStream(tempFile);
        byte[] buf = new byte[512];
        int size = io.read(buf);
        while (size > 0) {
            result += new String(buf, 0, size);
            size = io.read(buf);
        }
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (io != null)
                io.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return decodeString(result);
}

From source file:com.openkm.applet.Util.java

/**
 * Copy file/*w  w  w .  j  a v  a  2 s  . c  om*/
 */
private static void copyFile(File fromFile, File toFile) throws IOException {
    FileInputStream from = null;
    FileOutputStream to = null;

    try {
        from = new FileInputStream(fromFile);
        to = new FileOutputStream(toFile);
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1) {
            to.write(buffer, 0, bytesRead);
        }
    } finally {
        if (from != null) {
            try {
                from.close();
            } catch (IOException e) {
            }
        }

        if (to != null) {
            try {
                to.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static String readFile(String filePath) {
    if (filePath == null || !new File(filePath).exists()) {
        return null;
    }//from   w ww  .j  a  va  2  s. com
    FileInputStream fis = null;
    String content = null;
    try {
        fis = new FileInputStream(filePath);
        if (fis != null) {

            byte[] buffer = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int readLength = fis.read(buffer);
                if (readLength == -1)
                    break;
                arrayOutputStream.write(buffer, 0, readLength);
            }
            fis.close();
            arrayOutputStream.close();
            content = new String(arrayOutputStream.toByteArray());

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        content = null;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return content;
}

From source file:FileUtil.java

public static void copyFile(String from, String to, Boolean overwrite) {

    try {//from  ww w .  ja  v  a 2 s.c om
        File fromFile = new File(from);
        File toFile = new File(to);

        if (!fromFile.exists()) {
            throw new IOException("File not found: " + from);
        }
        if (!fromFile.isFile()) {
            throw new IOException("Can't copy directories: " + from);
        }
        if (!fromFile.canRead()) {
            throw new IOException("Can't read file: " + from);
        }

        if (toFile.isDirectory()) {
            toFile = new File(toFile, fromFile.getName());
        }

        if (toFile.exists() && !overwrite) {
            throw new IOException("File already exists.");
        } else {
            String parent = toFile.getParent();
            if (parent == null) {
                parent = System.getProperty("user.dir");
            }
            File dir = new File(parent);
            if (!dir.exists()) {
                throw new IOException("Destination directory does not exist: " + parent);
            }
            if (dir.isFile()) {
                throw new IOException("Destination is not a valid directory: " + parent);
            }
            if (!dir.canWrite()) {
                throw new IOException("Can't write on destination: " + parent);
            }
        }

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {

            fis = new FileInputStream(fromFile);
            fos = new FileOutputStream(toFile);
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }

        } finally {
            if (from != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    System.out.println(e);
                }
            }
            if (to != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    System.out.println(e);
                }
            }
        }

    } catch (Exception e) {
        System.out.println("Problems when copying file.");
    }
}

From source file:Main.java

public static String readFile(Context context, String fileName) {
    if (!exists(context, fileName)) {
        return null;
    }/*  w  w  w  .  j  a  va2 s.  c  om*/
    FileInputStream fis = null;
    String content = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {

            byte[] buffer = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int readLength = fis.read(buffer);
                if (readLength == -1)
                    break;
                arrayOutputStream.write(buffer, 0, readLength);
            }
            fis.close();
            arrayOutputStream.close();
            content = new String(arrayOutputStream.toByteArray());

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        content = null;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return content;
}

From source file:S3DataManager.java

public static void zipSource(final String directory, final ZipOutputStream out, final String prefixToTrim)
        throws Exception {
    if (!Paths.get(directory).startsWith(Paths.get(prefixToTrim))) {
        throw new Exception(zipSourceError + "prefixToTrim: " + prefixToTrim + ", directory: " + directory);
    }//from   ww  w.ja  v  a 2  s .  c  om

    File dir = new File(directory);
    String[] dirFiles = dir.list();
    if (dirFiles == null) {
        throw new Exception("Invalid directory path provided: " + directory);
    }
    byte[] buffer = new byte[1024];
    int bytesRead;

    for (int i = 0; i < dirFiles.length; i++) {
        File f = new File(dir, dirFiles[i]);
        if (f.isDirectory()) {
            if (f.getName().equals(".git") == false) {
                zipSource(f.getPath() + File.separator, out, prefixToTrim);
            }
        } else {
            FileInputStream inputStream = new FileInputStream(f);
            try {
                String path = trimPrefix(f.getPath(), prefixToTrim);

                ZipEntry entry = new ZipEntry(path);
                out.putNextEntry(entry);
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                inputStream.close();
            }
        }
    }
}

From source file:edu.umn.cs.spatialHadoop.util.FileUtil.java

/**
 * This function to copy a file from local file system to HDFS file stystem
 * // w  ww  . ja  v a2s.c  om
 * @author ibrahimsabek
 * @param localPath
 * @param hdfsPath
 * @throws IOException
 */
private static void copyFromLocal(Path localPath, FileSystem hdfsFS, Path hdfsPath) throws IOException {
    FSDataOutputStream out = hdfsFS.create(hdfsPath);
    FileInputStream localInputStream = new FileInputStream(new File(localPath.toString()));
    int bytesRead;
    byte[] localBuffer = new byte[1024];
    while ((bytesRead = localInputStream.read(localBuffer)) > 0) {
        out.write(localBuffer, 0, bytesRead);
    }

    localInputStream.close();
    out.close();
}