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

【JAVA学习笔记】66 - 本章作业(IO流)

项目代码

https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter19/src/com/yinhai/homework

1.使用File类和FileWriter类

(1)在判断e盘下是否有文件夹mytemp,如果没有就创建mytemp

public class Homework01 {public static void main(String[] args) throws FileNotFoundException {String path = "e:\\test\\mytemp";File file = new File(path);if(!file.exists()){file.mkdirs();}else{System.out.println("已存在");}}
}

        

(2)在e:\\mytemp目录下,创建文件hello.txt

记得关闭该字符流

FileWriter fileWriter =null;try {fileWriter = new FileWriter(path + "\\test.txt",true);fileWriter.write("hello,world\n");} catch (IOException e) {e.printStackTrace();}finally {fileWriter.close();}

(3)如果hello.txt已经存在,提示该文件已经存在,就不要再重复创建了

//3)如果hello.txt已经存在,提示该文件已经存在,就不要再重复创建了File file1 = new File(path,"\\hello.txt");FileWriter fileWriter1 =null;try {if(!file1.exists()) {fileWriter1 = new FileWriter(file1);fileWriter1.write("hello,world\n");}else{System.out.println("已存在");}} catch (IOException e) {e.printStackTrace();}finally {if(fileWriter1 != null) {fileWriter1.close();}}

2. 使用处理流

要求:使用BufferedReader读取一 个文本文件,为每行加上行号,再连同内容一并输出到屏幕上。
 

3.使用Properties类的使用

(1)要编写一个dog.properties

        name= tom

        age= 5

        color= red

(2)编写Dog类(name,age.color)创建一 个dog对象, 读取dog.properties 用相应的内容完
成属性初始化,并输出

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

3.使用Properties类,使用对象流

(1)要编写一个dog.properties

        name = tom

        age=5

        color= red

(2)编写Dog类(name,age,color)创建一个dog对象, 读取dog.properties 用相应的内容完成属性初始化,并输出

(3)将创建的Dog对象,序列化到文件dog.dat文件

public class Homework03 {public static void main(String[] args) throws IOException, ClassNotFoundException {Properties properties = new Properties();properties.setProperty("name","tom");properties.setProperty("age","50");properties.setProperty("color","red");properties.store(new FileOutputStream("src\\dog.properties"),null);Dog dog = new Dog(properties);ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("e:\\test\\dog.dat"));objectOutputStream.writeObject(dog);objectOutputStream.close();//需要关闭或者刷新才会写入成功ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("e:\\test\\dog.dat"));Object dogInputStream = objectInputStream.readObject();Dog dog1 = (Dog)dogInputStream;System.out.println(dog1);}
}

http://www.lryc.cn/news/223087.html

相关文章:

  • vscode中 vue3+ts 项目的提示失效,volar插件失效问题解决方案
  • Elasticsearch:在 ES|QL 中使用 DISSECT 和 GROK 进行数据处理
  • 基于自适应自回归模型的高级人工智能概念及其实现
  • windows的mysql启动错误,查看windows日志
  • centos7部署Canal与Canal集成使用
  • C语言--分段函数--switch语句
  • 动态规划31(Leetcode188买卖股票的最佳时机4)
  • npm包管理相关命令
  • 2023年Q3乳品行业数据分析(乳品市场未来发展趋势)
  • 软考 系统架构设计师系列知识点之边缘计算(2)
  • Maven中的继承与聚合
  • 第三章 UI开发的点点滴滴
  • 637. 二叉树的层平均值
  • 【Java笔试强训】Day9(CM72 另类加法、HJ91 走方格的方案数)
  • django REST框架- Django-ninja
  • 数据结构与算法C语言版学习笔记(3)-线性表的链式结构:链表
  • Web学习笔记-Vue3(环境配置、概念、整体布局设计)
  • 【React-Native开发3D应用】React Native加载GLB格式3D模型并打包至Android手机端
  • python的列表
  • [100天算法】-最短无序连续子数组(day 66)
  • 001. 变量、环境变量
  • 软考软件设计师刷题笔记整理
  • Canal
  • SpringBoot实现mysql与clickhouse多数据源
  • 为什么是LangChain?
  • Labview的分支判断
  • 蓝桥杯双周赛算法心得——串门(双链表数组+双dfs)
  • mysql 配置主从复制 及 Slave_SQL_Running = no问题排查
  • 再获5G RedCap能力认证!宏电5G RedCap工业智能网关通过中国联通5G物联网OPENLAB开放实验室测试验证
  • 牛客--汽水瓶python