1、FileOutputStream詳解java
1.該類的構造方法,有第二個參數node
FileOutputStream(String address,boolean append)git
append默認false,也就是新的寫入會覆蓋原來的東西。改成true的話,也就是以追加的形式寫入文件github
package com.bjpowernode.java_learning; import java.io.*; public class D97_1_FileOutputStream { public static void main(String[] args){ //1.建立文件輸出字符流 FileOutputStream f1 = null; try { f1 = new FileOutputStream("C:\\Users\\lenovo1\\Workspaces\\MyEclipse CI\\Java_learning\\src\\com\\bjpowernode\\java_learning\\temp1.txt"); //參數中的文件若是不存在的話,就會自動建立 //2.開始寫 //推薦最後的時候爲了保證數據徹底寫入硬盤,因此要刷新 String msg = "HelloWorld"; f1.flush();//強制寫入 //將String轉換成byte數組 byte[] bytes = msg.getBytes(); f1.write(bytes); //若是帶參數,即write(Object o,int a,int b)表明對象o的第a個字符到第b個字符寫入文件 }catch(Exception e1) { e1.printStackTrace(); }finally{ //關閉 if(f1 != null) { try { f1.close(); }catch(Exception e) { e.printStackTrace(); } } } } }
2、文件的複製數組
package com.bjpowernode.java_learning; import java.io.*; public class D97_2_CompleteCopyFile { public static void main(String[] args) throws IOException,FileNotFoundException{ //建立輸入流 FileInputStream f1 = new FileInputStream("C:\\Users\\lenovo1\\Workspaces\\MyEclipse CI\\Java_learning\\src\\com\\bjpowernode\\java_learning\\temp1.txt"); //建立輸出流 FileOutputStream f2 = new FileOutputStream("C:\\Users\\lenovo1\\Workspaces\\MyEclipse CI\\Java_learning\\src\\com\\bjpowernode\\java_learning\\temp2.txt"); //一邊讀一邊寫 byte[] bytes = new byte[1024];//1kb; int temp = 0; while((temp=f1.read(bytes)) != -1){ //將byte數組中的內容直接寫入 f2.write(bytes); } //刷新 f2.flush(); //關閉 f1.close(); f2.close(); } }
3、源碼:微信
D97_1_FileOutputStream.javaapp
D97_2_CompleteCopyFile.java學習
https://github.com/ruigege66/Java/blob/master/D97_1_FileOutputStream.java大數據
https://github.com/ruigege66/Java/blob/master/D97_2_CompleteCopyFile.javaui
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客園:https://www.cnblogs.com/ruigege0000/
4.歡迎關注微信公衆號:傅里葉變換,我的公衆號,僅用於學習交流,後臺回覆」禮包「,獲取大數據學習資料