Categories
Uncategorized

java foundation: 12. A DataInputStream stream 6,

output data stream

The use of the data stream () and () may perform data reading and writing of the formatted sequence.

Note: When the vehicle A DataInputStream reads a file, this file must be written, that would otherwise occur,Because in write e – mail while a special mark, only A DataInputStream can successfully read.

package stream;
  
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
  
public class Main {
    
    public static void main(String[] args) {
        write();
        read();
    }
 
    private static void read() {
        File f =new File("f:/testfile/test.txt");
        try (
                FileInputStream fis  = new FileInputStream(f);
                DataInputStream dis =new DataInputStream(fis);
        ){
            boolean b= dis.readBoolean();
            int i = dis.readInt();
            String str = dis.readUTF();
             
           
            System.out.println(

"to the integer read:"

+i); System.out.println(

"to the read character string:"

+str); System.out.println(

boolean "to read:"

+b); } catch (IOException e) { e.printStackTrace(); } } private static void write() { File f =new File("f:/testfile/test.txt"); try ( FileOutputStream fos = new FileOutputStream(f); DataOutputStream dos =new DataOutputStream(fos); ){ dos.writeBoolean(true); dos.writeInt(300); dos.writeUTF("123 this is gareen"); } catch (IOException e) { e.printStackTrace(); } } }

Leave a Reply