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

21-CS61B-lab6:java文件操作以及持久化一见

文件操作

这里的文件操作主要通过File类,Serializable的派生类等实现

拥有路径path(相对路径/绝对路径),创建File对象

File newFile = new File(path);

获得当前工作目录File

File CWD = new File(System.getProperty("user.dir"));

通过System.getProperty("user.dir")得到的是CWD的绝对路径

注:工作目录为运行Main.class的目录,并非储存Main.class的目录

       例如Main.class放在Main目录,我在lab6使用命令行java Main.Main,那么CWD得到的是lab6

获得File对象的路径的String形式

String path = CWD.getPath();

拥有路径中的多个目录名,想转换为File对象

File newFile = Paths.get(CWD.getPath(), dir1, dir2, dir3).toFile();

检查File对象所指向的文件/目录是否已创建

newFile.exists();

检查File对象所指向的是否为文件

newFile.isFile();

检查File对象指向的是否为目录

newFile.isDirectory();

将File指向的文件创建

try {newFile.createNewFile();
} catch (IOException e) {throw new RuntimeException(e);
}

将File指向的目录创建

newFile.mkdir();

将byte[]类写入文件

/*** @param newFile 要写入的文件* @param contents 要写入的内容对象,这里默认implements byte[]*/
static void writeContents(File newFile, Object... contents) {BufferedOutputStream str = new BufferedOutputStream(Files.newOutputStream(newFile.toPath()));for (Object obj : contents) {str.write((byte[]) obj);{str.close();
}

将String类转换为byte[]类

((String) obj).getBytes(StandardCharsets.UTF_8)

读取二进制文件

byte[] byteResult = Files.readAllBytes(newFile.toPath());

将byte[]类转换为String类

String StringResult = new String(byteResult, StandardCharsets.UTF_8);

对象的序列化

序列化对象指的是将对象转换成byte[]数组保存其信息

obj一定要implements Serializable

static byte[] serialize(Serializable obj) {ByteArrayOutputStream stream = new ByteArrayOutputStream(); ObjectOutputStream objectStream = new ObjectOutputStream(stream);objectStream.writeObject(obj); //写入,被stream接收objectStream.close(); //关闭Object流return stream.toByteArray(); //转换为byte[]
}

将对象写入文件

即序列化后写入文件

writeContents(newFile, serialize(obj));

从序列化文件中读取对象

/***@Param file 待读的文件*@Param expectedClass 期望返回的对象类型*/
static <T extends Serializable> T readObject(File file, Class<T> expectedClass) {ObjectInputStream in =new ObjectInputStream(new FileInputStream(file)); //使用in读取ObjectT result = expectedClass.cast(in.readObject());           //cast到期望的类上in.close();return result;
}

持久化

持久化的目的是使我们的程序有记忆,在下次运行的时候,能够继承上次运行的状态继续运行

实现的方式很简单,即将我们需要保存的对象状态作为文件记录在磁盘当中,下次运行时读取该文件恢复对象状态。

CapersRepository.java的若干方法实现

void CapersRepository.setupPersistence();

public static void setupPersistence() {if (!CAPERS_FOLDER.exists()) {CAPERS_FOLDER.mkdir();}File STORY = join(CAPERS_FOLDER, "story");if (!STORY.exists()) {try {STORY.createNewFile();} catch (IOException e) {throw new RuntimeException(e);}}File DOGS = join(CAPERS_FOLDER, "dogs");if (!DOGS.exists()) {DOGS.mkdir();}
}

void CapersRepository.writeStory(String);

public static void writeStory(String text) {File STORY = join(CAPERS_FOLDER, "story");String writeString = readContentsAsString(STORY);if (writeString.isEmpty()) {writeString = text;} else {writeString = writeString + "\n" + text;}writeContents(STORY, writeString);System.out.println(writeString);
}

void CapersRepository.makeDog(String, String, int);

public static void makeDog(String name, String breed, int age) {Dog dogObj = new Dog(name, breed, age);dogObj.saveDog();System.out.println(dogObj);
}

void CapersRepository.celebrateBirthday(String);

public static void celebrateBirthday(String name) {Dog dogObj = Dog.fromFile(name);if (dogObj != null) {dogObj.haveBirthday();dogObj.saveDog();}
}

Dog.java的若干方法实现

Dog Dog.FromFile(String);

public static Dog fromFile(String name) {File dogObj = join(DOG_FOLDER, name);if (!dogObj.exists()) {return null;}return readObject(dogObj, Dog.class);
}

void saveDog();

public void saveDog() {File dog = join(DOG_FOLDER, name);if (!dog.exists()) {try {dog.createNewFile();} catch (IOException e) {throw new RuntimeException(e);}}writeObject(dog, this);
}

http://www.xdnf.cn/news/10596.html

相关文章:

  • BiliNote简介
  • 第100期 DL,多输入多输出通道
  • 学习STC51单片机25(芯片为STC89C52RCRC)
  • edg浏览器打开后默认是360界面
  • 某电子计数跳绳的一次修复经历
  • abandon便签:一个免费好用审美在线的桌面便签应用
  • python打卡day43
  • 【Python序列化】TypeError: Object of type xxx is not JSON serializable问题的解决方案
  • 分词算法BBPE详解和Qwen的应用
  • day 40 python打卡
  • Spring框架学习day6--事务管理
  • 【ISAQB大纲解读】信息隐藏指的是什么
  • 基于Qt的app开发的过渡期
  • PH热榜 | 2025-06-01
  • Flex弹性布局
  • langGraph多Agent
  • 【C语言入门级教学】冒泡排序和指针数组
  • ShardingSphere 分片策略深度解析
  • 导入典籍数据
  • 《仿盒马》app开发技术分享-- 购物车业务逻辑完善(端云一体)
  • java 多线程
  • 基于贝叶斯优化神经网络的光伏功率预测综述
  • Java JVM 内存模型详解
  • LeetCode 付费题157. 用 Read4 读取 N 个字符解题思路
  • deep forest安装及使用教程
  • 强大的PDF编辑工具,操作方便 ,长久使用
  • 第1天:认识RNN及RNN初步实验(预测下一个数字)
  • 【C盘瘦身】Docker安装目录占用C盘过大,一键移动给C盘瘦身
  • 大数据-275 Spark MLib - 基础介绍 机器学习算法 集成学习 随机森林 Bagging Boosting
  • 8、电解电容—数据手册解读