Java serialization 101 dataoutputstream, PrintStream method details

1, java.io.DataOutputStream; data byte output stream

1. The "int i = 2" in memory can be written into the hard disk file, not the string, but the binary data, which can be of type.

 

package com.bjpowernode.java_learning;

import java.io.*;

​

public class D101_1_DataOutputStream {

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

    //Create data byte output stream

    String address = "C:\\\\Users\\\\lenovo1\\\\Workspaces\\\\MyEclipse CI\\\\Java_learning\\\\src\\\\com\\\\bjpowernode\\\\java_learning\\\\temp1.txt";

    DataOutputStream dos = new DataOutputStream(new FileOutputStream(address));

    //Preparation data

    byte b = 10;

    short s = 11;

    int i = 12;

    long l = 13;

    float f = 3.2f;

    double d = 2.3;

    boolean flag = false;

    char c = 'a';

    //Write in

    dos.writeByte(b);

    dos.writeShort(s);

    dos.writeInt(i);

    dos.writeLong(l);

    dos.writeFloat(f);

    dos.writeDouble(d);

    dos.writeBoolean(flag);

    dos.writeChar(c);

    //Refresh

    dos.flush();

    //The content written in is binary file, we use text to open it is a pile of garbled code

   

    //Close

    dos.close();

   

    //Create input stream

    DataInputStream dis = new DataInputStream(new FileInputStream(address));

    //read

    //Note: to use this stream to read data, you must know the data storage format and order in advance.

    byte b1 = dis.readByte();

    short s1 = dis.readShort();

    int i1 = dis.readInt();

    long l1 = dis.readLong();

    float f1 = dis.readFloat();

    double d1 = dis.readDouble();

    boolean flag1 = dis.readBoolean();

    char c1 = dis.readChar();

   

   

    System.out.println(b1);

    System.out.println(s1);

    System.out.println(i1);

    System.out.println(l1);

    System.out.println(f1);

    System.out.println(d1);

    System.out.println(flag1);

    System.out.println(c1);

    //Purpose: it's a bit like encrypted files, which are often seen in the telecom industry. We transmit data, and then inform the other party of our format and data order, so that the other party can analyze them

    //Close

    dis.close();

   

   

  }

​

}

​

2, PrintStream\PrintWriter

1.java.io.PrintStream; the standard output stream, which is printed to the console by default, in byte mode.

 

package com.bjpowernode.java_learning;

import java.io.*;

import java.util.Date;

import java.text.SimpleDateFormat;

​

public class D101_2_PrintStream {

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

    System.out.println("HelloWorld");

   

    PrintStream ps = System.out;

    ps.println("JAVA");

   

    String address = "C:\\\\Users\\\\lenovo1\\\\Workspaces\\\\MyEclipse CI\\\\Java_learning\\\\src\\\\com\\\\bjpowernode\\\\java_learning\\\\temp1.txt";

   

    //setOut Method can change the output direction, PrintStream(FileOutputStream f)Byte output stream acceptable

   

    System.setOut(new PrintStream(new FileOutputStream(address)));

    //Re export

    System.out.print("HAHA");

    //The above methods are usually used to record logs

   

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

    System.out.println(sdf.format(new Date()));

    m1();

    System.out.println(sdf.format(new Date()));

   

   

  }

  public static void m1() {

    System.out.println("m1 method execute");

  }

​

}

 

3, Source code:

D101_1_DataOutputStream.java

D101_2_PrintStream.java

https://github.com/ruigege66/Java/blob/master/D101_1_DataOutputStream.java

https://github.com/ruigege66/Java/blob/master/D101_2_PrintStream.java

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, "gift package", get big data learning materials.

 

Tags: Java MyEclipse github Big Data

Posted on Mon, 23 Mar 2020 10:51:14 -0400 by kcgame