Java基礎--常用IO流使用執行個體

來源:互聯網
上載者:User

標籤:java基礎   常用   details   沒有   fileread   read   exit   null   puts   

讀取文字檔:

 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 }

 

Java基礎--常用IO流使用執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.