Day14-Java进阶-字符缓冲流转换流序列化流打印流Properties 集合

1. 字符缓冲流

1.1 字符缓冲流构造方法

package com.itheima.buffered;import java.io.*;public class BufferedStreamDemo1 {/*字符缓冲流的基本使用注意: 缓冲流本身不具备读写功能, 只是对普通的流对象做包装构造方法:BufferedReader(Reader reader)BufferedWriter(Writer writer)*/public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("day14-code\\A.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("day14-code\\B.txt"));char[] chs = new char[1024];int len;while((len = br.read(chs)) != -1){bw.write(chs, 0, len);}br.close();bw.close();}private static void method() throws IOException {BufferedReader br = new BufferedReader(new FileReader("day14-code\\A.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("day14-code\\B.txt"));int i;while((i = br.read()) != -1){bw.write(i);}br.close();bw.close();}
}

1.2 字符缓冲流特有方法

package com.itheima.buffered;import java.io.*;public class BufferedStreamDemo2 {/*字符缓冲流的特有方法:public String readLine()    读取一行字符串, 读取到末尾返回 nullpublic void newLine()       写出换行符 (具有跨平台性)*/public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("day14-code\\A.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("day14-code\\C.txt"));String line;while((line = br.readLine()) != null){bw.write(line);bw.newLine();}br.close();bw.close();}private static void method() throws IOException {BufferedReader br = new BufferedReader(new FileReader("day14-code\\A.txt"));String line;while((line = br.readLine()) != null){System.out.println(line);}br.close();}
}

1.3 案例1-文本排序

package com.itheima.buffered;import java.io.*;
import java.util.TreeSet;public class BufferedStreamTest1 {/*案例: 请对 D:\出师表.txt 文件进行排序操作*/public static void main(String[] args) throws IOException {// 创建TreeSet集合用于排序TreeSet<String> ts = new TreeSet<>();// 创建输入缓冲流用于读取源文件数据BufferedReader br = new BufferedReader(new FileReader("D:\\出师表.txt"));String len;while ((len = br.readLine()) != null){// 将读取到的每一行字符串存入集合ts.add(len);}br.close();// 创建输出缓冲流关联目标文件BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\出师表.txt"));// 遍历集合, 取出排序后的数据for (String content : ts) {// 写出数据bw.write(content);bw.newLine();}bw.close();}
}

排序前:

排序后:

1.4 案例2-读取学生信息

2. 转换流

package com.itheima.change;import java.io.*;public class ChangeStreamDemo {/*转换流按照指定的字符编码读写构造方法:InputStreamReader(InputStream in, String CharsetName)OutputStreamWriter(OutputStream in, String CharsetName)*/public static void main(String[] args) throws IOException {read();OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\Test.txt", true), "GBK");osw.write("哈哈");osw.close();System.out.println("----------------------");read();}private static void read() throws IOException {InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\Test.txt"), "gbk");int i;while ((i = isr.read()) != -1) {System.out.println((char) i);}isr.close();}}

注: 字节流转换为字符流在后面的网络编程中提到

3. 序列化流

序列化流读写对象序列化: 将对象写出到文件public ObjectOutputStream(OutputStream out)反序列化: 从文件中将对象读取到程序public ObjectInputStream(InputStream in)

3.1 序列化流的操作流程

3.2 案例-读取多个对象

方式一: 用try-catch...捕捉读流末尾异常

package com.itheima.serialization;import com.itheima.domain.Student;import java.io.*;public class SerializationTest {/*需求: 现有3个学生对象, 并将对象序列化到流中, 随后完成反序列化操作*/public static void main(String[] args) throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\D.txt"));while (true) {try {Object o = ois.readObject();System.out.println(o);} catch (EOFException e) {break;}}ois.close();}private static void writeObject() throws IOException {Student stu1 = new Student("张三", 23);Student stu2 = new Student("李四", 24);Student stu3 = new Student("王五", 25);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\D.txt"));oos.writeObject(stu1);oos.writeObject(stu2);oos.writeObject(stu3);oos.close();}
}

方式二: 用集合封装学生对象 (只读写一次集合, 避免异常处理)

package com.itheima.serialization;import com.itheima.domain.Student;import java.io.*;
import java.util.ArrayList;public class SerializationTest2 {/*需求: 现有3个学生对象, 并将对象序列化到流中, 随后完成反序列化操作*/public static void main(String[] args) throws IOException, ClassNotFoundException {readObject();}private static void readObject() throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day14-code\\stu.txt"));ArrayList<Student> list = (ArrayList<Student>) ois.readObject();for (Student stu : list) {System.out.println(stu);}ois.close();}private static void writeObject() throws IOException {Student stu1 = new Student("张三", 23);Student stu2 = new Student("李四", 24);Student stu3 = new Student("王五", 25);ArrayList<Student> list = new ArrayList<>();list.add(stu1);list.add(stu2);list.add(stu3);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day14-code\\stu.txt"));oos.writeObject(list);oos.close();}
}

4. 打印流

package com.itheima.print;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;public class PrintStreamDemo2 {/*PrintStream的基本使用1. 创建对象关联文件public PrintStream (OutputStream os)public PrintStream (File f)public PrintStream (File f, String csn)public PrintStream (String filepath)public PrintStream (String filepath, String csn)2. 写出方法write() : 写出一个字节, 不建议使用, 无法原样写入.print() : 原样写入数据, 无换行println() : 原样写入数据, 带有换行*/public static void main(String[] args) throws Exception {PrintStream ps = new PrintStream(new FileOutputStream("day14-code\\F.txt", true));ps.println("你好");ps.close();}private static void method2() throws FileNotFoundException, UnsupportedEncodingException {PrintStream ps = new PrintStream("D:\\Test.txt", "gbk");ps.println("大家好");ps.close();}private static void method1() throws FileNotFoundException {PrintStream ps = new PrintStream("day14-code\\E.txt");ps.print(97);ps.println(97);ps.println(true);ps.println(false);ps.println('a');ps.println("abc");ps.println(12.3);ps.close();}
}

package com.itheima.print;import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;public class PrintWriterDemo {/*PrintWriter的使用*/public static void main(String[] args) throws IOException {PrintWriter pw = new PrintWriter(new FileWriter("day14-code\\F.txt"), true);pw.println("你好");}
}

5. Properties 集合

5.1 Properties 作为集合使用

package com.itheima.properties;import java.util.Properties;
import java.util.Set;public class PropertiesDemo1 {/*Properties作为集合的使用Object setProperty (String key, String value) : 类似Map集合的put方法String getProperty (String key) : 类似Map集合的get方法Set<String> stringPropertyNames() : 类似Map是集合的keySet方法*/public static void main(String[] args) {Properties prop = new Properties();prop.setProperty("username", "admin");prop.setProperty("password", "1234");Set<String> keySet = prop.stringPropertyNames();for (String key : keySet) {System.out.println(key + "---" + prop.getProperty(key));}}private static void method(Properties prop) {String username = prop.getProperty("username");String password = prop.getProperty("password");System.out.println(username);System.out.println(password);System.out.println(prop);}
}

5.2 Properties 和 IO有关的方法

package com.itheima.properties;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;public class PropertiesDemo2 {/*Properties 和 IO 有关的方法void load(InputStream inStream)     从输入字节流读取属性列表(键和元素对)void load(Reader reader)            从输入字符流读取属性列表(键和元素对)void store(OutputStream out, String comments)   将集合的键值对写出到文件(字节流)void store(Writer writer, String comments)      将集合的键值对写出到文件(字符流)*/public static void main(String[] args) throws IOException {Properties prop = new Properties();FileInputStream fis = new FileInputStream("day14-code\\config.properties");prop.load(fis);fis.close();System.out.println(prop);}private static void method() throws IOException {Properties prop = new Properties();prop.setProperty("username", "admin");prop.setProperty("password", "1234");FileWriter fos = new FileWriter("day14-code\\config.properties");// 此处的comments表示注释, 以字符串类型输入, null时表不要注释prop.store(fos, null);fos.close();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/645191.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【IC设计】奇数分频与偶数分频 电路设计(含讲解、RTL代码、Testbench代码)

文章目录 原理分析实现和仿真偶数分频的电路RTL代码偶数分频的电路Testbench代码偶数分频的电路仿真波形占空比为50%的三分频电路RTL代码占空比为50%的三分频电路Testbench代码占空比为50%的三分频电路仿真波形 参考资料 原理分析 分频电路是将给定clk时钟信号频率降低为div_c…

内容平台加码旅游:谁是下一个网红城市

“姐妹们&#xff0c;你们五一啥安排&#xff1f;”早在3月中旬&#xff0c;小威就在询问两个好朋友的行程&#xff0c;“不早早问&#xff0c;怕约不上你们。” 去年以来&#xff0c;国人的旅游需求快速复苏&#xff0c;像小威的朋友一样&#xff0c;之前爱玩的、不爱玩的似乎…

3月8日是星期六

突然有查询特殊条件日期的需求。 <html> <title>3月8日是星期六</title> <center> <h1 id"h1"></h1> <div id"div"></div> </center> <script> var weekday [星期日, 星期一, 星期二, 星期…

pyTorch框架部署实践

相关代码链接见文末 1.所需基本环境配置 首先&#xff0c;我们需要一个预先训练好的模型以及相应的配置。接下来&#xff0c;为了实际应用这个模型&#xff0c;我们必须搭建一个功能强大的服务器。这台服务器的核心任务是加载我们的模型&#xff0c;并能够接收用户上传的图片。…

线程池嵌套导致的死锁问题

1、背景 有一个报告功能&#xff0c;报告需要生成1个word&#xff0c;6个excel附件&#xff0c;总共7个文件&#xff0c;需要记录报告生成进度&#xff0c;进度字段jd初始化是0&#xff0c;每个文件生成成功进度加1&#xff0c;生成失败就把生成状态置为失败。 更新进度语句&…

基于深度学习网络的十二生肖图像分类matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 5.算法完整程序工程 1.算法运行效果图预览 2.算法运行软件版本 matlab2022a 3.部分核心程序 ............................................................... for i 1:16subplot(4,4,…

HubSpot功能有哪些?

HubSpot是一个功能丰富的平台&#xff0c;主要涵盖市场营销、销售、客户服务和客户关系管理&#xff08;CRM&#xff09;等领域。以下是HubSpot的一些主要功能&#xff1a; 市场营销自动化&#xff1a;HubSpot允许用户制定和执行多渠道的市场营销活动&#xff0c;包括创建和管…

为什么光电测径仪质量更稳定可靠?

光电测径仪与激光扫描式测径仪都是目前常用的外径自动化测量设备&#xff0c;他们能实现的功能相同&#xff0c;但为什么说光电测径仪更稳定可靠&#xff0c;下面一起来看一下。 光电测径仪测量原理 测头部件是测径仪的核心部件&#xff0c;它的作用是将被测物在CCD芯片上清晰…

redisson分布式锁的单机版应用

package com.redis;/*** author linn* date 2024年04月23日 15:31*/ import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.…

怎样压缩jpg文件体积?分享一个极速压缩的方法

在工作中我们经常会遇到图片体积过大需要压缩处理。想要快速压缩jpg格式体积还不想下载软件要怎么操作呢&#xff1f;下面&#xff0c;给大家分享一款小白也能轻松使用的Jpg压缩&#xff08;https://www.yasuotu.com/&#xff09;工具-压缩图。支持上传单张100M以内&#xff0c…

常见大厂面试题(SQL)01

知乎问答最大连续回答问题天数大于等于3天的用户及其对应等级 1.描述 现有某乎问答创作者信息表author_tb如下(其中author_id表示创作者编号、author_level表示创作者级别&#xff0c;共1-6六个级别、sex表示创作者性别)&#xff1a; author_id author_level sex 101 …

命令行vue-cli-service不是内部或外部命令

没有安装vue/cli-service导致的 npm install -g vue/cli-service