Example usage for java.io OutputStream write

List of usage examples for java.io OutputStream write

Introduction

In this page you can find the example usage for java.io OutputStream 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 output stream.

Usage

From source file:Main.java

public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    while (true) {
        int read = in.read(buffer);
        if (read != -1) {
            out.write(buffer, 0, read);
        } else {/*from w w w .  j av  a  2s .co  m*/
            return;
        }
    }
}

From source file:Main.java

public static int CopyFile(String fromFile, String toFile) {
    try {//from ww w. j a va 2  s .  com
        InputStream fosfrom = new FileInputStream(fromFile);
        OutputStream fosto = new FileOutputStream(toFile);
        byte bt[] = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();
        return 0;

    } catch (Exception ex) {
        return -1;
    }
}

From source file:Main.java

public static void copy(InputStream inputstream, OutputStream outputstream) throws IOException {
    byte abyte0[] = new byte[50000];
    do {/*from  w w  w .  ja va 2 s.c  o m*/
        int i = inputstream.read(abyte0);
        if (i > 0) {
            outputstream.write(abyte0, 0, i);
        } else {
            outputstream.close();
            return;
        }
    } while (true);
}

From source file:Main.java

private static void copyFile2(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;//from   ww w  . ja v a2  s  .  com
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:Main.java

public static void setupDatabase(Context context, String db_name) {
    ContextWrapper cw = new ContextWrapper(context);
    String db_path = cw.getDatabasePath(db_name).getPath();

    try {//from   w w w .j  a  v  a 2  s  .co m
        // Setup
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = context.getAssets().open(db_name);
        OutputStream myOutput = new FileOutputStream(db_path);

        // Write all the things.
        while ((length = myInput.read(buffer)) > 0)
            myOutput.write(buffer, 0, length);

        // Cleanup
        myOutput.close();
        myOutput.flush();
        myInput.close();
    } catch (IOException e) {
        // You done goofed.
        e.printStackTrace();
    }
}

From source file:Main.java

public static void copyfile(File source, File dest) {
    try {//from w ww.  j  a  v a2  s  . c om
        InputStream in = new FileInputStream(source);
        OutputStream out = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException e) {
        Log.e(TAG, e.getLocalizedMessage());
    } catch (IOException e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
}

From source file:StreamsUtils.java

public static void inputStream2OutputStream(InputStream stream, OutputStream out) throws IOException {
    int readedBytes;
    byte[] buf = new byte[1024];
    while ((readedBytes = stream.read(buf)) > 0) {
        out.write(buf, 0, readedBytes);
    }/*from   w w  w. ja v  a  2  s.  c om*/
    stream.close();
    out.close();
}

From source file:Main.java

private static void readFromAndWriteTo(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[EIGHT_KILO];
    int bytesRead = in.read(buf);
    while (bytesRead >= 0) {
        out.write(buf, 0, bytesRead);
        bytesRead = in.read(buf);//from  ww w  .j  a va 2  s . c o m
    }
}

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte buffer[] = new byte[1024];
    int len = -1;
    while ((len = is.read(buffer, 0, 1024)) != -1) {
        os.write(buffer, 0, len);
    }/*  w  w w.  j a v a  2 s.  c  om*/
}

From source file:Main.java

public static boolean copyStream(InputStream src, OutputStream dest) {
    byte[] buffer = new byte[COPYSTREAM_BUFFER_SIZE];

    try {/*from  w  ww  .j a v a2 s . co m*/
        int size;

        while ((size = src.read(buffer)) != -1) {
            dest.write(buffer, 0, size);
        }
    } catch (IOException e) {
        return false;
    }

    return true;
}