IO流的应用

public class IODemo {
    //编写对象序列化算法
    public static void writeObj(File file, Object object) {
        try {
            ObjectOutputStream objectOutputStream =
                    new ObjectOutputStream(new FileOutputStream(file));
            objectOutputStream.writeObject(object);
            objectOutputStream.flush();
            System.out.println("写入完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //反序列化编程
    public static void readObj(File file) {
        ObjectInputStream objectInputStream =
                null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(new File("./document/obj.txt")));
            System.out.println(objectInputStream.readObject());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    //编写字节流读写文件
    public static void binaryWrite(File file){
               try {
            //TimeUnit.SECONDS.sleep(5);
                   FileWriter fw = new FileWriter(file);
                   fw.write("");
                   FileOutputStream fos = new FileOutputStream(file,true);
            Scanner sc = new Scanner(System.in);
            while(true){
                System.out.println("开始编辑文本...");
                String s = sc.nextLine();
                if((!"".equals(s))&&"quit".equals(s)){
                    break;
                }
                byte[] bytes = s.getBytes();
                fos.write(bytes);
                fos.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //使用字节流读取文件
    public static void binaryRead(File file){
        try {
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes=new byte[1024];
            int len=0;
            //len = fis.read(bytes);//返回的是读取的最大字节数
           while((len = fis.read(bytes))!=-1){
                System.out.println(new String(bytes, 0, len));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //使用字符流写入文件
    public static void charWrite(File file){
        try {
            TimeUnit.SECONDS.sleep(5);
            FileWriter fw = new FileWriter(file);
            fw.write("");
            FileWriter fileWriter = new FileWriter(file, true);
            Scanner sc = new Scanner(System.in);
            System.out.println("请写入一些信息:");
            String line = "";
            while((line=sc.nextLine())!=null){
               if("quit".equals(line))break;
               fileWriter.write(line);
               fileWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    //使用字符缓冲流读取文件
    public static void charReader(File file){
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = br.readLine())!=null){
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void RandowAccessFileOpt(File file){
        //RandomAccessFile是字节流
        boolean flag=true;
        while(flag){
            System.out.println("您想如何操作文件?1.读取文件 2.写入文件 0.退出");
            Scanner sc = new Scanner(System.in);
            int select = sc.nextInt();
            switch (select){
                case 1:
                    try {
                        RandomAccessFile raf = new RandomAccessFile(file, "rw");
                        String line = raf.readLine();
                        while((line=raf.readLine())!=null){
                            System.out.println(line);
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    System.out.println("请开始编辑:");
                    try {
                        RandomAccessFile raf = new RandomAccessFile(file, "rw");
                        raf.seek(raf.length());
                        String line="";
                        while((line=sc.nextLine())!=null){
                            if("quit".equals(line))break;
                            raf.writeChars(line);
                        }

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 0:
                    flag=false;
                    break;
                default:
                    System.out.println("您的输入有误!");
            }
        }
    }
    public static void main(String[] args) {
        //Student student = new Student(1, "jack", '男', 19);
        //Student student2 = new Student(2, "jane", '女', 17);
        File file = new File("./document/obj.txt");
        //writeObj(file, student);
       // readObj(file);
        //binaryWrite(file);
        //binaryRead(file);
        //charWrite(file);
        charReader(file);
        RandowAccessFileOpt(file);
    }
}

class Student implements Serializable {
    private int id;
    private String name;
    private char sex;
    private int age;

    public Student() {
    }

    public Student(int id, String name, char sex, int age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值