如何使用文件字節輸出流(FileOutputStream)將數據寫到磁盤文件中

步驟:

1.創建目標對象(表示把數據存儲到哪一文件中)

2.創建文件字節輸出流對象

3.具體的輸出操作(往外寫的操作)

4.關閉資源對象

//演示文件字節輸出流
public class FileOutputStreamDemo {
	public static void main(String[] args) throws IOException {
		//1創建目標文件
		File desFile = new File("file/des.txt");
		//2創建字節輸出流對象
		FileOutputStream out = new FileOutputStream(desFile);
		//3IO操作
		/*
		 * void write(int b) 將一個字節寫出到文件中
		 * void write(byte[] b) 將數組b中的數據寫出到文件中
		 * void write(byte[] b,int off,int len) 將數組中的從off索引開始的len個字節寫出到文件中
		 */
		//out.write(65);寫出A
		out.write("哈哈哈".getBytes());
		//out.write("Lucyisbeatiful".getBytes(),5,5);
		//4 關閉文件
		out.close();
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章