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

/**
 * Saves a vcard string to an internal new vcf file.
 * @param vcard the string to save/*from  w ww .ja va  2s .c  o  m*/
 * @param filename the filename of the vcf
 * @param context the context used to open streams.
 */
public static void saveToDisk(String vcard, String filename, Context context) {
    if (TextUtils.isEmpty(vcard) || TextUtils.isEmpty(filename) || context == null) {
        return;
    }
    String path = context.getFilesDir().getAbsolutePath() + File.separator + "peer_profiles";
    File peerProfilesFile = new File(path);
    if (!peerProfilesFile.exists())
        peerProfilesFile.mkdirs();
    FileOutputStream outputStream;
    try {
        outputStream = new FileOutputStream(path + "/" + filename);
        outputStream.write(vcard.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeFile(byte[] buffer, String folder, String fileName) {
    boolean writeSucc = false;
    boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    String folderPath = "";
    if (sdCardExist) {
        folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator;
    } else {/*from  ww  w.j a  v  a  2  s  .  co  m*/
        writeSucc = false;
    }

    File fileDir = new File((folderPath));
    if (!fileDir.exists()) {
        fileDir.mkdirs();
    }

    File file = new File(folderPath + fileName);
    FileOutputStream out = null;

    try {
        out = new FileOutputStream(file);
        out.write(buffer);
        writeSucc = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return writeSucc;
}

From source file:Main.java

/**
 * Clear old data and inserts new data into file.
 * @param filename//from   w  w  w . j a v a  2  s .  co m
 * @param data
 * @return <c>true</c>, if data to file was inserted, <c>false</c> otherwise.</returns>
 */
public static boolean InsertDataToFile(String filename, String data) {
    // get file paht
    String filePath = generateFilePath(filename);
    try {
        // open file to write
        FileOutputStream fos = new Activity().openFileOutput(filePath, Context.MODE_PRIVATE);
        // write data to file
        fos.write(data.getBytes());
        // close file
        fos.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;
}

From source file:com.camel.trainreserve.TicketReserver.java

private static void getCaptchaImg() {
    String url = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=passenger&rand=randp&0.20558934959469144";
    ByteArrayOutputStream outStream = JDKHttpsClient.doGetImg(url, cookieStr);
    if (outStream.size() > 0) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        File imageFile = new File("G://12306OCR-2//" + (new Date()).getTime() + ".png");
        try {//from  w  ww .  j  a va2 s. c o  m
            FileOutputStream fos = new FileOutputStream(imageFile);
            byte[] bytes = outStream.toByteArray();
            fos.write(bytes);
            fos.flush();
            fos.close();
            outStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println(Thread.currentThread().getName() + " return empty");
    }
}

From source file:com.aqnote.shared.cryptology.cert.util.X509CertFileUtil.java

public static void writeCert(String b64cert, String certFileName) throws IOException {

    if (StringUtils.isBlank(b64cert) || StringUtils.isBlank(certFileName)) {
        return;//from   w  w  w.j a  v  a 2 s .  co  m
    }
    FileOutputStream fos1 = new FileOutputStream(certFileName);
    fos1.write(b64cert.getBytes());
    fos1.flush();
    fos1.close();
}

From source file:Main.java

public static void writeFileSdcard(String fileName, String message, boolean append) {
    FileOutputStream fout = null;
    try {//from  w  w w .  j av  a2  s  .  c om
        fout = new FileOutputStream(fileName, append);
        byte[] bytes = message.getBytes();
        fout.write(bytes);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/**
 * Simple copy.  Horrible performance for large files.
 * Good performance for alphabets./*from   ww w.j  a v a 2s.  co m*/
 */
public static void copyFile(File source, File dest) throws Exception {
    FileInputStream fis = new FileInputStream(source);
    try {
        FileOutputStream fos = new FileOutputStream(dest);
        try {
            int read = fis.read();
            while (read != -1) {
                fos.write(read);
                read = fis.read();
            }
        } finally {
            fos.close();
        }
    } finally {
        fis.close();
    }
}

From source file:com.izforge.izpack.test.util.TestHelper.java

/**
 * Helper to create a file of the specified size containing random data.
 *
 * @param file the file/*from  w ww  .ja  v a  2  s .  c  o  m*/
 * @param size the file size
 * @return a new file
 * @throws IOException for any I/O error
 */
public static File createFile(File file, int size) throws IOException {
    byte[] data = new byte[size];
    Random random = new Random();
    random.nextBytes(data);
    FileOutputStream stream = new FileOutputStream(file);
    stream.write(data);
    return file;
}

From source file:Main.java

/**
 * (java)write to file//w w  w  .j  av  a2  s .  co  m
 * 
 * @param str
 * @param path
 */
public static void writeFile(String str, File file) {
    FileOutputStream out;
    try {
        file.createNewFile();

        out = new FileOutputStream(file, false);
        String infoToWrite = str;
        out.write(infoToWrite.getBytes());
        out.close();

    } catch (IOException e) {
        Log.e("", "write error!");
    }
}

From source file:com.domnian.BotConfiguration.java

public static void writeDefault(File file) throws Exception {
    JSONObject defaultJson = new JSONObject();
    JSONObject connectDefault = new JSONObject();
    JSONObject identDefault = new JSONObject();
    JSONObject nickservDefault = new JSONObject();
    connectDefault.put("host", "irc.spi.gt");
    connectDefault.put("port", 6667);
    connectDefault.put("ssl", false);
    connectDefault.put("pass", "");
    connectDefault.put("channel", "#willies952002");
    defaultJson.put("connect", connectDefault);
    identDefault.put("user", "WilliesIRCBot");
    identDefault.put("nick", "WilliesIRCBot");
    identDefault.put("real", "Willies IRC Bot");
    defaultJson.put("ident", identDefault);
    nickservDefault.put("run", false);
    nickservDefault.put("email", "email@example.com");
    nickservDefault.put("password", "");
    defaultJson.put("nickserv", nickservDefault);
    defaultJson.put("manage", "admin");
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(defaultJson.toString().getBytes("UTF-8"));
    fos.close();//from  w  w  w. ja v a 2 s  . c om
}