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[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this file output stream.

Usage

From source file:Main.java

protected static void inputStreamToFile(InputStream is, File to) throws IOException {
    FileOutputStream out = null;
    try {/*  ww  w. jav a2s  .  com*/
        out = new FileOutputStream(to);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
    } finally {
        if (out != null) {
            out.close();
        }
        is.close();
    }
}

From source file:Main.java

public static String catchStreamToFile(String catchFile, InputStream inStream) throws Exception {

    File tempFile = new File(catchFile);
    try {/*from   www . j  av  a  2s.  c o m*/
        if (tempFile.exists()) {
            tempFile.delete();
        }
        tempFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, len);
    }
    inStream.close();
    fileOutputStream.close();
    return catchFile;
}

From source file:Main.java

protected static File unzipEntry(ZipInputStream zis, File targetFile) throws Exception {
    FileOutputStream fos = null;
    try {//from  ww  w . j av a2 s.c  om
        fos = new FileOutputStream(targetFile);

        byte[] buffer = new byte[BUFFER_SIZE];
        int len = 0;
        while ((len = zis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
    return targetFile;
}

From source file:in.co.sneh.model.CargaExcelRuralModel.java

public static boolean processFile(String path, FileItemStream item) {
    try {/*from  w w  w.ja v  a 2  s.c  o m*/
        File f = new File(path + File.separator + "exceles" + File.separator);
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();
        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read(b)) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return false;
}

From source file:com.elit2.app.model.FileUpload.java

public static boolean proccessFile(String path, FileItemStream item) {
    try {/*from  www  .  j  a v  a2  s . c o m*/
        File f = new File(path + File.separator + "images");
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();

        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read()) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void writeStreamToFile(InputStream is, File file) {
    FileOutputStream fos = null;
    try {/*from  w  w w  .ja v a  2s .c o  m*/
        fos = new FileOutputStream(file);
        byte[] buffer = new byte[4096];
        int length = 0;
        while ((length = is.read(buffer)) != -1) {
            fos.write(buffer, 0, length);
            fos.flush();
        }
        Log.d("tg", "FileUtils writeStreamToFile successed...");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/** Downloads a file from the specified URL and stores the file to the specified location 
 * @param fileUrl the URL from which the file should be downloaded
 * @param storageLocation the location to which the downloaded file should be stored. If the file exists, it will
 * be overridden! /*  w  ww  .  ja va2  s. c  om*/
 * @throws IOException */
public static void downloadFileFromWebserver(String fileUrl, String storageLocation) throws IOException {
    URL url = new URL(fileUrl);
    File file = new File(storageLocation);

    URLConnection urlConnection = url.openConnection();
    InputStream inputStream = urlConnection.getInputStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    FileOutputStream fileOutputStream = new FileOutputStream(file);

    byte[] buffer = new byte[1024];
    int bytesInBuffer = 0;
    while ((bytesInBuffer = bufferedInputStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, bytesInBuffer);
    }

    fileOutputStream.close();
}

From source file:Main.java

public static boolean saveRes(Context gContext, int res, String dirjpg) {
    InputStream is = gContext.getApplicationContext().getResources().openRawResource(res);
    boolean rez = false;
    try {/* ww  w  . j a v a2 s .c o  m*/
        FileOutputStream rtFOS = new FileOutputStream(dirjpg + "/" + res + ".gif");
        byte[] buffer = new byte[4096];
        int n = -1;
        while ((n = is.read(buffer, 0, 4095)) != -1) {
            if (n > 0)
                rtFOS.write(buffer, 0, n);
            rez = true;
        }
        rtFOS.flush();
        rtFOS.close();
    } catch (Exception e) {
        Log.e("FullscreenActivity", e.toString());
    }
    return rez;
}

From source file:Main.java

public static void writeInputStreamToFile(InputStream is, String fileName, Context context)
        throws FileNotFoundException, IOException {

    FileOutputStream fos = null;

    try {/*from   w  ww .  j  av a2 s  . com*/
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        byte buf[] = new byte[1024];
        int len = 0;
        while ((len = is.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }
        fos.close();

    } catch (Exception exp) {
        Log.v(TAG, "Failed to write to file exp:" + exp.getMessage());
        exp.printStackTrace();
    }
}

From source file:Main.java

public static boolean copy(File source, File target) {
    if (source == null || target == null || !source.exists() || source.length() < 100) {
        return false;
    }/*  ww w . jav a  2s. c  o  m*/
    try {
        FileOutputStream fos = new FileOutputStream(target);
        FileInputStream fis = new FileInputStream(source);
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        fos.flush();
        fos.close();
        fis.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}