当前位置: 首页 > news >正文

Java 第19章 IO流 课堂练习+本章作业

文章目录

  • Buffered流拷贝二进制文件
  • 创建文件写入文本
  • 读取文本文件
  • 存读Properties文件

Buffered流拷贝二进制文件

package com.hspedu.chapter19.outputStream;import java.io.*;public class BufferedCopy02 {public static void main(String[] args) {String srcFilePath = "c:\\bh.jpg";String destFilePath = "c:\\hsp.jpg";BufferedInputStream bis = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(new FileInputStream(srcFilePath));bos = new BufferedOutputStream(new FileOutputStream(destFilePath));byte[] buff = new byte[1024];int readLen = 0;while ((readLen = bis.read(buff)) != -1) {bos.write(buff, 0, readLen);}System.out.println("Copied successfully..");} catch (IOException e) {throw new RuntimeException(e);} finally {try {bis.close();bos.close();} catch (IOException e) {throw new RuntimeException(e);}}}
}

创建文件写入文本

在这里插入图片描述

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;public class Homework01 {public static void main(String[] args) throws IOException {String filePath = "c:\\mydir";File file = new File(filePath);if (!file.exists()) {if (file.mkdirs())System.out.println(filePath + " has been created successfully..");elseSystem.out.println(filePath + " was fail to be created..");} else {System.out.println(filePath + " has already existed..");}String destfile = filePath + "\\hello.txt";File file1 = new File(destfile);if (!file1.exists()) {if (file1.createNewFile()) {BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file1));bufferedWriter.write("hello, world~~ 韩顺平教育");bufferedWriter.close();System.out.println(destfile + " has been created successfully..");} else {System.out.println(destfile + " has already existed..");}} else {System.out.println(destfile + " has already existed..");}}
}

读取文本文件

在这里插入图片描述

public class Homework02 {public static void main(String[] args) throws IOException {String filePath = "c:\\hello.txt";String line = "";int lineNum = 0;BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));while ((line = bufferedReader.readLine()) != null) {System.out.println(++lineNum + " " + line);}if (bufferedReader != null)bufferedReader.close();}
}

在这里插入图片描述

public class Homework02 {public static void main(String[] args) throws IOException {String filePath = "c:\\hello.txt";// InputStreamReader指定编码方式InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");String line = "";int lineNum = 0;BufferedReader bufferedReader = new BufferedReader(isr);while ((line = bufferedReader.readLine()) != null) {System.out.println(++lineNum + " " + line);}if (bufferedReader != null)bufferedReader.close();}
}

存读Properties文件

在这里插入图片描述

public class Homework03 {public static void main(String[] args) throws IOException {String filePath = "src\\dog.properties";Properties properties = new Properties();properties.load(new FileReader(filePath));String name = properties.get("name") + ""; //Object -> Stringint age = Integer.parseInt(properties.get("age") + "");// Object -> intString color = properties.get("color") + "";//Object -> StringDog dog = new Dog(name, age, color);System.out.println("===dog对象信息====");System.out.println(dog);//将创建的Dog 对象 ,序列化到 文件 dog.dat 文件String serFilePath = "c:\\dog.dat";ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFilePath));oos.writeObject(dog);//关闭流oos.close();System.out.println("dog对象,序列化完成...");}//在编写一个方法,反序列化dog@Testpublic void m1() throws IOException, ClassNotFoundException {String serFilePath = "e:\\dog.dat";ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFilePath));Dog dog = (Dog)ois.readObject();System.out.println("===反序列化后 dog====");System.out.println(dog);ois.close();}
}class Dog implements  Serializable{private String name;private int age;private String color;public Dog(String name, int age, String color) {this.name = name;this.age = age;this.color = color;}@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +", color='" + color + '\'' +'}';}
}
http://www.lryc.cn/news/272306.html

相关文章:

  • 一键制作电子样册,提升企业品牌形象
  • Linux 的引导与服务控制
  • 多输入多输出 | MATLAB实现SSA-CNN麻雀算法优化卷积神经网络多输入多输出预测
  • 高端电流检测方案
  • IP地址、子网掩码与网络地址
  • python 深度学习 记录遇到的报错问题10
  • linux下docker搭建Prometheus +SNMP Exporter +Grafana进行核心路由器交换机监控
  • Github 2023-12-31 开源项目日报 Top10
  • 管程-第三十三天
  • 嵌入式中断理解
  • React16源码: Hooks源码实现
  • 华为端口隔离高级用法经典案例
  • java项目启动jar包启动参数设置端口号
  • 【数据结构和算法】寻找数组的中心下标
  • 多粒度在研究中的应用
  • Docker命令---查看容器日志
  • Spring Boot 基于Redisson实现注解式分布式锁
  • Javascript 正则表达式零宽断言
  • Chocolatey
  • 雍禾植发成毛发行业标杆!雍禾医疗获“年度医疗大健康消费企业”
  • Linux内核--进程管理(十二)共享内存和信号量
  • java 构造方法
  • CISSP 第2章: 人员安全和风险管理概念
  • 前端八股文(CSS篇)一
  • 游戏加速器LSP/DLL导致WSL.EXE无法打开问题修复!
  • 宏电股份5G RedCap终端产品助力深圳极速先锋城市建设
  • linux top命令中 cpu 利用率/mem 使用率与load average平均负载计算方式
  • win11出现安全中心空白和IT管理员已限制对某些区域的访问(不一样的解决方式),真实的个人经历,并且解决经过
  • 关于安卓重启设备和重启应用进程
  • Linux内核--进程管理(十三)O(1)调度算法