Java基础--常用IO流使用实例
读取文本文件:
1 private static void ioDemo1() { 2 try { 3 FileReader fileReader = new FileReader("C:\\Users\\yan\\Desktop\\编码规范 (2).txt"); 4 int aa; 5 aa = fileReader.read(); 6 while (aa!=-1) {//转换为char类型,否则是一堆数字 7 System.out.print((char)aa); 8 aa = fileReader.read(); 9 } 10 fileReader.close(); 11 } catch (FileNotFoundException e) { 12 e.printStackTrace(); 13 } catch (IOException e) { 14 e.printStackTrace(); 15 } 16 }
复制二进制文件:
1 private static void ioDemo2() { 2 try { 3 FileInputStream fr = new FileInputStream("D:/test.xlsx");//不能使用FileReader(字符流) 4 FileOutputStream fw = new FileOutputStream("D:/test1.xlsx"); 5 int read = fr.read(); 6 while (read!=-1) { 7 fw.write(read); 8 read=fr.read(); 9 } 10 fw.flush();//杯子中没有饮料不代表饮料已喝完,此方法就是清空吸管中的饮料 11 fw.close(); 12 fr.close(); 13 } catch (FileNotFoundException e) { 14 e.printStackTrace(); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 }
使用缓冲流复制二进制文件:
1 private static void ioDemo3() { 2 try { 3 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/test.xlsx")); 4 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\yan\\Desktop\\test.xlsx")); 5 byte[] buffer = new byte[2048]; 6 int read = bis.read(buffer); 7 while(read!=-1) 8 { 9 bos.write(buffer, 0, read); 10 read = bis.read(buffer); 11 } 12 bos.flush(); 13 bos.close(); 14 bis.close(); 15 } catch (FileNotFoundException e) { 16 e.printStackTrace(); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 }
数据流传递数据:
1 private static void ioDemo4() { 2 try { 3 4 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 5 DataOutputStream dos = new DataOutputStream(baos); 6 long src = 1234454544; 7 dos.writeLong(src); 8 9 byte[] bytes = baos.toByteArray(); 10 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 11 DataInputStream dis = new DataInputStream(bais); 12 long recieve; 13 recieve = dis.readLong(); 14 15 System.out.println("recieve:" + recieve); 16 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 }
PrintStream:
ObjectStream:
1 private static void ioDemo5() { 2 ObjectOutputStream oos = null; 3 ObjectInputStream ois = null; 4 Student s = new Student("悟空", 007, 95.5f); 5 Student s1 = null; 6 7 try { 8 FileOutputStream fos = new FileOutputStream("C:\\Users\\yan\\Desktop/ccc.txt"); 9 oos = new ObjectOutputStream(fos); 10 oos.writeObject(s); 11 12 ois = new ObjectInputStream(new FileInputStream("C:\\Users\\yan\\Desktop/ccc.txt")); 13 s1 = (Student) ois.readObject(); 14 15 System.out.println("姓名:" + s1.getName()); 16 System.out.println("学号:" + s1.getId()); 17 System.out.println("成绩:" + s1.getScore()); 18 fos.close(); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } finally { 22 try { 23 oos.close(); 24 ois.close(); 25 System.exit(-1); 26 } catch (Exception e) { 27 System.exit(-1); 28 } 29 } 30 } 31 //实现Serializable的作用是可以将对象保存在文件中用于传输 32 class Student implements Serializable { 33 private static final long serialVersionUID = -111977186014986048L; 34 private String name; 35 private int id; 36 private transient float score;//transient意思是转瞬即逝,起的作用是反序列化 37 38 public Student(String name, int id, float score) { 39 super(); 40 this.name = name; 41 this.id = id; 42 this.score = score; 43 } 44 45 public String getName() { 46 return name; 47 } 48 49 public void setName(String name) { 50 this.name = name; 51 } 52 53 public int getId() { 54 return id; 55 } 56 57 public void setId(int id) { 58 this.id = id; 59 } 60 61 public float getScore() { 62 return score; 63 } 64 65 public void setScore(float score) { 66 this.score = score; 67 } 68 }
God, Grant me the SERENITY, to accept the things I cannot change,
COURAGE to change the things I can, and the WISDOM to know the difference.