《从零开始的Java世界》10File类与IO流

devtools/2024/10/18 12:34:37/

《从零开始的Java世界》系列主要讲解Javase部分,从最简单的程序设计到面向对象编程,再到异常处理、常用API的使用,最后到注解、反射,涵盖Java基础所需的所有知识点。学习者应该从学会如何使用,到知道其实现原理全方位式地学习,才能为以后框架的学习打下良好的基础。

目录

1.File类

1.1概述

1.2实例化

1.2.1构造器

1.3常用方法

2.IO流

2.1概述

2.2分类

2.3流的API

3.文件流(节点流)

3.1FileReader

3.2FileWriter

3.3FileInputStream及FileOutputStream

4.处理流

4.1缓冲流

4.1.1实现步骤

4.1.2实现代码

4.2转换流

4.2.1介绍

4.2.2实现

4.3对象流

4.3.1说明

4.3.2对象的序列化机制

4.3.3使用

序列化过程

反序列化过程

​编辑


1.File类

1.1概述

1.2实例化

1.2.1构造器

1.3常用方法

 

2.IO流

2.1概述

2.2分类

2.3流的API

3.文件流(节点流)

3.1FileReader

读写文本文件用字符流

java">@Testpublic void test(){FileReader fr = null;try {//1.创建File类对象,对应一个文件File file = new File("hello.txt");//2.创建输入型的字符流,用于读取数据fr = new FileReader(file);//3.读取数据,显示在控制台上//方式一:
//            int data = fr.read();
//            while (data != -1){
//                System.out.print((char)data);
//                data = fr.read();//方式二:char buffer[] = new char[5];int len = fr.read(buffer);while (len != -1){for(int i = 0; i < len; i++){System.out.print(buffer[i]);}len = fr.read(buffer);}} catch (IOException e) {e.printStackTrace();} finally {//4.关闭流资源try {fr.close();} catch (IOException e) {e.printStackTrace();}}}

3.2FileWriter

java">@Test
public void test2() {FileWriter fw = null;try {//1.创建File对象,指明操作的文件名称File file = new File("info.txt");//2.创建输出流fw = new FileWriter(file);//3.输出过程fw.write("ykx666");} catch (IOException e) {e.printStackTrace();}//4.关闭资源try {fw.close();} catch (IOException e) {e.printStackTrace();}
}

3.3FileInputStream及FileOutputStream

读写非文本文件(如图片、音视频)用字节流

java">@Test
public void test(){FileInputStream fis = null;FileOutputStream fos = null;try {//1.创建相关的File类对象File file1 = new File("sdz.jpg");File file2 = new File("copy_sdz.jpg");//2.创建相关字节流fis = new FileInputStream(file1);fos = new FileOutputStream(file2);//3.数据的输入和输出byte[] buffer = new byte[1024];int len;while((len = fis.read(buffer)) != -1)fos.write(buffer,0,len);System.out.println("复制成功!");}catch (IOException e) {e.printStackTrace();}//4.关闭资源try {fis.close();} catch (IOException e) {e.printStackTrace();}try {fos.close();} catch (IOException e) {e.printStackTrace();}
}

4.处理流

4.1缓冲流

4.1.1实现步骤

4.1.2实现代码

java">/** 使用BufferedInputStream/BufferedOutputStream复制一张图片* @author yangkx* @date 2023-05-22 19:48*/
@Test
public void test(){FileInputStream fis = null;FileOutputStream fos = null;BufferedInputStream bis = null;BufferedOutputStream bos = null;try {//1.创建相关的File类对象File file1 = new File("sdz.jpg");File file2 = new File("copy_sdz2.jpg");//2.创建相关字节流、缓冲流fis = new FileInputStream(file1);fos = new FileOutputStream(file2);bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);//3.数据的输入和输出byte[] buffer = new byte[1024];int len;while((len = bis.read(buffer)) != -1)bos.write(buffer,0,len);System.out.println("复制成功!");}catch (IOException e) {e.printStackTrace();}//4.关闭资源//先关外层try {bis.close();} catch (IOException e) {e.printStackTrace();}try {bos.close();} catch (IOException e) {e.printStackTrace();}try {fis.close();} catch (IOException e) {e.printStackTrace();}try {fos.close();} catch (IOException e) {e.printStackTrace();}
}

4.2转换流

4.2.1介绍

4.2.2实现

4.3对象流

4.3.1说明

4.3.2对象的序列化机制

4.3.3使用

序列化过程

反序列化过程

内容来源于尚硅谷javase课程的ppt,仅作为学习笔记参考


http://www.ppmy.cn/devtools/14197.html

相关文章

Hadoop - 安装

文章目录 关于 Hadoop架构变迁 1.0 --> 2.0 --> 3.0 安装配置安装配置环境变量配置core-site.xmlhdfs-site.xmlmapped-site.xmlyarn-site.xml配置 hadoop-env 启动/停止 Hadoop 服务查看 hdfs report 关于 Hadoop The Apache™ Hadoop project develops open-source soft…

数字信号处理操作教程_音频解码:3-8 G711A音频解码实验

一、实验目的 学习G711音频的格式和G711A音频解码的原理&#xff0c;并实现将BIT格式解码为PCM格式。 二、实验原理 G711 G711是国际电信联盟订定出来的一套语音压缩标准&#xff0c;主要用于电话。它主要用脉冲编码调制对音频采样&#xff0c;采样率为8k每秒。它利用一个 …

python根据一定概率判断是否选择

&#x1f47d;发现宝藏 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。【点击进入巨牛的人工智能学习网站】。 Python中基于概率进行选择的方法 在编程中&#xff0c;我们经常会遇到需要根据一定的概率来…

PHP定期给自己网站目录做个特征镜像供快速对比

效果图 上代码&#xff1a; <style> h1{font-size:24px;line-height:180%;font-weight:600;margin:1px 2px;color:#0180cf;} h2{font-size:20px;line-height:140%;font-weight:600;margin:2px 4px;color:green;} h3{font-size:16px;line-height:140%;font-weight:600;m…

Docker NetWork (网络)

Docker 为什么需要网络管理 容器的网络默认与宿主机及其他容器都是相互隔离的&#xff0c;但同时我们也要考虑下面的一些问题&#xff0c; 比如 多个容器之间是如何通信的容器和宿主机是如何通信的容器和外界主机是如何通信的容器中要运行一些网络应用(如 nginx、web 应用、数…

LabVIEW专栏八、类

该章目的是可以开发仪器类。 一、类的概述 一般来说类有三大特性&#xff0c;封装&#xff0c;继承和多态。 在实际项目中&#xff0c;最主要是继承和多态&#xff0c;要搞清楚这两者的概念和在LabVIEW中是怎样应用的。在LabVIEW中&#xff0c;面向对象编程用到的就是LabVIE…

vue keepAlive的使用

一、了解keepAlive 1.1 Vue 中的 keep-alive 是什么? keep-alive 是 Vue.js 的一个内置组件,它用于缓存不活动的组件实例,而不是销毁它们。在 Vue 应用中,当组件被包含在 keep-alive 组件中时,该组件在切换时不会被销毁,而是被保存在一个内存中,这可以显著提升大型应用…

Linux权限敏感文件 | 误操作chmod -R 777 /*

一、【写在前面】 最近笔者跳槽&#xff0c;有一段时间没写博客&#xff0c;最近会把这个博客更新起来&#xff0c;一是作为本人的技术总结&#xff0c;二是分享问题。 初学者经常会认为只有更改了文件才会导致系统不正常&#xff0c;但在Linux中更改权限也会导致很多奇奇怪怪…