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 + '}'; } }
IO流的应用
于 2022-10-13 20:52:06 首次发布