IO流
File
IO
字节流
FileOutputStream fos = new FileOutputStream("D\\b.txt"); //不存在,创建 存在,清空 fos.write(); fos.close(); FileInputStream fos = new FileInputStream("D\\b.txt"); fos.read(); fos.close();
缓冲流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test-io\\copy.txt")); BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
byte [] bytes = new byte[1024]; int len; while ((len = bis.read(bytes)) != -1) { bos.write(bytes, 0,len); } bos.close(); bis.close();
字符流
写出数据 FileWriter fw = new FileWriter("test-io\\a.txt");
字符缓冲流
BufferedWriter bw = new BufferedWriter(new FileWriter("test-io\\a.txt"));
BufferedReader bf = new BufferedReader(new FileReader("test-io\\a.txt"));
其他流
转换流
InputStreamReader isr = new InputStreamReader(new FileInputStream("test-io\\a.txt"),"gbk");
OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("test-io\\a.txt"),"gbk");jdk11后
FileReader fr = new FileReader("test-io\\a.txt", Charset.forName("gbk"));
FileWriter fw = new FileWriter("test-io\\a.txt",Charset.forName("utf-8"));
对象操作流
//实现接口Serializable,就可以被序列化 public class User implements Serializable{...}
// 写入对象流ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test-io\\b.txt"));
User user = new User("lisi", "itheima");
oos.writeObject(user);
oos.close();
// 读入对象流 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test-io\\b.txt")); User user = (User) ois.readObject(); System.out.println(user); ois.close();private static final serialVersionUID = 1L;
private String tansient password;
Properties
Properties prop = new Properties();
prop.put("张三", "李四"); // 删 prop.remove("jack"); // 修 prop.put("jack", "xiaowang"); // 查 Object o = prop.get("张三");
Properties prop = new Properties(); FileWriter fw = new FileWriter("test-io\\prop.properties"); prop.put("lisi","123"); prop.put("wangwu","xiaoliu"); prop.put("zhaoqi","admin"); prop.store(fw,null); fw.close();
Properties prop = new Properties(); FileReader fr = new FileReader("test-io\\prop.properties"); prop.load(fr); fr.close(); System.out.println(prop);