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 WriteFile(String file, String message) throws IOException {
    File f = new File(file);
    if (!f.exists()) {
        f.createNewFile();/*from  w  w w  .  j  a  va 2 s . c om*/
    }
    FileOutputStream fout = new FileOutputStream(file);
    byte[] bytes = message.getBytes();
    fout.write(bytes);
    fout.close();
}

From source file:Main.java

/**
 * Saves a file from the given URL to the given filename and returns the file
 * @param link URL to file//from   w  w  w  .j av a  2 s .c om
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static File saveFileFromNet(URL link, String fileName) throws IOException {
    InputStream in = new BufferedInputStream(link.openStream());
    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 file = new File(fileName);
    if (!file.exists()) {
        if (file.getParentFile() != null) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(response);
    fos.close();

    return new File(fileName);
}

From source file:Main.java

/**
 * Check is a file is writable. Detects write issues on external SD card.
 *
 * @param file The file/*from w w  w  . jav a  2s  . c  o  m*/
 * @return true if the file is writable.
 */
public static boolean isWritable(@NonNull final File file) {
    boolean isExisting = file.exists();

    try {
        FileOutputStream output = new FileOutputStream(file, true);
        try {
            output.close();
        } catch (IOException e) {
            // do nothing.
        }
    } catch (FileNotFoundException e) {
        return false;
    }
    boolean result = file.canWrite();

    // Ensure that file is not created during this process.
    if (!isExisting) {
        //noinspection ResultOfMethodCallIgnored
        file.delete();
    }

    return result;
}

From source file:Main.java

public static void byteArrayToFile(byte[] byteData, File filePath) {

    try {// ww  w .j  a  va 2s.  c o m
        FileOutputStream fos = new FileOutputStream(filePath);
        fos.write(byteData);
        fos.close();

    } catch (FileNotFoundException ex) {
        System.out.println("FileNotFoundException : " + ex);
    } catch (IOException ioe) {
        System.out.println("IOException : " + ioe);
    }

}

From source file:Main.java

public static void writeToPublicDirectory(String filename, byte[] data, String directory,
        String environmentDirectory) throws Exception {
    File publicDirectory = new File(Environment.getExternalStoragePublicDirectory(environmentDirectory),
            directory);/*from ww  w .  j  a  v a  2 s .c om*/
    boolean result = publicDirectory.mkdirs();
    File targetFile = new File(publicDirectory, filename);
    FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
    fileOutputStream.write(data);
    fileOutputStream.close();
}

From source file:Main.java

public static void data2file(byte[] w, String fileName) throws Exception {
    FileOutputStream fos = null;
    try {//from   ww  w  .  j  a v a2s. co  m
        fos = new FileOutputStream(fileName);
        fos.write(w);
        fos.close();
    } catch (Exception e) {
        if (fos != null)
            fos.close();
        throw e;
    }
}

From source file:Main.java

public static void CacheString(String data, String filename, Context ctx) {
    try {//from w  w w  . j a  v a2  s.  c o  m
        FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
        fos.write(data.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void writeFile(Context context, String text, String fileName) {
    try {/*from  ww w .ja va2s .c o m*/
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_APPEND);
        fos.write(text.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void setCache(String rawJson) {
    try {/*  www .ja  v  a 2s . co  m*/
        FileOutputStream fileOutputStream = new FileOutputStream(cache);
        fileOutputStream.write(rawJson.getBytes());
        fileOutputStream.close();
    } catch (IOException e) {
        //???
        e.printStackTrace();
    }
}

From source file:Main.java

public static File WriteStreamToFile(InputStream resStream) {
    try {/*  w ww. j  a  v a  2 s .  c  o  m*/
        byte[] bytes = new byte[resStream.available()];
        File tmpFile = File.createTempFile("z4-", ".tmp");
        tmpFile.deleteOnExit();
        DataInputStream dis = new DataInputStream(resStream);
        dis.readFully(bytes);
        FileOutputStream foutStream = new FileOutputStream(tmpFile.getPath());
        foutStream.write(bytes);
        foutStream.close();
        dis.close();
        return tmpFile;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}