将基本类型数据写入到文件中,接着读取出来
package com.machuang.io.others; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataStream { public static void main(String[] args) throws IOException { String path = "F:/win10/test/data.txt"; writeBasicTypes(path); // 与要读取的文件建立联系 DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream(path) ) ); // 读取操作 double recDou = dis.readDouble(); long recL = dis.readLong(); String recS = dis.readUTF(); System.out.println("recDou:\t" + recDou + "\n" + "recL:\t" + recL + "\n" + "recS:\t" + recS); dis.close(); } public static void writeBasicTypes(String desPath) throws IOException { // 要写出的数据 double dataDou = 3.6; long numL = 100L; String dataStr = "architecture"; // 关联写出的文件 DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(desPath)) ); dos.writeDouble(dataDou); dos.writeLong(numL); dos.writeUTF(dataStr); dos.flush(); dos.close(); } }
运行结果如下
原文地址:https://www.cnblogs.com/cappuccinom/p/8822961.html
时间: 2024-11-14 02:59:49