Write float to a file in Java

Description

The following code shows how to write float to a file.

Example


//from w  w  w .j  a v a  2  s  .  c om
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Main {

  public static void main(String[] args) throws Exception{
    int idA = 1;
    String nameA = "City";
    int populationA = 5;
    float tempA = 1.0f;

    int idB = 2;
    String nameB = "S";
    int populationB = 2;
    float tempB = 1.45f;

    FileOutputStream fos = new FileOutputStream("cities.dat");
    DataOutputStream dos = new DataOutputStream(fos);

    dos.writeInt(idA);
    dos.writeUTF(nameA);
    dos.writeInt(populationA);
    dos.writeFloat(tempA);

    dos.writeInt(idB);
    dos.writeUTF(nameB);
    dos.writeInt(populationB);
    dos.writeFloat(tempB);

    dos.flush();
    dos.close();

    FileInputStream fis = new FileInputStream("cities.dat");
    DataInputStream dis = new DataInputStream(fis);

    int cityId = dis.readInt();
    System.out.println("City Id: " + cityId);
    String cityName = dis.readUTF();
    System.out.println("City Name: " + cityName);
    int cityPopulation = dis.readInt();
    System.out.println("City Population: " + cityPopulation);
    float cityTemperature = dis.readFloat();
    System.out.println("City Temperature: " + cityTemperature);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip