📜  Java Java类设置 2

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

Java Java类设置 2

Java Java类设置 1

更多方法:

  • byte readByte() :读取并返回一个输入字节。
    Syntax:public final byte readByte()
                        throws IOException
    Returns: the next byte of this input stream as a signed 8-bit byte.
    Throws:
    EOFException
    IOException 
    
  • float readFloat() :读取四个输入字节并返回一个浮点值。
    Syntax:public final float readFloat()
                          throws IOException
    Returns :the next four bytes of this input stream, interpreted as a float.
    Throws:
    EOFException 
    IOException
  • void readFully(byte[] b) :从输入流中读取一些字节并将它们存储到缓冲区数组 b 中。
    Syntax:public final void readFully(byte[] b)
                         throws IOException
    Parameters:
    b - the buffer into which the data is read.
    Throws:
    EOFException
    IOException
  • void readFully(byte[] b, int off, int len) :从输入流中读取 len 个字节。
    Syntax:public final void readFully(byte[] b,
                 int off,
                 int len)
                         throws IOException
    Parameters:
    b - the buffer into which the data is read.
    off - the start offset of the data.
    len - the number of bytes to read.
    Throws:
    EOFException 
    IOException
  • String readLine() :从输入流中读取下一行文本。
    Syntax:
    Returns: the next line of text from this input stream.
    Throws:
    IOException
    Deprecated This method does not properly convert bytes to characters.
    
  • long readLong() :读取八个输入字节并返回一个 long 值。
    Syntax:public final long readLong()
                        throws IOException
    Returns:
    the next eight bytes of this input stream, interpreted as a long.
    Throws:
    EOFException
    IOException
  • short readShort() :读取两个输入字节并返回一个短值。
    Syntax:public final short readShort()
                          throws IOException
    Returns:
    the next two bytes of this input stream, interpreted as a signed 16-bit number.
    Throws:
    EOFException
    IOException
  • String readUTF() :读取已使用修改后的 UTF-8 格式编码的字符串。
    Syntax:public final String readUTF()
                         throws IOException
    Returns:
    a Unicode string.
    Throws:
    EOFException 
    IOException
    UTFDataFormatException 
    
  • int skipBytes(int n) :尝试从输入流中跳过 n 个字节的数据,丢弃跳过的字节。
    Syntax:public final int skipBytes(int n)
                        throws IOException
    Parameters:
    n - the number of bytes to be skipped.
    Returns:
    the actual number of bytes skipped.
    Throws:
    IOException

方案二:

//Java program to demonstrate DataInputStream
// This program uses try-with-resources. It requires JDK 7 or later.
import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
  
class DataInputStreamDemo
{
    public static void main(String args[]) throws IOException
    {
        //writing the data.
        try ( DataOutputStream dout =
                    new DataOutputStream(new FileOutputStream("file.dat")) )
        {
            dout.writeBytes("1");
            dout.writeFloat(4.4545f);
            dout.writeUTF("geeksforgeeks");
            dout.writeChars("GeeksforGeeks\n");
            dout.writeBytes("ABCDEFG");
  
        }
  
        catch(FileNotFoundException ex)
        {
            System.out.println("Cannot Open the Output File");
            return;
        }
  
        // reading the data back.
        try ( DataInputStream din =
                    new DataInputStream(new FileInputStream("file.dat")) )
        {
  
            //illustrating readByte() method
            byte t=din.readByte();
              
            //illustrating readFloat() method
            float u=din.readFloat();
              
            //illustrating readUTF() method
            String temp=din.readUTF();
              
            //illustrating readLine() method
            String temp1=din.readLine();
  
            System.out.println("Values: " + t +" "+" "+u+" "+temp+" "+temp1 );
  
            //illustrating skipBytes() method
            //skipping "AB"
            din.skipBytes(2);
  
            //illustrating readFully() method
            byte[] b=new byte[4];
            din.readFully(b,0,4);
            System.out.println(Arrays.toString(b));
  
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Cannot Open the Input File");
            return;
        }
    }
}

输出:

Values: 49  4.4545 geeksforgeeks  GeeksforGeeks 
[67, 68, 69, 70]