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

/**
 * @param fileName /*w w  w. ja  v a2s  .  c o  m*/
 * @param message
 */
public static void writeFileSdcard(String fileName, String message) {

    try {

        FileOutputStream fout = new FileOutputStream(fileName);
        byte[] bytes = message.getBytes();
        fout.write(bytes);
        fout.flush();
        fout.close();
    }

    catch (Exception e) {

        e.printStackTrace();

    }

}

From source file:de.huxhorn.lilith.tools.CreateMd5Command.java

public static boolean createMd5(File input) {
    final Logger logger = LoggerFactory.getLogger(CreateMd5Command.class);

    if (!input.isFile()) {
        if (logger.isWarnEnabled())
            logger.warn("{} isn't a file!", input.getAbsolutePath());
        return false;
    }/*from   w w w .  j  av  a2  s  .c  o m*/
    File output = new File(input.getParentFile(), input.getName() + ".md5");

    try {

        FileInputStream fis = new FileInputStream(input);
        byte[] md5 = ApplicationPreferences.getMD5(fis);
        if (md5 == null) {
            if (logger.isWarnEnabled()) {
                logger.warn("Couldn't calculate checksum for {}!", input.getAbsolutePath());
            }
            return false;
        }
        FileOutputStream fos = new FileOutputStream(output);
        fos.write(md5);
        fos.close();
        if (logger.isInfoEnabled()) {
            logger.info("Wrote checksum of {} to {}.", input.getAbsolutePath(), output.getAbsolutePath());
            logger.info("MD5: {}", Hex.encodeHexString(md5));
        }
    } catch (IOException e) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while creating checksum!", e);
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * Take the screen shot of the device//w  ww .  j a v  a  2  s . co m
 * 
 * @param view
 */
public static void screenShotMethod(View view) {
    Bitmap bitmap;
    if (view != null) {
        View v1 = view;
        v1.setDrawingCacheEnabled(true);
        v1.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + "CySmart" + File.separator + "file.jpg");
        try {
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:Main.java

public static void write(Context context, String fileName, String content) {
    if (content == null)
        content = "";

    try {/* ww w.j a  v a 2s  .c  om*/
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        fos.write(content.getBytes());
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void writeInstallationFile(File installation) throws IOException {
    FileOutputStream out = new FileOutputStream(installation);
    String id = UUID.randomUUID().toString();
    out.write(id.getBytes());
    out.close();//ww  w  . j  a  va 2s.co m
}

From source file:Main.java

public static boolean writeToExternalStoragePublic(String filename, String fileContent) {
    boolean saved = false;
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
    if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {
        try {/*from  w w w .j  av a  2 s .c om*/
            boolean exists = (new File(path)).exists();
            if (!exists) {
                new File(path).mkdirs();
            }

            //Remote legacy file
            File file = new File(path + filename);
            file.delete();

            // Open output stream
            FileOutputStream fOut = new FileOutputStream(path + filename, true);
            fOut.write(fileContent.getBytes());
            // Close output stream
            fOut.flush();
            fOut.close();

            saved = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return saved;
}

From source file:Main.java

/**
 * Write data to file./* w ww  .  j av  a  2 s . c  o m*/
 *
 * @param fileName
 * @param message
 */
public static void write2File(String fileName, String message, boolean append) {
    try {
        FileOutputStream outStream = new FileOutputStream(fileName, append);
        byte[] bytes = message.getBytes();
        outStream.write(bytes);
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:fr.asso.vieillescharrues.parseurs.TelechargeFichier.java

/**
 * Mthode statique grant le tlechargement de fichiers
 * @param url Adresse du fichier/*from   w w w. j  a  v  a 2s  . c  om*/
 * @param fichierDest Nom du ficher en local
 */
public static void DownloadFromUrl(URL url, String fichierDest) throws IOException {
    File file;
    if (fichierDest.endsWith(".jpg"))
        file = new File(PATH + "images/", fichierDest);
    else
        file = new File(PATH, fichierDest);
    file.getParentFile().mkdirs();
    URLConnection ucon = url.openConnection();

    try {
        tailleDistant = ucon.getHeaderFieldInt("Content-Length", 0); //Rcupre le header HTTP Content-Length
        tailleLocal = (int) file.length();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // Compare les tailles des fichiers
    if ((tailleDistant == tailleLocal) && (tailleLocal != 0))
        return;

    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(baf.toByteArray());
    fos.close();
}

From source file:subhan.portal.config.Util.java

public static void downloadFromUrl(String downloadUrl, String fileName) throws IOException {
    File root = android.os.Environment.getExternalStorageDirectory(); // path ke sdcard

    File dir = new File(root.getAbsolutePath() + "/youread"); // path ke folder

    if (dir.exists() == false) { // cek folder eksistensi
        dir.mkdirs(); // kalau belum ada, dibuat
    }//from w  ww  .  j  av  a  2  s.c  o m

    URL url = new URL(downloadUrl); // you can write here any link
    File file = new File(dir, fileName);

    // Open a connection to that URL. 
    URLConnection ucon = url.openConnection();

    // Define InputStreams to read from the URLConnection. 
    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    // Read bytes to the Buffer until there is nothing more to read(-1).
    ByteArrayBuffer baf = new ByteArrayBuffer(5000);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }

    // Convert the Bytes read to a String. 
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(baf.toByteArray());
    fos.flush();
    fos.close();
}

From source file:Main.java

private static void saveToFile(byte[] data, String path) {
    if (data == null || data.length == 0) {
        return;/* w  w  w  .  j a v  a2  s.c  o m*/
    }
    if (TextUtils.isEmpty(path)) {
        return;
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(path, true);
        fos.write(data);
        fos.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}