📜  Java Java类设置 2

📅  最后修改于: 2022-05-13 01:55:47.059000             🧑  作者: Mango

Java Java类设置 2

Java Java类设置 1

更多方法:

  • void write(byte[] buf) :写入一个字节数组。此方法将阻塞,直到实际写入字节。
    Syntax :public void write(byte[] buf)
               throws IOException
    Parameters:
    buf - the data to be written
    Throws:
    IOException 
    
  • void write(byte[] buf, int off, int len) :写入字节子数组。
    Syntax :public void write(byte[] buf,
             int off,
             int len)
               throws IOException
    Parameters:
    buf - the data to be written
    off - the start offset in the data
    len - the number of bytes that are written
    Throws:
    IOException
  • void write(int val) :写入一个字节。此方法将阻塞,直到实际写入字节。
    Syntax :public void write(int val)
               throws IOException
    Parameters:
    val - the byte to be written to the stream
    Throws:
    IOException 
  • void writeBoolean(boolean val) :写入一个布尔值。
    Syntax :public void writeBoolean(boolean val)
                      throws IOException
    Parameters:
    val - the boolean to be written
    Throws:
    IOException 
  • void writeByte(int val) :写入一个 8 位字节。
    Syntax :public void writeByte(int val)
                   throws IOException
    Parameters:
    val - the byte value to be written
    Throws:
    IOException
  • void writeBytes(String str) :将字符串写入字节序列。
    Syntax :public void writeBytes(String str)
                    throws IOException
    Parameters:
    str - the String of writeBytes to be written
    Throws:
    IOException
  • void writeChar(int val) :写入一个 16 位字符。
    Syntax :public void writeChar(int val)
                   throws IOException
    Parameters:
    val - the char value to be written
    Throws:
    IOException
  • void writeChars(String str) :将字符串写入字符序列。
    Syntax :public void writeChars(String str)
                    throws IOException
    Parameters:
    str - the String of chars to be written
    Throws:
    IOException
  • protected void writeClassDescriptor(ObjectStreamClass desc) :将指定的类描述符写入 ObjectOutputStream。类描述符用于标识写入流的对象的类。 ObjectOutputStream 的子类可以重写此方法以自定义将类描述符写入序列化流的方式。然后应该重写 ObjectInputStream 中的相应方法 readClassDescriptor 以从其自定义流表示中重构类描述符。默认情况下,此方法根据对象序列化规范中定义的格式写入类描述符。
    Syntax :protected void writeClassDescriptor(ObjectStreamClass desc)
                                 throws IOException
    Parameters:
    desc - class descriptor to write to the stream
    Throws:
    IOException
  • void writeDouble(double val) :写入 64 位双精度。
    Syntax :public void writeDouble(double val)
                     throws IOException
    Parameters:
    val - the double value to be written
    Throws:
    IOException
  • void writeFields() :将缓冲字段写入流。
    Syntax :public void writeFields()
                     throws IOException
    Throws:
    IOException
    NotActiveException
  • void writeFloat(float val) :写入一个 32 位浮点数。
    Syntax :public void writeFloat(float val)
                    throws IOException
    Parameters:
    val - the float value to be written
    Throws:
    IOException 
  • void writeInt(int val) :写入一个 32 位整数。
    Syntax :public void writeInt(int val)
                  throws IOException
    Parameters:
    val - the integer value to be written
    Throws:
    IOException
    
  • void writeLong(long val) :写入 64 位长。
    Syntax :public void writeLong(long val)
                   throws IOException
    Parameters:
    val - the long value to be written
    Throws:
    IOException 
  • void writeObject(Object obj) :将指定的对象写入ObjectOutputStream。写入对象的类、类的签名、类及其所有超类型的非瞬态和非静态字段的值.可以使用 writeObject 和 readObject 方法覆盖类的默认序列化。此对象引用的对象是可传递的,因此可以通过 ObjectInputStream 重建一个完全等效的对象图。
    Syntax :public final void writeObject(Object obj)
                           throws IOException
    Parameters:
    obj - the object to be written
    Throws:
    InvalidClassException 
    NotSerializableException 
    IOException
  • protected void writeObjectOverride(Object obj) :子类用来覆盖默认 writeObject 方法的方法。此方法由使用受保护的无参数构造函数构造 ObjectInputStream 的 ObjectInputStream 的受信任子类调用。子类应提供带有修饰符“final”的覆盖方法。
    Syntax :protected void writeObjectOverride(Object obj)
                                throws IOException
    Parameters:
    obj - object to be written to the underlying stream
    Throws:
    IOException 
  • void writeShort(int val) :写一个 16 位短。
    Syntax :public void writeShort(int val)
                    throws IOException
    Parameters:
    val - the short value to be written
    Throws:
    IOException
  • protected void writeStreamHeader() :提供了 writeStreamHeader 方法,因此子类可以将自己的标头附加或前置到流中。它将幻数和版本写入流。
    Syntax :protected void writeStreamHeader()
                              throws IOException
    Throws:
    IOException
  • void writeUnshared(Object obj) :将“未共享”对象写入 ObjectOutputStream。此方法与 writeObject 相同,不同之处在于它始终将给定对象作为流中新的唯一对象写入(与指向先前序列化实例的反向引用相反)。具体来说:
    • 通过 writeUnshared 写入的对象始终以与新出现的对象(尚未写入流的对象)相同的方式序列化,无论该对象之前是否已写入。
    • 如果 writeObject 用于写入之前已使用 writeUnshared 写入的对象,则之前的 writeUnshared 操作将被视为单独对象的写入。换句话说,ObjectOutputStream 永远不会生成对通过调用 writeUnshared 写入的对象数据的反向引用。

    虽然通过 writeUnshared 写入对象本身并不能保证在反序列化时对该对象的唯一引用,但它允许在流中多次定义单个对象,因此接收方对 readUnshared 的多次调用不会发生冲突。请注意,上述规则仅适用于使用 writeUnshared 编写的基础级对象,不适用于要序列化的对象图中的任何可传递引用的子对象。
    覆盖此方法的 ObjectOutputStream 子类只能在拥有“enableSubclassImplementation”SerializablePermission 的安全上下文中构建;任何尝试在没有此权限的情况下实例化此类子类都将导致抛出 SecurityException。

    Syntax :public void writeUnshared(Object obj)
                       throws IOException
    Parameters:
    obj - object to write to stream
    Throws:
    NotSerializableException
    InvalidClassException
    IOException 
  • void writeUTF(String str) :以修改后的 UTF-8 格式写入此字符串的原始数据。请注意,将字符串作为原始数据或作为对象写入流中存在显着差异。 writeObject 写入的 String 实例最初作为 String 写入流中。未来的 writeObject() 调用将字符串的引用写入流中。
    Syntax :public void writeUTF(String str)
                  throws IOException
    Parameters:
    str - the String to be written
    Throws:
    IOException
  • void flush() :刷新流。这将写入任何缓冲的输出字节并刷新到底层流。
    Syntax :public void flush()
               throws IOException
    Throws:
    IOException

程序 :

//Java program demonstrating ObjectOutputStream
//write methods
import java.io.*;
class ObjectOutputStreamDemo
{
    public static void main(String[] args) throws IOException, ClassNotFoundException 
    {
        FileOutputStream fout = new FileOutputStream("file.txt");
        ObjectOutputStream oot = new ObjectOutputStream(fout);
          
        String a = "GeeksforGeeks";
        String b = "Geek";
        byte[] be = {'A','B','C'};
  
        //illustrating write()
        oot.write(1);
  
        //illustrating writeInt(int i)
        oot.writeInt(1);
  
        //illustrating writeBoolean(boolean a)
        oot.writeBoolean(true);
  
        //illustrating writeObject(Object x)
        oot.writeObject(a);
  
        //illustrating writeByte(byte a)
        oot.writeByte(65);
  
        //illustrating writeBytes(String b)
        oot.writeBytes(b);
  
        //illustrating writeDouble(double d)
        oot.writeDouble(2.3);
  
        //illustrating writeUTF(String str)
        oot.writeUTF(a);
  
        //illustrating writeFloat(float x)
        oot.writeFloat(2.42f);
  
        //illustrating writeLone(long x)
        oot.writeLong(234342347908l);
  
        //illustrating writeChars(String a)
        oot.writeChars(a);
  
        //illustrating writeShort(int val)
        oot.writeShort(2);
  
        //illustrating write(byte[] buff)
        oot.write(be);
          
        //flushing the stream
        oot.flush();
          
        oot.close();
          
        byte c[]=new byte[4];
        char c1[]=new char[13];
          
        FileInputStream fin = new FileInputStream("file.txt");
        ObjectInputStream oit = new ObjectInputStream(fin);
          
        System.out.println(oit.read());
        System.out.println(oit.readInt());
        System.out.println(oit.readBoolean());
        System.out.println(oit.readObject());
        System.out.println(oit.readByte());
        oit.read(c);
          
        for (int i = 0; i  < 4 ; i++) 
        {
            System.out.print((char)c[i]);
        }
          
        System.out.println();
        System.out.println(oit.readDouble());
        System.out.println(oit.readUTF());
        System.out.println(oit.readFloat());
        System.out.println(oit.readLong());
          
        for (int i = 0; i < 13 ; i++)
        {
            System.out.print(oit.readChar());
        }
          
        System.out.println();
        System.out.println(oit.readShort());
        oit.readFully(be);
          
        for (int i = 0; i < 3 ; i++) 
        {
            System.out.print((char)be[i]);
        }
        oit.close();
    }
}

输出 :

1
1
true
GeeksforGeeks
65
Geek
2.3
GeeksforGeeks
2.42
234342347908
GeeksforGeeks
2
ABC

方案二:

//Java program illustrating ObjectOutputStream
//write methods
import java.io.*;
class ObjectOutputStreamDemo
{
    public static void main(String[] args) throws IOException,
            ClassNotFoundException
    {
            FileOutputStream out = new FileOutputStream("file.txt");
            ObjectOutputStream oout = new ObjectOutputStream(out);
            oout.writeObject(new demo());
              
            //illustrating writeUnshared()
            //Writes an "unshared" object to the ObjectOutputStream.
            oout.writeUnshared(14);
              
            //flush the stream
            oout.flush();
              
            oout.close();
  
            FileInputStream fin=new FileInputStream("file.txt");
            ObjectInputStream ois=new ObjectInputStream(fin);
  
            // read an object from the stream and cast it to demo
            demo obj = (demo)ois.readObject();
              
            System.out.println( obj.var);
            System.out.println(ois.readUnshared());
    }
}
class demo implements Serializable
{
    static int var = 25;
          
    // assign a new serialPersistentFields
    private static final ObjectStreamField[] serialPersistentFields = 
    {
        new ObjectStreamField("var", Integer.TYPE)
    };
  
    private void readObject(ObjectInputStream in)
    throws IOException, ClassNotFoundException
    {
        // makes them available by name.
        ObjectInputStream.GetField fields = in.readFields();
              
        //Get the value of the named int field from the persistent field.
        var = fields.get("var", 0);
    }
  
    private void writeObject(ObjectOutputStream out)
                throws IOException
    {
        // write into the ObjectStreamField array the variable string
        ObjectOutputStream.PutField fields = out.putFields();
        fields.put("var", var);
              
        //Write the buffered fields to the stream
        out.writeFields();
  
    }
}

输出 :

25
14