標準輸入輸出() & 列印流
阿新 • • 發佈:2022-03-30
public static void main(String[] args) { //System 類 的 public final static InputStream in = null; // System.in 編譯型別 InputStream // System.in 執行型別 BufferedInputStream // 表示的是標準輸入 鍵盤 System.out.println(System.in.getClass()); //老韓解讀 //1. System.out public final static PrintStream out = null;//2. 編譯型別 PrintStream //3. 執行型別 PrintStream //4. 表示標準輸出 顯示器 System.out.println(System.out.getClass()); System.out.println("hello, 韓順平教育~"); Scanner scanner = new Scanner(System.in); System.out.println("輸入內容"); String next = scanner.next(); System.out.println("next=" + next); }
位元組列印流(本質還是輸出流)
public static void main(String[] args) throws IOException { PrintStream out = System.out; //在預設情況下,PrintStream 輸出資料的位置是 標準輸出,即顯示器 /* public void print(String s) { if (s == null) { s = "null"; } write(s); }View Code*/ out.print("john, hello"); //因為print底層使用的是write , 所以我們可以直接呼叫write進行列印/輸出 out.write("韓順平,你好".getBytes()); out.close(); //我們可以去修改列印流輸出的位置/裝置 //1. 輸出修改成到 "e:\\f1.txt" //2. "hello, 韓順平教育~" 就會輸出到 e:\f1.txt //3. public static void setOut(PrintStream out) { // checkIO(); // setOut0(out); // native 方法,修改了out // } System.setOut(new PrintStream("e:\\f1.txt")); System.out.println("hello, 韓順平教育~"); }
字元列印流
public static void main(String[] args) throws IOException { //PrintWriter printWriter = new PrintWriter(System.out); PrintWriter printWriter = new PrintWriter(new FileWriter("e:\\f2.txt")); printWriter.print("hi, 北京你好~~~~"); printWriter.close();//flush + 關閉流, 才會將資料寫入到檔案.. }View Code
配置檔案,Properties類 格式: ip=225.225.225.225 name=tom
讀取
public static void main(String[] args) throws IOException { //使用Properties 類來讀取mysql.properties 檔案 //1. 建立Properties 物件 Properties properties = new Properties(); //2. 載入指定配置檔案 properties.load(new FileReader("src\\mysql.properties")); //3. 把k-v顯示控制檯 properties.list(System.out); //4. 根據key 獲取對應的值 String user = properties.getProperty("user"); String pwd = properties.getProperty("pwd"); System.out.println("使用者名稱=" + user); System.out.println("密碼是=" + pwd); }View Code
寫入&修改
public static void main(String[] args) throws IOException { //使用Properties 類來建立 配置檔案, 修改配置檔案內容 Properties properties = new Properties(); //建立 //1.如果該檔案沒有key 就是建立 //2.如果該檔案有key ,就是修改 /* Properties 父類是 Hashtable , 底層就是Hashtable 核心方法 public synchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> entry = (Entry<K,V>)tab[index]; for(; entry != null ; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) { V old = entry.value; entry.value = value;//如果key 存在,就替換 return old; } } addEntry(hash, key, value, index);//如果是新k, 就addEntry return null; } */ properties.setProperty("charset", "utf8"); properties.setProperty("user", "湯姆");//注意儲存時,是中文的 unicode碼值 properties.setProperty("pwd", "888888"); //將k-v 儲存檔案中即可 properties.store(new FileOutputStream("src\\mysql2.properties"), null); System.out.println("儲存配置檔案成功~"); }View Code