总结: 主要是 捕获异常
package com.a.b; import java.io.*; public class testData { public static void main(String[] args) throws IOException { File newDir = new File("c:\javas"); if (!newDir.exists()) { newDir.mkdir();// 如果目录不存在,就创建目录 File newFile = new File(newDir, "test.txt"); try { newFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }// 创建文件对象 // 建立DataInputStream 流 DataOutputStream dos = null; try { dos = new DataOutputStream(new FileOutputStream(newFile)); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { dos.writeInt(10); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }// 写入整型数据m dos.writeBoolean(true);// 写入boolean型数据 dos.writeFloat(23.2f);// 写入浮点型double型 dos.writeChars("writeen into file");// 写入字符串 dos.close(); System.out.println("LIST ths file content"); // 建立DataInputStream流 DataInputStream dis = new DataInputStream(new FileInputStream( newFile)); System.out.println(dis.readChar()); System.out.println(dis.readInt()); System.out.println(dis.readBoolean()); char c; while ((c = dis.readChar()) != -1) { System.out.println(c);// 读取字符串 dis.close(); } } } }