Java之IO流详解(二)——字节流
admin
2024-02-21 10:22:26
0

一、IO流

  1. IO流概述

(1)IO:Input读取数据(输入)、Output写数据(输出)。
(2)流:一种抽象概念,是对数据传输的总称,数据在设备间的传输称为流,流的本质是数据传输,IO流就是用来处理设备间数据传输问题的。
(3)常见应用:文件的上传、下载、复杂等

  1. IO流的分类

(1)根据数据流向分类:
输入流:读数据,将硬盘中的数据读取到内存中;
输出流:写数据,将程序中的数据写入到硬盘中。

(2)根据数据类型分类:
字节流:字节输入流、字节输出流;
字符流:字符输入流、字符输出流。

(3)IO流应用场景
纯文本文件,优先使用字符流;
图片、视频、音频等二进制文件,优先使用字节流;
不确定文件类型,优先使用字节流,因为字节流是万能的流。

二、字节流写入数据

  1. 概述

1、InputStream:这是个抽象类,是字节输入流的所有类的超类。
2、OutputStream:这是个抽象类,是字节输出流的所有类的超类。
3、FileOutputStream:文件输出流,用于将数据写入FIle。
4、FileOutputStream:创建文件输出流以指定的名称写入文件。

  1. 写入方法
方法说明
void write(int b)将指定的字节写入此文件输出流,一次写一个字节数据
void write(byte[] b)将b.length字节从指定的字节数组写入此文件输出流,一次写一个字节数据
void write(byte[] b, int off, int len)将len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流,一次写一个字节数组的部分数据

eg:

public static void main(String[] args) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");//void write(int b) 将指定的字节写入此文件输出流,一次写一个字节数据fileOutputStream.write(97);fileOutputStream.close();
}
public static void main(String[] args) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");//void write(byte[] b) 将b.length字节从指定的字节数组写入此文件输出流,一次写一个字节数据byte[] bytes = {97,98,99,100};fileOutputStream.write(bytes);fileOutputStream.close();
}
public static void main(String[] args) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");//void write(byte[] b, int off, int len) 将len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流,一次写一个字节数组的部分数据byte[] bytes = {97,98,99,100};fileOutputStream.write(bytes,1,bytes.length-1);fileOutputStream.close();
}
public static void main(String[] args) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");String str = "chenzeyuhaohaoxiexitiantianxiangshang";fileOutputStream.write(str.getBytes());fileOutputStream.close();
}
  1. 写入数据换行、追加写数据

(1)字节流写入数据换行:

windows系统的换行符:\r\n
linux系统的换行符:\n
mac系统的换行符:\r

eg:

//写入数据换行
public static void main(String[] args) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");String str = "chenzeyu";for (int i=0; i<10; i++){fileOutputStream.write(str.getBytes());fileOutputStream.write("\r\n".getBytes());}fileOutputStream.close();
}

(2)字节流写入数据通过new FileOutputStream(new File(“C:\Users\Administrator\Desktop\test\test01\test.txt”),true);表示追加写入数据,如果第二个参数为true,则表示字节流写入文件的末尾。

eg:

//追加写数据
public static void main(String[] args) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt",true);String str = "qizekj";for (int i=0; i<10; i++){fileOutputStream.write(str.getBytes());fileOutputStream.write("\r\n".getBytes());}fileOutputStream.close();
}
  1. 字节流写入数据异常的处理
    在异常处理时,是在finally中执行close方法来释放资源。
    eg:
public static void main(String[] args) {FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");String str = "qizekj";for (int i=0; i<10; i++){fileOutputStream.write(str.getBytes());}} catch (IOException e) {e.printStackTrace();}  finally {if(fileOutputStream != null){try {//释放资源fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}

三、字节流读取数据

  1. 概述

1、FileInputStream:从文件中获取输入字节。
2、FileInputStream:通过打开与实际文件的连接来创建一个FileInputStream,该文件由路径名name命令。

  1. 常用方法
方法说明
int read()从该输入流读取一个字节数据,返回值为 -1 时说明文件读取完毕

eg:

public static void main(String[] args) {FileInputStream fileInputStream = null;try {fileInputStream = new FileInputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test\\\\test01\\\\test.txt");int read;while ((read = fileInputStream.read()) != -1){System.out.print((char) read);}} catch (IOException e) {e.printStackTrace();}finally {if(fileInputStream != null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}
  1. 字节流以字节数组的读取数据

eg:

public static void main(String[] args) {FileInputStream fileInputStream = null;try {fileInputStream = new FileInputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test\\\\test01\\\\test.txt");byte[] bytes = new byte[1024];int read;while ((read = fileInputStream.read(bytes)) != -1){System.out.print(new String(bytes,0, read));}} catch (IOException e) {e.printStackTrace();}finally {if(fileInputStream != null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}

四、字节流复制数据

  1. 复制文本数据

步骤:
(1)使用FileInputStream把文件内容读到程序内存中;
(2)使用FileOutputStream把内容写入到另一个文件中。
eg:

public static void main(String[] args) {FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test02\\test.txt");int read;while ((read = fileInputStream.read())!= -1){fileOutputStream.write(read);}} catch (IOException e) {e.printStackTrace();}finally {if(fileInputStream != null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if(fileOutputStream != null){try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}
  1. 字节流复制图片

eg:

public static void main(String[] args) {FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.jpg");fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test02\\test.jpg");byte[] bytes = new byte[1024];int read;while ((read = fileInputStream.read(bytes))!= -1){fileOutputStream.write(bytes,0, read);}} catch (IOException e) {e.printStackTrace();}finally {if(fileInputStream != null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if(fileOutputStream != null){try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}

五、字节缓冲流

1、使用字节流一个字节一个字节读取或者写入数据,会频繁的发送系统内核调用,所以效率非常低。这时可以使用字节缓冲流,缓冲区是一个内存区域的概念,类似于以“块”的形式写入或者读取数据,减少系统调用频率。
2、BufferedInputStream(InputStream in):字节缓冲输入流。
3、BufferedOutputStream(OutputStream out):字节缓冲输出流。
4、字节缓冲流的缓冲区大小默认是8K,即8192字节。

public static void main(String[] args) {//字节缓冲流写数据BufferedOutputStream bufferedOutputStream = null;try {bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test\\\\test01\\\\test.txt"));bufferedOutputStream.write("hao hao xue xi tian tian xiang shang".getBytes());} catch (IOException e) {e.printStackTrace();}finally {if(bufferedOutputStream != null){try {bufferedOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}//字节缓冲流读数据BufferedInputStream bufferedInputStream = null;try {bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test\\\\test01\\\\test.txt"));int length;byte[] bytes = new byte[1024];while ((length = bufferedInputStream.read(bytes))!= -1){System.out.println(new String(bytes, 0, length));}} catch (IOException e) {e.printStackTrace();}finally {if(bufferedInputStream != null){try {bufferedInputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}
  • 字节缓冲流复制视频
    eg:
public static void main(String[] args) {BufferedInputStream bufferedInputStream = null;BufferedOutputStream bufferedOutputStream = null;try {bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\Wang Wang Team.qlv"));bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test02\\Wang Wang Team.qlv"));byte[] bytes = new byte[1024];int length;while ((length = bufferedInputStream.read(bytes)) != -1){bufferedOutputStream.write(bytes,0, length);}} catch (IOException e) {e.printStackTrace();}finally {if(bufferedInputStream != null){try {bufferedInputStream.close();} catch (IOException e) {e.printStackTrace();}}if(bufferedOutputStream != null){try {bufferedOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}

六、编码与解码、乱码

  • 汉字是GBK编码,则占用2个字节; 汉字是UTF-8编码,则占用3个字节。
  • 当字节流读取存储中文数据时可能会乱码,这时可以用字符流读取存储。 字符流=字节流+编码表。
  • 编码和解码使用的码表一定要一致,否则会导致乱码。
  • 编码表(ASC II)就是生活中的字符和计算机二进制的对照关系表。 如a -> 97。
  • Unicode:世界计算机协会制定通用的编码表,2个字节表示一个字符。
  • UTF-8:Unicode编码表的升级版,汉字基本都是3个字节。
  • GB2312:识别数千个中文。
  • GBK:识别2万多中文,2个字节表示一个字符。
  • GB18030:GBK的升级版。
  • ISO8859-1:欧洲制定的编码表。兼容ASCII。多了一些欧洲的语言,它用1个字节的全部来表示数据。没有未知字符的!。

编码:

方法说明
byte[] getBytes()使用平台的默认字符集将此String编码为byte数组
byte[] getBytes(String charsetName)使用指定的字符集将此String编码为byte数组

解码:

方法说明
String(byte[] bytes)使用平台的默认字符集解码指定的byte数组,构造一个新的String
String(byte[] bytes, String charsetName)使用指定的字符集解码指定的byte数组,构造一个新的String

eg:

public static void main(String[] args) {FileOutputStream fileOutputStream = null;FileInputStream fileInputStream = null;try {String str = "好好学习,天天向上";byte[] bytes = str.getBytes("UTF-8");fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");fileOutputStream.write(bytes);fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");byte[] bytes2 = new byte[1024];int length;while ((length = fileInputStream.read(bytes2)) != -1){System.out.println(new String(bytes2,0, length, "UTF-8"));}} catch (IOException e) {e.printStackTrace();}finally {if(fileInputStream != null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}

上一篇:MVCC 脏读理解

下一篇:Python模块和包

相关内容

热门资讯

安卓7.0系统速度咋样,速度与... 你有没有发现,自从手机更新到安卓7.0系统后,感觉整个手机都焕然一新了呢?今天,就让我来给你详细聊聊...
安卓文件系统隔离,Androi... 你知道吗?在安卓的世界里,有一个神奇的小秘密,那就是安卓文件系统隔离。听起来是不是有点高大上?别急,...
电脑板安卓系统下载,轻松实现多... 你有没有想过,你的电脑板突然间变得如此强大,竟然能运行安卓系统?没错,这就是科技的魅力!今天,就让我...
安卓系统双开app排行,热门双... 安卓系统双开App排行大揭秘在数字化时代,手机已经成为我们生活中不可或缺的一部分。而安卓系统,作为全...
安卓原生系统谁在开发,谷歌主导... 你有没有想过,那个陪伴你每天刷抖音、玩游戏、办公的安卓系统,究竟是谁在背后默默开发呢?今天,就让我带...
vive属于安卓系统吗,揭秘V... 你有没有想过,那个让人眼前一亮的VR设备Vive,它到底是不是安卓系统的呢?这个问题,估计不少对VR...
ios系统和安卓系统传递文件,... 你有没有想过,当你把一张照片或者一个文件从你的手机传给朋友时,背后其实是一个复杂的系统在默默工作呢?...
安卓刷鸿蒙系统工具,一招解锁全... 你有没有想过,让你的安卓手机也来一场华丽的变身?没错,就是那种从安卓系统切换到鸿蒙系统的神奇之旅。今...
安卓系统看不到系统文件,为何无... 你是不是也遇到了这样的烦恼?手机里明明有好多文件,可就是找不到它们的小身影,安卓系统竟然看不到系统文...
安卓系统升级模式,迭代创新与用... 你知道吗?安卓系统升级模式,这可是个让人又爱又恨的话题。每次手机收到升级通知,心里既期待又紧张,就像...
安装安卓虚拟系统教程,安卓虚拟... 你有没有想过在电脑上也能体验安卓系统的乐趣呢?没错,就是那种随时随地都能玩手机游戏、刷抖音的感觉,现...
叶茂然安卓系统下载,畅享智能生... 你有没有听说最近叶茂然安卓系统下载成了热门话题?没错,就是那个叶茂然安卓系统!今天,我就要带你全方位...
安卓子系统下载地址,深度解析下... 你有没有想过,你的安卓手机里那些神秘的子系统其实也可以下载下来,自己动手安装呢?没错,今天就要来给你...
安卓手机系统铃声替换,唤醒你的... 你有没有发现,每次手机响个不停,是不是总想给它换个铃声,让它听起来更个性、更有范儿?没错,安卓手机系...
登陆系统和安卓系统区别,差异解... 你有没有想过,为什么你的手机里装了那么多应用,却还是觉得登陆系统有点儿麻烦呢?今天,就让我带你一起探...
安卓系统手表壁纸华为,科技美学... 你有没有发现,最近安卓系统手表的江湖风头正劲呢?尤其是华为家的那些宝贝,简直让人爱不释手。今天,就让...
安卓系统闹钟组件设置,轻松打造... 你有没有发现,每天早上闹钟响起的时候,是不是总感觉时间过得飞快,一转眼又是一个新的一天呢?今天,就让...
安卓系统的几大组件,组件架构与... 你有没有发现,你的安卓手机里藏着许多神奇的“小精灵”呢?它们默默无闻地工作,让你的手机变得如此强大和...
安卓系统关闭app流量,轻松关... 手机里的APP们是不是有时候让你觉得流量消耗得有点儿太快了呢?别急,今天就来教你几招,让你的安卓手机...
安卓系统无尽之海,安卓系统中的... 安卓系统,无尽之海中的航行者想象你正站在一望无际的海洋边,海风轻拂,波光粼粼。这片海洋,深邃而神秘,...