Saturday, December 29, 2007

to save a file

public static void saveInFile(String completePathFile, InputStream in)
            throws IOException {
        BufferedInputStream bis = new BufferedInputStream(in);
        File file = new File(completePathFile);
        if (!file.exists())
            file.createNewFile();

        FileOutputStream stream = new FileOutputStream(completePathFile);
        BufferedOutputStream bos = null;
        try {
            int read_bytes = 0;
            byte[] buffer = new byte[4096];
            bos = new BufferedOutputStream(stream, buffer.length);
            while ((read_bytes = bis.read(buffer, 0, buffer.length)) != -1) {
                bos.write (buffer, 0, read_bytes);
            }
        } catch (IOException ex) {
            throw new IOException("ERROR IN SAVING FILE !! pathFile : "
                    + file.getAbsolutePath());
        } finally {
            bos.flush();
            bos.close();
            stream.close();
        }
    }

No comments: