Java-数据流
数据流,提供了可以读/写任意数据类型的方法:
DataOutputStream: 提供了 writeXxx(xxx value)方法.
DataInputStream: 提供了 readXxx()方法.
注意: writeXxx和readXxx必须要对应起来, writeByte写出的数据,此时只能使用readByte读取回来.
//数据流 public class DataStreamDemo { public static void main(String[] args) throws Exception { File f = new File("C:/IOtest/data/data.txt"); //write(f); read(f); } private static void read(File f) throws Exception { DataInputStream in = new DataInputStream(new FileInputStream(f)); System.out.println(in.readByte()); System.out.println(in.readChar());//只能用相对应的方式来读取 System.out.println(in.readUTF()); //System.out.println(in.readInt()); in.close(); } private static void write(File f) throws Exception { DataOutputStream out = new DataOutputStream(new FileOutputStream(f)); out.writeByte(66);// byte out.writeChar('穷');// char out.writeUTF("天王盖地虎!");// string out.close(); } }
共有 0 条评论