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 data2file(byte[] w, String fileName) throws Exception {
    FileOutputStream fos = null;
    try {//w  w  w .j  a  v  a 2  s  .  com
        fos = new FileOutputStream(fileName);
        fos.write(w);
        fos.close();
    } catch (Exception e) {
        if (fos != null)
            fos.close();
        throw e;
    }
}

From source file:Main.java

/**
 * Given a byte array of content, writes it to a temporary file and then
 * returns the path to it as a URL//from   w  ww . j  av a 2  s. co  m
 * @param contents The content of the file
 * @param type The file extension to use
 * @throws IOException if an error occurs
 */
public static URL stringToTempFile(byte[] contents, String type) throws IOException {

    // Make sure we have a temp directory
    checkForTempDirectory();

    // Generate a random file name and keep doing it until we get a unique one
    File f = null;
    String fName = null;
    do {
        fName = tempDirectory + File.separator + ((int) (Math.random() * 10000000)) + "." + type;
        f = new File(fName);
    } while (f.exists());

    System.out.println("TEMP: Creating temp file " + fName);
    FileOutputStream out = new FileOutputStream(f);
    out.write(contents);
    out.close();

    // Remember this file for later deletion
    tempFileList.add(fName);

    return new URL("file://" + fName);
}

From source file:br.edu.ifpb.utils.PhotoUtils.java

public static String salvarFoto(String nomePasta, String nomeFoto, InputStream foto) {
    BufferedImage bf = null;//from   ww w .j av a2 s.  c  o  m
    String path = nomePasta;

    try {
        bf = ImageIO.read(foto);
        path = path.replaceAll("%20", " ");
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(path + nomeFoto);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getByteImage(bf));

        //            return path.substring(path.indexOf("img/")) + nomeFoto;
        return path + nomeFoto;
    } catch (IOException ex) {
        throw new CouldNotCreateFileException("O arquivo " + (path + nomeFoto) + " no pde ser criado.");
    }
}

From source file:Main.java

public static void bitmapToFile(Bitmap bitmap, File file) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] bitmapdata = bos.toByteArray();
    FileOutputStream fos;
    try {/*  www  .  j av  a 2s .c  om*/
        fos = new FileOutputStream(file);
        fos.write(bitmapdata);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/***
 * write a line to the log file/*from   w w  w.j  a  va 2s  . c o  m*/
 * 
 * @param line
 */
public static void writeToLog(String line) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + folderName);
    boolean success = true;
    if (!f.exists()) {
        success = f.mkdir();
    }
    if (success) {
        // Do something on success
        File fileDir = new File(f, dateFormat.format(date) + ".txt");
        try {
            FileOutputStream os = new FileOutputStream(fileDir, true);
            os.write(line.getBytes());
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean compress(Bitmap bitmap, File outFile) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 35, baos);
    try {/*from  w ww .  j ava2s . co m*/
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);
        fileOutputStream.write(baos.toByteArray());
        fileOutputStream.close();
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static void saveNamedFile(String patch, String data, String name) {

    try {/*from w ww  . j  av  a 2s .c o m*/

        File direct = new File(patch);
        if (!direct.exists())
            direct.mkdirs();

        File f = new File(patch + "/" + name);
        if (!f.exists())
            f.createNewFile();

        FileOutputStream fos = new FileOutputStream(f);
        String s = data;
        fos.write(s.getBytes("UTF-8"));
        fos.close();

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }

}

From source file:Main.java

public static void saveImage(String imagePath, byte[] buffer) throws IOException {
    File f = new File(imagePath);
    if (f.exists()) {
        return;/*ww w  .j  av a 2  s  .  c  om*/
    } else {
        File parentFile = f.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(imagePath);
        fos.write(buffer);
        fos.flush();
        fos.close();
    }
}

From source file:Main.java

public static String writeBitmap(byte[] data) throws IOException {
    File file = new File(Environment.getExternalStorageDirectory() + "/bookclip/");
    file.mkdir();//from w  w w .  j  a  v  a 2  s  . c  o m
    String bitmapPath = file.getPath() + "/" + System.currentTimeMillis() + ".jpg";
    FileOutputStream outStream = new FileOutputStream(bitmapPath);
    outStream.write(data);
    outStream.close();

    return bitmapPath;
}

From source file:org.apache.camel.example.SpringJmsClientRemotingServerTest.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  ww  .  j a v  a2 s.  c  o  m*/

    appCtx = new ClassPathXmlApplicationContext("/META-INF/spring/camel-server.xml",
            "camel-client-remoting.xml");
    appCtx.start();
}