天天看点

Java DataInputStream readFloat()方法与示例

DataInputStream类readFloat()方法 (DataInputStream Class readFloat() method)

  • readFloat() method is available in java.io package. readFloat()方法 在java.io包中可用。
  • readFloat() method is used to read 4 bytes (i.e. 32 bit) of float value of data input and returns a float value read. readFloat()方法 用于读取4个字节(即32位)的数据输入的float值,并返回读取的float值。
  • readFloat() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error. readFloat()方法 是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
  • readFloat() method may throw an exception at the time of reading float. readFloat()方法 在读取float时可能会引发异常。
    • IOException : This exception may throw when this stream is not opened. IOException :如果未打开此流,则可能引发此异常。
    • EndOfFileException : This exception may throw when this stream has reached its endpoint. EndOfFileException :当此流到达其端点时,可能引发此异常。
Syntax: 句法:
public final float readFloat();
           
Parameter(s): 参数:
  • It does not accept any parameter.

    它不接受任何参数。

Return value: 返回值:

The return type of the method is float, it returns 4 bytes of data input, manipulated as float.

方法的返回类型是float ,它返回4个字节的数据输入,作为float进行操作。

Example: 例:
// Java program to demonstrate the example 
// of float readFloat() method of 
// DataInputStream

import java.io.*;

public class ReadFloatOfDIS {
 public static void main(String[] args) throws IOException {
  InputStream is_stm = null;
  DataInputStream dis_stm = null;
  FileOutputStream fos_stm = null;
  DataOutputStream dos_stm = null;
  float[] f_arr = {
   97.0f,
   98.0f,
   99.0f,
   100.0f,
   101.0f
  };

  try {
   // Instantiate FileInputStream, 
   // DataInputStream, FileOutputStream
   // and DataOutputStream 

   fos_stm = new FileOutputStream("C:\\Users\\Preeti Jain\\Desktop\\programs\\includehelp.txt");
   dos_stm = new DataOutputStream(fos_stm);

   // Loop to write each float till end
   for (float val: f_arr) {

    // By using writeFloat() method isto
    // write a float to the
    // DataOutputStream dos_stm

    dos_stm.writeFloat(val);
   }


   is_stm = new FileInputStream("C:\\Users\\Preeti Jain\\Desktop\\programs\\includehelp.txt");
   dis_stm = new DataInputStream(is_stm);

   // Loop To Read Available Data till end
   while (dis_stm.available() > 0) {

    // By using readFloat() method isto read 
    // float at a time

    float fl = dis_stm.readFloat();
    System.out.println("dis_stm.readFloat(): " + fl);
   }
  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   // To free system resources linked
   // with these streams
   if (is_stm != null)
    is_stm.close();

   if (dis_stm != null)
    dis_stm.close();

   if (dos_stm != null)
    dos_stm.close();

   if (fos_stm != null)
    fos_stm.close();
  }
 }
}
           
Output 输出量
dis_stm.readFloat(): 97.0
dis_stm.readFloat(): 98.0
dis_stm.readFloat(): 99.0
dis_stm.readFloat(): 100.0
dis_stm.readFloat(): 101.0
           
翻译自: https://www.includehelp.com/java/datainputstream-readfloat-method-with-example.aspx