java輸入輸出11:IO流(FileOutputStream)
阿新 • • 發佈:2018-12-22
FileOutputStream(String str)在建立物件的時候沒有這個檔案會建立該檔案,如果有這個檔案就會將其清空。
package filePackage; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo7_FileOutputStream { public static void main(String[] args) throws IOException { //建立位元組輸出流物件,如果沒有就自動建立 FileOutputStream fos = new FileOutputStream("yy.txt"); //雖然寫出的是一個int數,但是到檔案上的是一個位元組,會自動去除前三個8位。 fos.write(97); fos.write(98); fos.write(99); fos.close(); } }
FileOutputStream(String str,true)在建立物件的時候沒有這個檔案會建立該檔案,如果有這個檔案就會追加資料。
package filePackage; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo7_FileOutputStream { public static void main(String[] args) throws IOException { //建立位元組輸出流物件,如果沒有就自動建立 FileOutputStream fos = new FileOutputStream("yy.txt",true); //雖然寫出的是一個int數,但是到檔案上的是一個位元組,會自動去除前三個8位。 fos.write(97); fos.write(98); fos.write(99); fos.write(100); fos.close(); } }