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

public static void writeSDFile(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 w  ww .j a va 2s.  c om
}

From source file:Main.java

public static String loadImageFromUrl(Context context, String imageURL, File file) {
    try {//  ww  w .  jav a  2s .com
        URL url = new URL(imageURL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setDoInput(true);
        con.connect();
        if (con.getResponseCode() == 200) {
            InputStream inputStream = con.getInputStream();

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 20];
            int length = -1;
            while ((length = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
            byteArrayOutputStream.close();
            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
            return file.getPath();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

@SuppressWarnings("unused")
private static void saveImage(byte[] data) {
    File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
    if (pictureFile == null) {
        Log.d(TAG, "Error creating media file, check storage permissions");
        return;/*from www . ja  va2s  . c om*/
    }

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }
}

From source file:Main.java

/**
 * DB test//from w w w.j a  va2  s .co m
 */
public static void runBackup(Context context) {
    File file = context.getDatabasePath("PhotoDeskHiddenFolder.db");
    int size = (int) file.length();

    String path = Environment.getExternalStorageDirectory() + "/PhotoDesk/";
    try {
        byte[] buffer = new byte[size];
        InputStream inputStream = new FileInputStream(file);
        inputStream.read(buffer);
        inputStream.close();

        File outputDBDirectory = new File(path);
        if (!outputDBDirectory.isDirectory())
            outputDBDirectory.mkdir();

        path += "test.db";

        File outputFile = new File(path);
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(buffer);
        outputStream.flush();
        outputStream.close();

    } catch (Exception e) {
    }
}

From source file:Main.java

public static void writeToFile(File file, byte[] data) throws IOException {
    FileOutputStream fou = null;
    try {//from   w w w.jav  a2s.c o m
        fou = new FileOutputStream(file);
        fou.write(data);
    } finally {
        if (fou != null) {
            try {
                fou.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static void write(Context context, String fileName, String content) {
    if (content == null) {
        content = "";
    }//  w  w  w.j  a v a2  s.  c  o m
    try {
        FileOutputStream fs = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        fs.write(content.getBytes());
        fs.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * save private key and public key of a keypair in the directory
 *
 * @param dir//  w ww .  j av  a2 s. c  om
 * @param keyPair
 * @param name keys will be stored as name_private.key and name_public.key
 * @throws IOException
 */
public static void saveKeyPair(File dir, KeyPair keyPair, String name) throws IOException {

    // Store Public Key.
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded());
    FileOutputStream fos = new FileOutputStream(new File(dir, name + "_public.key"));
    fos.write(x509EncodedKeySpec.getEncoded());
    fos.close();

    // Store Private Key.
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded());
    fos = new FileOutputStream(new File(dir, name + "_private.key"));
    fos.write(pkcs8EncodedKeySpec.getEncoded());
    fos.close();
}

From source file:Main.java

public static void logGlobalException(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {// w  w  w. j a  v a 2 s .co  m
        File file = new File(dir + "/error_logs.log");
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file, true);

        for (String log : logs) {
            fos.write((log + "\n\n").getBytes());
        }
        fos.close();
    } catch (IOException ioe) {
        Log.e("AndroidUtils", "An exception has occured in logGlobalException!");
        ioe.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates a File from a Bitmap//from www  . j  a  v a2  s .c o  m
 *
 * @param bitmap to convert in a file
 *
 * @return File
 */
public static File createFileFromBitmap(Bitmap bitmap) {

    if (bitmap == null)
        return null;

    File photoFile = null;

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    File photoStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    if (photoStorage != null) {
        photoFile = new File(photoStorage, (System.currentTimeMillis()) + ".png");
        try {
            //f.createNewFile();
            FileOutputStream fo = new FileOutputStream(photoFile);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            Log.e(TAG, "Error saving image ", e);
        }
    }

    return photoFile;
}

From source file:org.apache.camel.example.SpringJmsClientServerTest.java

@BeforeClass
public static void setupFreePort() throws Exception {
    // find a free port number, and write that in the custom.properties file
    // which we will use for the unit tests, to avoid port number in use problems
    int port = AvailablePortFinder.getNextAvailable();
    String bank1 = "tcp.port=" + port;

    File custom = new File("target/custom.properties");
    FileOutputStream fos = new FileOutputStream(custom);
    fos.write(bank1.getBytes());
    fos.close();/*from   w w w .jav a2  s.c o  m*/
}