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

/**
 * Write file to sdcard/*w w  w  .  j  av  a2  s . c o m*/
 * @param dir
 * @param bt
 * @return
 */
public synchronized static File writeToSDCard(String dir, byte[] bt) {
    File file = null;
    try {
        file = new File(dir);
        if (file.exists()) {
            return file;
        }
        file.createNewFile();

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bt);
        fos.close();
        return file;
    } catch (Exception e) {
        if (file.exists()) {
            file.delete();
        }
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void saveToFile(Bitmap bmp, String filePath) throws IOException {
    File file = new File(filePath);
    if (!file.exists()) {
        file.createNewFile();/*from w ww. ja  v  a  2 s  .  c  o m*/
    }
    FileOutputStream fout = new FileOutputStream(file);
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, fout);
    fout.close();
}

From source file:Main.java

public static void cacheStringToFile(String str, String filename) {
    File f = new File(filename);
    if (f.exists()) {
        f.delete();/*from   w  ww . j  av a  2 s .  com*/
    }
    try {
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        FileOutputStream fos = new FileOutputStream(f, true);
        fos.write(str.getBytes());
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static String storeImage(Context context, Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File f = null;/*ww w.  j  a v a2s.  c  o m*/
    try {
        f = File.createTempFile("citationsImg", ".png", context.getExternalCacheDir());
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return f.getAbsolutePath();
}

From source file:Main.java

public static void write(Context context, byte[] data, String fileName)
        throws FileNotFoundException, IOException {

    File file = getFile(context, fileName);

    if (file != null && data != null) {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data);/* w  ww .ja  va2 s.c om*/
        fos.close();
    }
}

From source file:de.unimannheim.spa.process.util.FileUtils.java

public static File convertMultipartToFile(MultipartFile multipartFile)
        throws IllegalStateException, IOException {
    File convertedFile = new File(multipartFile.getOriginalFilename());
    FileOutputStream fos = new FileOutputStream(convertedFile);
    fos.write(multipartFile.getBytes());
    fos.close();
    return convertedFile;
}

From source file:Main.java

public static void saveCover(Bitmap cover, String path) {
    try {/*from  w w  w . j a  v  a2s. c  o m*/
        FileOutputStream out = new FileOutputStream(path);
        cover.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
    } catch (Exception e) {
        // Do nothing
    }
}

From source file:Main.java

public static void writeFile(String fileName, String write_str) throws IOException {
    File file = new File(fileName);
    FileOutputStream fos = new FileOutputStream(file);
    byte[] bytes = write_str.getBytes();
    fos.write(bytes);//from www .j  a  va 2 s.c  o m
    fos.close();
}

From source file:Main.java

/**
 * Saves a file from the given URL using HTTPS to the given filename and returns the file
 * @param link URL to file//from w w  w. j a v  a  2s  . c o m
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static void saveFileFromNetHTTPS(URL link, String fileName) throws IOException {
    HttpsURLConnection con = (HttpsURLConnection) link.openConnection();

    InputStream in = new BufferedInputStream(con.getInputStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    File f = new File(fileName);
    if (f.getParentFile() != null) {
        if (f.getParentFile().mkdirs()) {
            System.out.println("Created Directory Structure");
        }
    }

    FileOutputStream fos = new FileOutputStream(f);
    fos.write(response);
    fos.close();
}

From source file:Main.java

public static void save(String filename, String content, Context context) throws Exception {
    FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
    outStream.write(content.getBytes());
    outStream.close();
}