How to write and read byte array from file using DataOutputStream() and DataInputStream() in Java

3 Answers

0 votes
package javaapplication1;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        String sFilePath = "d://data.txt";
        byte[] bArr = {65, 66, 67, 68, 69, 70, 71};

        try {

            DataOutputStream dos;
            try (FileOutputStream fos = new FileOutputStream(sFilePath)) {
                dos = new DataOutputStream(fos);
                for (byte b : bArr) {
                    dos.writeChar(b);
                }   dos.flush();
            }

            try (FileInputStream fis = new FileInputStream(sFilePath);
                 DataInputStream dis = new DataInputStream(fis)) {
                
                while (dis.available() > 0) {
                    char ch = dis.readChar();
                    System.out.print(ch + " ");
                }
            }
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }
}

/*
      
run:

A B C D E F G
  
*/

 



answered Nov 10, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) {

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

        try {

            try (FileOutputStream fos = new FileOutputStream(sFilePath)) {
                String s = "Java Programming";
                fos.write(s.getBytes());
            }
            try (FileInputStream fis = new FileInputStream(sFilePath);
                 DataInputStream dis = new DataInputStream(fis)) {
                 
                while (dis.available() > 0) {
                    byte b = dis.readByte();
                    System.out.print(b + " ");
                }
            }

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

/*
      
run:

74 97 118 97 32 80 114 111 103 114 97 109 109 105 110 103
  
 */

 



answered Nov 14, 2016 by avibootz
0 votes
package javaapplication1;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class JavaApplication1 {
 
    public static void main(String[] args) throws IOException {
 
        String sFilePath = "d://data.txt";
        byte[] bArr = {65, 66, 67, 68, 69, 70, 71};
 
        try {
 
            DataOutputStream dos;
            try (FileOutputStream fos = new FileOutputStream(sFilePath)) {
                dos = new DataOutputStream(fos);
                for (byte b : bArr) {
                    dos.writeByte(b);
                }   dos.flush();
            }
 
            try (FileInputStream fis = new FileInputStream(sFilePath);
                 DataInputStream dis = new DataInputStream(fis)) {
                 
                while (dis.available() > 0) {
                    byte b = dis.readByte();
                    System.out.print(b + " ");
                }
            }
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }
}
 
/*
       
run:
 
65 66 67 68 69 70 71
   
*/

 



answered Nov 14, 2016 by avibootz
...