Example usage for java.io FileOutputStream write

List of usage examples for java.io FileOutputStream write

Introduction

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

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this file output stream.

Usage

From source file:Main.java

/**
 * Writes the given value into the given file
 *
 * @return true on success, false on failure
 */// w ww .j a v  a 2 s .c  om
public static boolean writeLine(String fileName, String value) {
    try {
        FileOutputStream fos = new FileOutputStream(fileName);
        fos.write(value.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        Log.e(TAG, "Could not write to file " + fileName, e);
        return false;
    }

    return true;
}

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/*  www  . ja  v  a  2 s . c  o m*/
 * @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

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

    try {//from  www . j a  v a  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

/**
 * Takes in the file stream to the offline file storage that is going to be written to and
 * the latest trip data that is going to be written to the offline storage file.
 * <p/>/*  w  w  w. j a va2s .co  m*/
 * The new trip data will then be appended to the rest of the trip data.
 *
 * @param writer The output stream of the offline file for storage. Get this from {@link android.content.Context#openFileOutput}
 * @param line   The new trip data as a String
 */
public synchronized static void writeToOfflineStorage(FileOutputStream writer, String line) {
    try {
        line += "\n";
        writer.write(line.getBytes());
    } catch (IOException e) {
        //TODO: Handle IO exception
        Log.d("ERROR", "IO Exception in OfflineUtilities#writeToOfflineStorage: " + e.getMessage());
    }
}

From source file:Main.java

public static File WriteStreamToFile(InputStream resStream) {
    try {/*w w  w  .java  2s. c  om*/
        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;
}

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);
    fos.close();/*from   ww w  .ja v  a 2s. co m*/
}

From source file:Main.java

public static void createFile(File file, int numBytes) throws IOException {
    File parentFile = file.getParentFile();
    if (parentFile != null) {
        parentFile.mkdirs();/*  www.  ja  v a 2  s .  c  o m*/
    }
    byte[] buffer = new byte[numBytes];
    FileOutputStream output = new FileOutputStream(file);
    try {
        output.write(buffer);
    } finally {
        output.close();
    }
}

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  .  jav  a  2  s  .c om
    }
    FileOutputStream fout = new FileOutputStream(file);
    byte[] bytes = message.getBytes();
    fout.write(bytes);
    fout.close();
}

From source file:Main.java

public static void put(String s, String name) {
    try {//from  w  w  w.  ja v a2 s  .  c o  m
        File saveFile = new File(Environment.getExternalStorageDirectory() + "/hhtlog/" + name + ".txt");
        if (!saveFile.exists()) {
            File dir = new File(saveFile.getParent());
            dir.mkdirs();
            saveFile.createNewFile();
        }

        FileOutputStream outStream = new FileOutputStream(saveFile);
        outStream.write(s.getBytes());
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveString(String filename, String jsonString) throws Exception {
    File file = new File(sAppContext.getCacheDir().getPath() + "/" + filename);
    FileOutputStream fos = new FileOutputStream(file);
    byte[] buff = jsonString.getBytes();
    fos.write(buff);
    fos.flush();/*from   w ww. j a va  2s  . c  o  m*/
    fos.close();
    return true;
}