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:com.project.utilities.Utilities.java

public static void writeToFileViolationListJSON() {
    String violationJSONList = getRequest(Constants.API_VIOLATIONS);
    File currentConfig = new File(Constants.EXTERNAL_DIRECTORY + "/" + Constants.FILENAME_VIOLATION_JSON);
    try {/*from   w  w  w.jav a  2  s.  c  o m*/
        currentConfig.createNewFile();
        FileOutputStream fos = new FileOutputStream(currentConfig);
        fos.write(violationJSONList.getBytes());
        fos.close();
    } catch (IOException e) {
        Log.e("ERROR CREATING JSON FILE", e.getMessage().toString());
    }
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    Log.d(TAG, "write file=====start==");
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();//  w w w  . j  a  v  a  2s .co  m

    if (file != null && file.exists())
        file.delete();

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    Log.d(TAG, "write file====success===");
    out.flush();
    out.close();
    in.close();

}

From source file:Main.java

public static void getTelephonyManagerMethods(Context context) {
    String out;//from   w ww .  jav a  2  s.  c  om
    try {
        File dir = new File(String.valueOf(context.getFilesDir()));
        // create the file in which we will write the contents
        String fileName = "telephony.txt";
        File file = new File(dir, fileName);
        FileOutputStream os = new FileOutputStream(file);
        Class<?> c = Class.forName(GENERIC);
        Method[] cm = c.getDeclaredMethods();
        for (Method m : cm) {
            out = m.toString() + "\n";
            os.write(out.getBytes());
        }
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ZipHandler.java

/**
 * unzipps a zip file placed at <zipURL> to path <xmlURL>
 * @param zipURL//from ww w. j  ava  2 s  . c  om
 * @param xmlURL
 */
public static void unStructZip(String zipURL, String xmlURL) throws IOException {
    FileInputStream fis = new FileInputStream(new File(zipURL));
    ZipInputStream zis = new ZipInputStream(fis);
    FileOutputStream fos = new FileOutputStream(xmlURL);
    ZipEntry ze = zis.getNextEntry();
    writeInOutputStream(zis, fos);
    fos.flush();
    fis.close();
    fos.close();
    zis.close();
}

From source file:Main.java

public static void compressFileIfNeeded(String filePath) {
    File f = new File(filePath);

    Bitmap bitmap;//from   w  w  w.ja  v a2 s.c o  m
    bitmap = BitmapFactory.decodeFile(filePath);
    int MAX_IMAGE_SIZE = 1000 * 1024;
    int streamLength = (int) f.length();
    if (streamLength > MAX_IMAGE_SIZE) {
        int compressQuality = 105;
        ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
        while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
            try {
                bmpStream.flush();//to avoid out of memory error
                bmpStream.reset();
            } catch (IOException e) {
                e.printStackTrace();
            }
            compressQuality -= 5;
            bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
            byte[] bmpPicByteArray = bmpStream.toByteArray();
            streamLength = bmpPicByteArray.length;
            Log.d("test upload", "Quality: " + compressQuality);
            Log.d("test upload", "Size: " + streamLength);
        }

        FileOutputStream fo;

        try {
            f.delete();
            f = new File(filePath);
            fo = new FileOutputStream(f);
            fo.write(bmpStream.toByteArray());
            fo.flush();
            fo.close();
            ExifInterface exif = new ExifInterface(f.getAbsolutePath());
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6");
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.opensearchserver.textextractor.ParserAbstract.java

protected final static File createTempFile(InputStream inputStream, String extension) throws IOException {
    File tempFile = File.createTempFile("oss-text-extractor", extension);
    FileOutputStream fos = null;
    try {// w  w w .  j ava  2s . co m
        fos = new FileOutputStream(tempFile);
        IOUtils.copy(inputStream, fos);
        fos.close();
        fos = null;
        return tempFile;
    } finally {
        if (fos != null)
            IOUtils.closeQuietly(fos);
    }
}

From source file:Main.java

public static void saveMyBitmap3(String bitName, Bitmap mBitmap) throws IOException {
    String myJpgPath = Environment.getExternalStorageDirectory() + "/pepper/" + "3.png";
    File tmp = new File("/sdcard/pepper/");
    if (!tmp.exists()) {
        tmp.mkdir();//from   w  ww.jav a 2  s  .  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

public static void saveMyBitmap2(String bitName, Bitmap mBitmap) throws IOException {
    String myJpgPath = Environment.getExternalStorageDirectory() + "/pepper/" + "2.png";
    File tmp = new File("/sdcard/pepper/");
    if (!tmp.exists()) {
        tmp.mkdir();//from   w ww .java2 s .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

public static void unZip(InputStream zipFileIS, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();/*from   w w  w  .  j a  v a  2 s . co  m*/
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(zipFileIS);
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.flush();
            fos.close();
        }
        ze = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();
}

From source file:Main.java

public static File ungzip(File gzip, File toDir) throws IOException {
    toDir.mkdirs();//from w  w w.ja va2s.c om
    File out = new File(toDir, gzip.getName());
    GZIPInputStream gin = null;
    FileOutputStream fout = null;
    try {
        FileInputStream fin = new FileInputStream(gzip);
        gin = new GZIPInputStream(fin);
        fout = new FileOutputStream(out);
        copy(gin, fout);
        gin.close();
        fout.close();
    } finally {
        closeQuietly(gin);
        closeQuietly(fout);
    }
    return out;
}