欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > Java重修笔记 第六十五天 IO 流 - 打印流、PrintStream 和 PrintWriter、properties 类

Java重修笔记 第六十五天 IO 流 - 打印流、PrintStream 和 PrintWriter、properties 类

2024/10/23 23:34:38 来源:https://blog.csdn.net/Code___slave/article/details/142800664  浏览:    关键词:Java重修笔记 第六十五天 IO 流 - 打印流、PrintStream 和 PrintWriter、properties 类
  • 打印流

1. 打印流只有输出流没有输入流

2. 字节打印流和字符打印流都是包装流,字节打印流是 FilterOutputStream 类的子类

3. 打印流不仅可以打印在显示器上,根据构造函数传进来的参数指定打印到不同对象中去

  • PrintStream 类(字节打印流)
public class PrintStream01 {public static void main(String[] args) throws IOException {// 字节打印流PrintStream out = System.out;// 默认情况下, 打印流输出数据的位置是显示器out.print("PrintStream Hello");out.write("韩顺平, 你好".getBytes(StandardCharsets.UTF_8));// 修改打印流打印的设备System.setOut(new PrintStream("d:\\a.txt"));// 修改默认输出设备后, 输出的就不是显示器, 而是规定的文件中System.out.println("hello, 韩顺平教育! ");out.close();}
}

  • PrintWriter 类(字符打印流)
public class PrintWriter01 {public static void main(String[] args) throws IOException {// PrintWriter printWriter = new PrintWriter(System.out);PrintWriter printWriter = new PrintWriter(new FileWriter("d:\\a.txt"));printWriter.println("PrintWriter hello");printWriter.close();}
}

  • Properties 类常用方法

1. load:加载输入流到 Properties 对象

        public void load(Reader reader) throws IOException

        参数:reader - 输入字符流

        public void load(InputStream inStream) throws IOException

        参数:inStream - 输入字节流

        异常:IOException - 从输入流读取时是否发生错误

                   IllegalArgumentException - 输入中是否出现格式错误的Unicode转义

2. list:将此属性列表打印到指定的输出流

        public void list(PrintWriter out)

        参数:out - 输出字符流

        public void list(PrintStream out)

        参数:out - 输出字节流

        异常:ClassCastException - 如果此属性列表中有 key 不是字符串

3. getProperty:根据 key 获取 value

        public String getProperty(String key)

        返回值:key 对应的 value 值,若文件中没有对应的键值对,则返回 null

        参数:key - 键

public class Properties01 {public static void main(String[] args) throws IOException {// 使用 Properties 类来读取 mysql.properties 文件, 获得该文件下的所有属性信息// 第一步: 创建 Properties 对象Properties properties = new Properties();// 第二步: 加载指定的配置文件properties.load(new FileReader("src\\mysql.properties"));// 第三步: 读取配置文件中的信息properties.list(System.out);// 第四步: 处理信息, 根据 key 获取对应的 valueSystem.out.println("用户名 = " + properties.getProperty("user"));System.out.println("密码 = " + properties.getProperty("pwd"));}
}

4. 配置键值对

        public Object setProperty(String key, String value)

        返回值:此属性列表中指定键的上一个值,如果没有则返回 null 

        参数:key - 要放入此属性列表的关键字  

                   value - key 对应的 value 值

        说明:底层调用的是 HashTable 的 put() 方法,不过将 key 和 value 的类型限制为 String 

5. 存储配置文件

        public void store(Writer writer, String comments) throws IOException

        参数:writer - 输出字符流

                   comments - 属性列表的描述,会打印到该配置文件的第一行

        public void store(OutputStream out, String comments) throws IOException

        参数:out - 输出字节流

                   comments - 属性列表的描述,会打印到该配置文件的第一行

        说明:若文件不存在,则根据输出流指定的位置生成一个文件,文件内容是 setProperty 方法中添加的键值对,当键值对中出现中文时,会以 Unicode 码的形式保存(字节输入流)。当文件存在时,key 值相同的键值对会修改其对应的 value 值,否则就是添加键值对。

        异常:IOException - 如果将此属性列表写入指定的输出流 

                  ClassCastException - 如果此 Properties 对象包含的键值对类型不是 String

                  NullPointerException - 如果 out 为空

public class Properties02 {public static void main(String[] args) throws IOException {// 使用 Properties 来创建配置文件, 并修改文件里面的内容Properties properties = new Properties();// 配置键值对properties.setProperty("charset", "utf8");properties.setProperty("user", "汤姆"); // 若使用字节输入流保存中文时, 保存的是该中文的 Unicode 码properties.setProperty("pwd", "abc123");// 将配置好的键值对存储到指定文件中properties.store(new FileOutputStream("src\\mysql2.properties", true), "this is a description~");System.out.println("保存配置文件成功");}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com