> 文章列表 > 打印流,Properties类

打印流,Properties类

打印流,Properties类

  1. 打印流只有输出流,没有输入流
  2. package com.hspedu.printstream;import java.io.IOException;
    import java.io.PrintStream;/* @author 韩顺平* @version 1.0* 演示PrintStream (字节打印流/输出流)*/
    public class PrintStream_ {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);}*/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, 韩顺平教育~");}
    }
    
  3. package com.hspedu.transformation;import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;/* @author 韩顺平* @version 1.0* 演示 PrintWriter 使用方式*/
    public class PrintWriter_ {public static void main(String[] args) throws IOException {//PrintWriter printWriter = new PrintWriter(System.out);//System.out标准输出显示器PrintWriter printWriter = new PrintWriter(new FileWriter("e:\\\\f2.txt"));printWriter.print("hi, 北京你好~~~~");printWriter.close();//flush + 关闭流, 才会将数据写入到文件..}
    }
    
  4. Properties类

    1. package com.hspedu.properties_;import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.io.IOException;
      import java.util.Properties;/* @author 韩顺平* @version 1.0*/
      public class Properties02 {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);}
      }
      

      Properties类的方法

    2. package com.hspedu.properties_;import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.util.Properties;/* @author 韩顺平* @version 1.0*/
      public class Properties03 {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 nullif (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, 就addEntryreturn null;}*/properties.setProperty("charset", "utf8");properties.setProperty("user", "汤姆");//注意保存时,是中文的 unicode码值properties.setProperty("pwd", "888888");//将k-v 存储文件中即可properties.store(new FileOutputStream("src\\\\mysql2.properties"), null);//null这个位置,在文件第一行是注释,null表示没有注释。System.out.println("保存配置文件成功~");}
      }
      

光彪游戏网