我們都知道流分為 位元組流和字符流
輸出流又分:位元組輸出流,字符輸出流
輸入流又分:位元組輸入流,字符輸入流
/*在java中OutputStream表示位元組輸出流,可以將java程式中的資料寫到檔案中, OutputStream是所有位元組輸出流的頂層父類,是一個抽象類,如果要用,需要使用子類, 常用的子類FileOutputStream 構造方法: FileOutputStream?(File file): 傳遞File型別的檔案, FileOutputStream?(String name): 傳遞一個字串型別的檔案路徑, 常用方法: void write?(int b): 向檔案中寫入一個位元組, void write?(byte[] b): 向檔案中寫入一個位元組陣列, void write?(byte[] b, int off, int len): 向檔案中寫入位元組輸入的一部分, void close?(): 釋放資源. FileOutputStream的使用步驟 1. 創建一個FileOutputStream物件,并且指定一個目的地檔案 2. 呼叫write方法,向檔案中寫入資料, 3. 釋放資源(關閉流) 注意: 在java中,一個字符占兩個位元組,但是如果這個字符在ASCII碼范圍內 ,那么這個字符在計算機 中是占一個位元組的,可以以一次寫一個位元組的方式寫入到檔案中. 中文在作業系統中占多個位元組的, 如果檔案采用的是GBK編碼,那么這個中文就占2個位元組, 如果檔案采用的是UTF-8編碼,那么這個中文占3個位元組, */ public class Demo02OutputStream { public static void main(String[] args) throws IOException { byte arr[] = new byte[]{12, 13, 13, 45}; //創建一個FileOutputStream物件,并且指定一個目的檔案 FileOutputStream fs = new FileOutputStream("file01.text"); //呼叫write方法,向檔案中寫入資料 //寫入byte【】型別 fs.write(arr); //寫入BYTE【】型別,從幾號開始到幾號索引結束 fs.write(arr, 1, arr.length - 1); // System.out.println(fs); //如果不釋放資源,那么如果這個程式不結束的話,這個資源會一直處于被占用的狀態. //3. 釋放資源(關閉流) fs.close(); } }
/* 字串和位元組輸出的相互轉換, 字串->位元組陣列 byte[] getBytes?(): 使用平臺默認的編碼方式將字串轉成位元組陣列, 位元組陣列 -> 字串 String?(byte[] bytes): 將位元組陣列轉成一個字串 String?(byte[] bytes, int offset, int length): 將位元組陣列的一部分轉成String, 引數bytes: 表示要轉換的位元組陣列 引數offset: 表示從哪個位置開始轉 引數length: 表示轉幾個. */ public class Demo03Parse { public static void main(String[] args) { //定義一個字串 String s="abcde"; //呼叫方法把這個字串轉換為位元組陣列 byte[] arr=s.getBytes(); System.out.println(Arrays.toString(arr)); String s1="java愛好者"; byte[]arr1=s1.getBytes(); System.out.println(Arrays.toString(arr1));//從列印結果可以看到文字占了三個位元組 System.out.println("---------------------"); //位元組陣列轉換為字串 String str1=new String(arr); System.out.println("str1:"+str1); String str2=new String(arr,1,arr.length-2); System.out.println(str2); } }
/* 使用位元組輸出流向檔案中一次寫一個位元組陣列 方法: void write?(byte[] b): 向檔案中寫入一個位元組陣列, void write?(byte[] b, int off, int len): 向檔案中寫入位元組輸入的一部分, */ public class Demo04OutputStream { public static void main(String[] args) throws IOException { //創建FileOutputStream物件,并指定目標檔案 FileOutputStream fs=new FileOutputStream("java265.txt"); //使用位元組輸出流向檔案寫一個位元組陣列 //創建一個字串,轉位元組陣列 String s="abcde"; byte[] b = s.getBytes(); //把位元組陣列輸出到指定檔案 // fs.write(b); //向檔案中寫入位元組輸出的一部分 fs.write(b,0,b.length-1); //關閉流 fs.close(); }
/* 如果想要在原來檔案的后面進行續寫,可以使用另外的構造方法創建物件, FileOutputStream?(File file, boolean append): 第二個引數append表示是否要續寫, true表示續寫, FileOutputStream?(String name, boolean append): 第二個引數append表示是否要續寫, true表示續寫, */ public class Demo05OutputStream { public static void main(String[] args) throws IOException { FileOutputStream fs = new FileOutputStream("fiel02.text", true); //字串轉位元組陣列 fs.write("我是java265".getBytes()); //釋放資源 fs.close(); } }
/* 換行寫 如果要實作換行,可以使用換行符, 每個作業系統的換行符都不一樣, windows: \r\n linux: \n mac: \r */ public class Demo06WriteLine { public static void main(String[] args) throws IOException { FileOutputStream fs = new FileOutputStream("fiel03", true); fs.write("我是一個java愛好者\r\n".getBytes()); fs.write("我來自java265\r\n".getBytes()); fs.write("\r\n\r\njava265.com".getBytes()); fs.close(); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/479212.html
標籤:Java
上一篇:繪制幾何圖形
下一篇:從零玩轉人臉識別
