//读取内容,然后写入到一个文件中
public class Test07 { public static void main(String[] args) { try { //实例化一个文件读取器对象 FileReader fr = new FileReader("c://test.txt"); //包装成带缓存的读取器 BufferedReader br = new BufferedReader(fr); //一行一行的读取文本信息,打印输出到控制台 String t = br.readLine(); while (t!=null){ System.out.println(t); t = br.readLine(); } } catch (Exception ex) { ex.printStackTrace(); } } } |
//在文件中写入内容
public class Test08 { public static void main(String[] args) { try { //实例化一个字符文件写入器 FileWriter fw = new FileWriter("c://test.txt"); //包装成一个带缓存的高级流 PrintWriter pw = new PrintWriter(fw); pw.println("testtest......"); //刷新缓存,把缓存中的数据写入目的文件中 pw.flush(); } catch (Exception ex) { ex.printStackTrace(); } } } |
//文件拷贝
public class Test09 { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("c://apple.bmp"); FileOutputStream fos = new FileOutputStream("c://apple2.bmp"); /* int size = fis.available(); byte [] buf = new byte[size]; fis.read(buf); //从文件中读取信息,保存在buf中 fos.write(buf); //把buf中的数据写入到另一个文件中 */ byte [] buf = new byte[2048]; int count; while ( (count = fis.read(buf)) >0 ){ System.out.println(count); fos.write(buf,0,count); } } catch (Exception ex) { } } } |
//字节流转换为字符流,写入和读取
public class Test02 { public static void main(String[] args) throws Exception{ FileOutputStream fos = new FileOutputStream("C://objectFile.obj");//字节文件输出流 //包装成对象输出流,用来把内存中的数据写入到文件中 ObjectOutputStream oos = new ObjectOutputStream(fos); String obj1 = "hello"; //字符串对象 Date obj2 = new Date(); //日期型的对象,默认构造生成的是当前的系统日期
//把对象的数据,写入目标文件 oos.writeObject(obj1); oos.writeObject(obj2); oos.close(); //关闭流
FileInputStream fis = new FileInputStream("C://objectFile.obj");//字节文件输入流
//包装成对象输入流,用来把文件中的数据读到内存的对象中 ObjectInputStream ois = new ObjectInputStream(fis); String obj11 = (String)ois.readObject(); System.out.println("ojb11:" + obj11); System.out.println("obj11==obj1:" + (obj11 == obj1) ); //false System.out.println("obj11.equals(obj1):" + obj11.equals(obj1)); //true
Date obj22 = (Date)ois.readObject(); System.out.println("ojb22:" + obj22); System.out.println("obj22==obj2:" + (obj22 == obj2) ); //false System.out.println("obj22.equals(obj2):" + obj22.equals(obj2)); //true ois.close(); } } |
//序列化:把内存中的对象,以字节方式保存在文件或者网络 //反序列化:把文件或者网络的字节,还原成内存中的对象 //客户类 class Customer implements Serializable{ private String name ; //姓名 private int age; //年龄 //构造函数,给数据成员赋初值 public Customer(String name, int age){ this.name = name; this.age = age; } //覆盖Object中的方法 public boolean equals(Object o){ if (this==o) return true; if (!(o instanceof Customer)) return false; final Customer other = (Customer)o; if (this.name.equals(other.name) && this.age == other.age){ return true; }else { return false; } } public String toString(){ return "name = " + name + ", age=" + age; } } public class Test03 { public static void main(String[] args) throws Exception { Customer obj3 = new Customer("Tom", 20); FileOutputStream fos = new FileOutputStream("C://objectFile.obj"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(obj3); oos.close();
FileInputStream fis = new FileInputStream("C://objectFile.obj"); ObjectInputStream ois = new ObjectInputStream(fis); Customer obj33 = (Customer) ois.readObject(); System.out.println("ojb33:" + obj33); System.out.println("obj33==obj3:" + (obj33 == obj3)); System.out.println("obj11.equals(obj1):" + obj33.equals(obj3)); } } |