-IO流的四個基類:
字節流:inputStream、outputSteam、
字符流:writer、reader
字符流出現的原因
package day18_IO; import java.io.FileWriter; import java.io.IOException; //對應視頻18_07 import communal.GetCurrentPath; /** * 概述:學習IO流(字符流和字節流) 字節流的兩個基類: InputStream,OutputStream 字符流的兩個基類: Reader,Writer * * @author: kolen.j * */ public class Day18_07_FileWriteDemo { public static void main(String[] args) throws IOException { // 通過GetCurrentPath類獲取當前目錄的 “類路徑”/bin/ GetCurrentPath path = new GetCurrentPath(); String urlpath = path.getCurrentPath(); // 創建一個FileWriter對象,該對象一被初始化就必須要明確被操作的文件 // 而且該文件會被創建到指定目錄下,如果該目錄下有同名文件,將會被覆蓋 // 其實該步驟就是在明確數據的存放目的地 FileWriter fw = new FileWriter(urlpath + "test.txt"); // 調用writer方法,將字符串寫入到流中 fw.write("asdfasdsaddfsadfsdf"); // 刷新流對象中的緩衝中的數據 // 將數據刷到目的地中 // fw.flush(); // 關閉流資源,但是關閉之前會刷新一次內部的緩衝中的數據 fw.close(); } }