Example usage for java.io FileOutputStream close

List of usage examples for java.io FileOutputStream close

Introduction

In this page you can find the example usage for java.io FileOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file output stream and releases any system resources associated with this stream.

Usage

From source file:Main.java

public static void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();//  w  ww.  j av a  2s.  c o m
    outStream.flush();
    outStream.close();
}

From source file:Main.java

/**
 * Decompresses a given byte array that is a compressed folder.
 * /*from www. jav  a  2s . c  o m*/
 * @param folderAsCompressedArray to decompress
 * @param unzippedLocation where the decompressed folder should be
 * @throws IOException e
 * @throws FileNotFoundException e
 */
public static void decompressFolderByteArray(byte[] folderAsCompressedArray, File unzippedLocation)
        throws IOException, FileNotFoundException {
    ZipInputStream zipFile = new ZipInputStream(new ByteArrayInputStream(folderAsCompressedArray));
    ZipEntry ze = null;
    final int minusOne = -1;
    while ((ze = zipFile.getNextEntry()) != null) {
        FileOutputStream fout = new FileOutputStream(
                new File(unzippedLocation, ze.getName()).getAbsolutePath());
        for (int c = zipFile.read(); c != minusOne; c = zipFile.read()) {
            fout.write(c);
        }
        zipFile.closeEntry();
        fout.close();
    }
    zipFile.close();
}

From source file:Main.java

public static void writeInputStreamToFile(InputStream is, String fileName, Context context)
        throws FileNotFoundException, IOException {

    FileOutputStream fos = null;

    try {/*from   w  w  w. ja v  a 2  s.c o m*/
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        byte buf[] = new byte[1024];
        int len = 0;
        while ((len = is.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }
        fos.close();

    } catch (Exception exp) {
        Log.v(TAG, "Failed to write to file exp:" + exp.getMessage());
        exp.printStackTrace();
    }
}

From source file:Main.java

public static void saveMyBitmap(String bitName, Bitmap mBitmap) throws IOException {
    String myJpgPath = Environment.getExternalStorageDirectory() + "/pepper/" + "1.png";
    File tmp = new File("/sdcard/pepper/");
    if (!tmp.exists()) {
        tmp.mkdir();//w  ww. j  a  v  a2s  .  c  o m
    }
    File f = new File(myJpgPath);
    f.createNewFile();
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * This method allow to append a message to an error file
 *
 * @param filename//w w  w . j av  a  2 s.com
 *            the error file to write
 * @param message
 *            the message to append to file
 */
public static void errorLog(String filename, String message) {
    try {
        File file = new File(filename);
        String path = file.getParent();
        if (path != null) {
            new File(path).mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(file, true); // true->append
        String time = "" + (new java.sql.Timestamp(System.currentTimeMillis()));
        message = time + "  " + message + "\n";
        fos.write(message.getBytes());
        fos.close();
    } catch (Exception e) {
        System.err.println("Unable to write file: " + filename);
        e.printStackTrace();
    }
}

From source file:Main.java

public static void unzip(InputStream is, String dir) throws IOException {
    File dest = new File(dir);
    if (!dest.exists()) {
        dest.mkdirs();//from   w w  w  . j a  v  a  2 s.c o  m
    }

    if (!dest.isDirectory())
        throw new IOException("Invalid Unzip destination " + dest);
    if (null == is) {
        throw new IOException("InputStream is null");
    }

    ZipInputStream zip = new ZipInputStream(is);

    ZipEntry ze;
    while ((ze = zip.getNextEntry()) != null) {
        final String path = dest.getAbsolutePath() + File.separator + ze.getName();

        String zeName = ze.getName();
        char cTail = zeName.charAt(zeName.length() - 1);
        if (cTail == File.separatorChar) {
            File file = new File(path);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    throw new IOException("Unable to create folder " + file);
                }
            }
            continue;
        }

        FileOutputStream fout = new FileOutputStream(path);
        byte[] bytes = new byte[1024];
        int c;
        while ((c = zip.read(bytes)) != -1) {
            fout.write(bytes, 0, c);
        }
        zip.closeEntry();
        fout.close();
    }
}

From source file:com.chinarewards.gwt.license.util.ImageDisposeUtil.java

/**
 * @param imgsrc   //from  w  w  w. j a  va2  s .c om
 * @param imgdist ? 
 * @param widthdist  
 * @param heightdist 
 * @param int benchmark :0,12
 * 
 */
public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist, int benchmark) {

    try {
        // System.out.println("*******widthdist********:"+widthdist);
        // System.out.println("*******heightdist********:"+heightdist);
        // System.out.println("*******benchmark********:"+benchmark);
        File srcfile = new File(imgsrc);
        if (!srcfile.exists()) {
            return;
        }
        Image src = javax.imageio.ImageIO.read(srcfile);
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        if (width <= widthdist && height <= heightdist) {
            // SysUtil.cpoyFile(imgsrc, imgdist);
            FileUtils.copyFile(new File(imgsrc), new File(imgdist));
            return;
        }
        // 
        float wh = (float) width / (float) height;
        if (benchmark == 0) {
            if (wh > 1) {
                float tmp_heigth = (float) widthdist / wh;
                BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB);
                tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null);
                FileOutputStream out = new FileOutputStream(imgdist);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(tag);
                out.close();
            } else {
                float tmp_width = (float) heightdist * wh;
                BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB);
                tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null);
                FileOutputStream out = new FileOutputStream(imgdist);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(tag);
                out.close();
            }
        }
        if (benchmark == 1) {
            float tmp_heigth = (float) widthdist / wh;
            BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null);
            FileOutputStream out = new FileOutputStream(imgdist);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
            out.close();
        }
        if (benchmark == 2) {
            float tmp_width = (float) heightdist * wh;
            BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null);
            FileOutputStream out = new FileOutputStream(imgdist);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
            out.close();
        }
    } catch (IOException ex) {
        logger.error(ex);
    }
}

From source file:org.processbase.ui.core.Constants.java

private static void save() throws FileNotFoundException, IOException {
    String userHomeDir = System.getProperty("user.home");
    File file = new File(userHomeDir + "/processbase3.properties");
    properties.setProperty("APP_SERVER", "GLASSFISH3");
    properties.setProperty("TASKLIST_PAGE_URL", "/web/guest/bpm-console");
    properties.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
    properties.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
    properties.setProperty("java.naming.factory.state",
            "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
    properties.setProperty("java.naming.provider.bonitaurl", "iiop://localhost:23700");
    properties.setProperty("java.security.auth.login.config", "appclientlogin.conf");
    properties.setProperty("DL_GROUP", "DOCUMENTS");
    properties.setProperty("BONITA_DOMAIN", "default");

    properties.setProperty("hibernate.connection.datasource", "jdbc/pbbam");
    properties.setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
    properties.setProperty("CUSTOM_UI_JAR_PATH", "/processbasecustomuijar");

    properties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
    properties.setProperty("org.omg.CORBA.ORBInitialPort", "23700");

    FileOutputStream fos = new FileOutputStream(file);
    properties.store(fos, null);/*from   ww w .ja  va2  s  .c  o  m*/
    fos.close();
}

From source file:Main.java

public static boolean saveToFile(Bitmap bitmap, File file) {
    FileOutputStream outputStream = null;
    try {/*ww w  .  j a v  a  2  s.  c  o m*/
        outputStream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, outputStream);
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        try {
            outputStream.close();
        } catch (Exception e) {
            // nothing
        }
    }
}

From source file:Main.java

/**
 * Copy file a to b and remove a. //from  w  w  w . ja v  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;
}