package New_IO;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Test5 {
/**
* 数据流,专门用来做基本数据类型的读写的
* @param args
*/
public static void main(String[] args) {
try {
// Test5.testDataOutputStream(“F:\Java_project_new\src\New_IO\tt6.txt”);
Test5.testDataInputStream(“F:\Java_project_new\src\New_IO\tt6.txt”);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 数据输出流
* 用数据输出流写到文件的中的基本数据类型的数据,是乱码的,不能直接辨认出来 需要数据输入流来读取
*/
public static void testDataOutputStream(String output) throws Exception{
DataOutputStream out = new DataOutputStream(new FileOutputStream(output));
// out.writeBoolean(true);
out.writeDouble(1.44d);
out.flush();
out.close();
}
/**
*数据输入流
* 用数据输出流写到文件的中的基本数据类型的数据,是乱码的,不能直接辨认出来,需要数据输入流来读取
* 用数据输入流读取数据输出流写到文件中的数据时,要保证使用和当时写的数据类型一致的类型来读取
* 也就是说,如果写的时候是writeDouble,读的时候就得是readDouble
*
*/
public static void testDataInputStream(String output)throws Exception{
DataInputStream in = new DataInputStream(new FileInputStream(output));
System.out.println(in.readDouble());
in.close();
}
}
本文提供了一个Java程序示例,展示了如何使用DataOutputStream将基本数据类型写入文件,以及如何使用DataInputStream从文件中读取这些数据。通过具体代码说明了数据的正确读写方式。
1844

被折叠的 条评论
为什么被折叠?



