字节流 (Reader,Writer)

    输入流  FileReader

      

public class Demo {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("b.txt");
int len;
while((len=fr.read())!=-1){
System.out.print((char)len);
}
fr.close();
}
}

  

    输出流  FileWriter

public class Demo {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("b.txt");
//创建输出流对象
FileWriter fw=new FileWriter("a.txt");
char[] ch=new char[1024];
int len;
while((len=fr.read(ch))!=-1){//读数据
//写出数据
fw.write(ch,0,len);
}
//关闭流
fr.close();
fw.close();
}

  

   高效流    BufferedReader   BufferedWriter

public class Demo {
public static void main(String[] args) throws IOException {
//fileReaderDemo();
FileReader fr=new FileReader("b.txt");
          //创建高效输入字符流对象
BufferedReader br=new BufferedReader(fr);
//创建输出流对象
FileWriter fw=new FileWriter("a.txt");
BufferedWriter bw=new BufferedWriter(fw);
char[] ch=new char[1024];
String len;
while((len=br.readLine())!=null){//读数据
//写出数据
bw.write(len);
bw.newLine();
bw.flush();
}
//关闭流
br.close();
bw.close();
}
}

字符流  (InputStream,OutputStream)

    输入流  FileInputStream

      

public class Demo {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("a.txt");
int len;
while ((len=fis.read())!=-1) {
System.out.print((char)len);
}
fis.close();
}
}

    输出流 FileOutputStream

public class Demo {
public static void main(String[] args) throws IOException {
//fileReaderDemo();
//method01();
FileInputStream fis=new FileInputStream("b.txt");
FileOutputStream ios=new FileOutputStream("a.txt");
int len;
while ((len=fis.read())!=-1) {
ios.write(len);
}
fis.close();
ios.close();
}
}

  

    高效流

public class Demo {
public static void main(String[] args) throws IOException {
//fileReaderDemo();
//method01();
FileInputStream fis=new FileInputStream("b.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream ios=new FileOutputStream("a.txt");
BufferedOutputStream bos=new BufferedOutputStream(ios);
byte[] b=new byte[1024];
int len;
while ((len=bis.read(b))!=-1) {
bos.write(b,0,len);
bos.flush();
}
fis.close();
ios.close();
}
}

  

转换流

    输入流      InputStreamReader

    输出流      outputStreamWriter

public class Demo {
public static void main(String[] args) throws IOException {
//fileReaderDemo();
//method01();
FileInputStream fis=new FileInputStream("b.txt");
InputStreamReader isr=new InputStreamReader(fis);//转化为字符流 转换输入流
BufferedReader br=new BufferedReader(isr);//包装为高效流
FileOutputStream ios=new FileOutputStream("a.txt");//转化为字符流 转换输出流
OutputStreamWriter osw=new OutputStreamWriter(ios);//包装为高效流
BufferedWriter bw=new BufferedWriter(osw);
String len;
while ((len=br.readLine())!=null) {
bw.write(len);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}

  

标准流

    输入流

public class Demo {
public static void main(String[] args) throws IOException {
//标准输入流
InputStream in = System.in;
InputStreamReader fis=new InputStreamReader(in);
int len;
while ((len=fis.read())!=-1) {
System.out.print((char)len);
}
fis.close();
}
}

    输出流

public class Demo {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
InputStreamReader fis=new InputStreamReader(new FileInputStream("a.txt"));
OutputStreamWriter osw=new OutputStreamWriter(out);
int len;
while((len=fis.read())!=-1){
osw.write(len);//输出到控制台
}
fis.close();
osw.close();
}
}

输出流   (不能写出)

    输入流   PrintWriter(父类为Writer)

		PrintWriter pw=new PrintWriter("c.txt");
pw.println("hello");
pw.println("world");//将内容写入c.txt文件中 自带刷新功能
pw.close();

  

对象流   (序列化)

    输出流(将对象写入到文件中,这个对象必须序列化才能写入到文件中   否则会报错)

    输入流 ()

public class ObjectTest {
public static void main(String[] args) throws IOException, Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"d.txt"));
ArrayList<Student> array = new ArrayList<Student>();
Student s1 = new Student("赵云", 15);
Student s2 = new Student("张飞", 15);
array.add(s1);
array.add(s2);
oos.writeObject(array);//序列化过程
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(//反序列化
"d.txt"));
ArrayList<Student> arrays = (ArrayList<Student>) ois.readObject();
for (Student student : arrays) {
System.out.println(student);
}
}
}

  

public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3919560601713535195L;
// serialVersionUID 用来标识类的唯一性 它是根据类的内容计算出来的 如果写入和读出的类的id不一致 则会报错
String name;
int age;
String sex; public Student(String name,int age){
this.name=name;
this.age=age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
} }

  

错误异常
java.io.InvalidClassException
local class incompatible: 类不相容
stream classdesc serialVersionUID = -3919560601713535195, local class serialVersionUID = 5350889806824666248

  

解决方案:
给类进行唯一标识
让类自动生成 UID

  

最新文章

  1. Ogre 1.9 Android移植
  2. MySql 数据操作类
  3. php基础32:正则匹配-修饰符
  4. Spring MVC防御CSRF、XSS和SQL注入攻击
  5. Kafka Unknow host
  6. vm安装ubuntu桥接模式无法联网
  7. [转] 使用NVM快速搭建NODE开发环境
  8. 【HDOJ】1717 小数化分数2
  9. CGRect包含交错,边缘,中心的检测
  10. Egret 位图,纹理,添加背景 学习
  11. Tomcat安装和目录简介
  12. C#设计模式&mdash;&mdash;单例模式的实现
  13. 在CentOS7中安装scala-2.11.12
  14. mapreduce 学习笔记
  15. 对JavaScript垃圾回收机制的理解?
  16. MD5 SHA1 CRC32
  17. String StringBuffer和StringBuilder区别及性能
  18. hello world讲解1
  19. ovs 下流表port 1进入,port 1出去
  20. pta 习题集5-18 打印学生选课清单

热门文章

  1. Nginx 错误汇总
  2. 迷你MVVM框架 avalonjs 1.4发布
  3. 解决PL/SQL导出cvs文件中文显示乱码
  4. 输入N组父子对,求父子对所组成的二叉树的高度----17年某公司的笔试题
  5. iis配置asp.net常见验证失败问题解决方案
  6. select语法图
  7. 神龟快跑,2016做的一款UWP游戏
  8. Java的线程同步
  9. vmware搭建vSAN提示磁盘不合格或者看不到磁盘的解决办法
  10. sign和token设计