输入输出流的五种方式

如果想要实现输出流的换行,应该用"\r\n",只用"\n"是实现不了的。

1、FileInputStream和FileOutputStream

1 FileOutputStream fos = new FileOutputStream("D:\\输入输出流.txt");
2 String word = "输入输出流";
3 byte [] bytes = word.getBytes();
4 fos.write(bytes);
5 System.out.println("写入成功");
6 fos.close();

 

 1 FileInputStream fis = new FileInputStream("D:\\输入输出流.txt");
 2 byte [] bytes = new byte[1024];
 3 int data;
 4 while((data=fis.read(bytes))!=-1){
 5       String str = new String(bytes,0,data);
 6       System.out.print(str);  
 7 }
 8 fis.close();
 9 /*
10      int data = fis.read(bytes);
11      while(data!=-1){
12            String str = new String(bytes,0,data);
13            System.out.println(str);
14            data = fis.read(bytes);    
15      }
16 */
  

 

2、FileReader和FileWriter

1 FileReader fr = new FileReader("E:\\输入输出流.txt");
2 char [] chars = new char[1024];
3 int data;
4 while((data=fr.read(chars))!=-1){
5        String str = new String(chars,0,data);
6        System.out.println(str);
7 }
8 fr.close();
1 FileWriter fw = new FileWriter("E:\\输入输出流.txt");
2 String  word = "输入输出流";
3 fw.write(word);
4 System.out.println("写入成功");
5 fw.close();

3、BufferedReader和BufferedWriter

Reader fr = new FileReader("D:\\输入输出流.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while((line=br.readLine())!=null){
       System.out.println(line);
}
br.close();
fr.close();
Writer writer = new FileWriter("D:\\输入输出流.txt");
BufferedWriter bw = new BufferedWriter(writer);
String word = "输入输出流";
bw.write(word);
System.out.println("写入成功");
bw.close();
writer.close();

4、DataInputStream和DataOutputStream

InputStream is = new FileInputStream("D:\\输入输出流");
DataInputStream os = new DataInputStream(is);
OutputStream os = new FileOutputStream("E:\\输入输出流");
DataOutputStream dos = new DataOutputStream(os);
int data;
byte [] bytes = new byte[1024];
while((data = dis.read(bytes))!=-1) {
       dos.write(bytes,0,data);
}

 5、序列化与反序列化

例:使用序列化将学生对象保存到文件中,实现步骤如下:

  ① 创建学生类,实现Serializable接口

  ② 引入相关类

  ③ 创建对象输出流

  ④ 调用writeObject()方法将对象写入文件

  ⑤ 关闭对象输出流

public class Student implements java.io.Serializable{
      private String name; // 学生姓名
      private int age; // 学生年龄
      private String gender; // 学生性别
   
      /**
         *无参构造
         */
      public Student(){
      }
      /**
         * 有参构造
         */
       public Student(String name,int age,String gender){
                 this.name = name;
                 this.age = age;
                 this.gender = gender;
       }  
       public String getName(){
                 return this.name;
       }
       public int getAge(){
               return this.age;
       }
       public String getGender(){
                return this.gender;
       }
}
public class SerializableObj {
    public static void main(String[] args) throws Exception {
        // 创建Object输出流
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\1.txt"));
        Student stu1 = new Student("安娜", 18, "女");
        Student stu2 = new Student("李卫", 20, "男");
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(stu1);
        list.add(stu2);
        // 对象序列化,写入输出流
        oos.writeObject(list);
        // 关流
        oos.close();

    }
}

使用但序列化读取文件中的学生对象

实现步骤:

     ① 引入相关类

     ② 创建对象输出流

     ③ 调用readObject()方法读取对象。

     ④ 关闭对象输出流

public static void main(String[] args) throws Exception {
        // 创建Object输入流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\1.txt"));
        // 反序列化,强制转换类型
        ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
        // 输出生成后的对象信息
        for (Student stu : list) {
            System.out.println("姓名为:" + stu.getName());
            System.out.println("年龄为:" + stu.getAge());
            System.out.println("性别为:" + stu.getGender());
        }
        ois.close();
    }

 

posted @ 2017-05-11 18:02  AlexanderTheGreat  阅读(1158)  评论(0编辑  收藏  举报