How to write byte array to file using BufferedOutputStream()

2 Answers

0 votes
package javaapplication1;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) {

        String sFileName = "d://data.txt";

        try {
            BufferedOutputStream bos;
            try (FileOutputStream fos = new FileOutputStream(new File(sFileName))) {
                bos = new BufferedOutputStream(fos);
                String s = "Java Programming";
                bos.write(s.getBytes());
                bos.flush();
            }

        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
}

// in file: Java Programming
/*
      
run:


  
 */

 



answered Nov 12, 2016 by avibootz
edited Nov 12, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) {

        String sFileName = "d://data.txt";
        byte[] bArr = {97, 98, 99, 100, 101};

        try {
            DataOutputStream dos;
            try (FileOutputStream fos = new FileOutputStream(new File(sFileName))) {
                dos = new DataOutputStream(fos);
                for (byte b : bArr) {
                    dos.writeChar(b);
                }
                dos.flush();
            }
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
}

// in file:  a b c d e

/*
      
run:


  
 */

 



answered Nov 12, 2016 by avibootz

Related questions

1 answer 195 views
1 answer 206 views
1 answer 190 views
1 answer 235 views
235 views asked Mar 17, 2015 by avibootz
1 answer 153 views
...