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模块和包

相关内容

热门资讯

安卓系统最强定位手机版,最强定... 你有没有想过,在茫茫人海中,如何让你的手机定位功能像侦探一样精准无误?今天,就让我带你一探究竟,揭秘...
安卓运行环境选哪个系统,And... 你有没有想过,你的安卓手机到底是在哪个运行环境下才能发挥出最佳性能呢?这可是个技术活儿,选对了系统,...
zui15系统是安卓系统吗,揭... 亲爱的读者,你是否曾好奇过,那些在手机上运行得风生水起的系统,它们究竟是不是安卓的呢?今天,就让我带...
ios系统和安卓系统权限区别,... 你有没有发现,无论是手机还是平板,我们用的最多的就是那些APP了。而这些APP,它们在手机里可是有着...
荣耀手环6安卓版系统,智能生活... 你有没有注意到,最近你的手腕上是不是多了一抹亮丽的色彩?没错,说的就是荣耀手环6安卓版系统!这款智能...
极品奴隶系统下载安卓版,体验独... 你有没有听说过那个超级火的“极品奴隶系统”安卓版?最近,这款游戏在朋友圈里可是炸开了锅,大家都说它好...
安卓手机苹果系统扣费,揭秘扣费... 你有没有遇到过这种情况?手机里突然多了一笔扣费,而且还是那种你完全没意识到的扣费?尤其是当你用的是安...
安卓系统智能电视刷机,焕新体验 亲爱的电视迷们,你是否曾为你的安卓智能电视的性能所困扰?是不是觉得它运行缓慢,功能受限?别担心,今天...
安卓系统无法安装applica... 最近是不是遇到了安卓系统无法安装application的烦恼?别急,让我来帮你一探究竟,解决这个让人...
怎么取消安卓系统锁屏,解锁锁屏... 手机锁屏功能虽然能保护我们的隐私,但有时候也会让人头疼,比如忘记密码或者想快速查看信息时。那么,怎么...
安卓系统高德怎么下载,轻松获取... 你有没有发现,现在手机上导航软件真是越来越方便了?尤其是安卓系统的用户,高德地图这款神器简直成了出行...
安卓系统的开源部分,开源代码背... 你知道吗?安卓系统,这个在我们手机上无处不在的小家伙,竟然有一部分是开源的!是不是觉得有点神奇?别急...
小米下载安卓13系统,畅享智能... 亲爱的手机控们,你是否已经迫不及待想要体验最新的操作系统呢?没错,我说的就是安卓13系统!而今天,我...
安卓系统如何设置拍月亮,捕捉夜... 月亮,那轮皎洁的夜空明珠,总是让人心生向往。你是否也想用你的安卓手机捕捉到它的美丽瞬间呢?别急,今天...
安卓v8以上系统,探索安卓V8... 你知道吗?最近手机界可是掀起了一股新潮流,那就是安卓V8以上系统。这可不是什么小打小闹,而是实实在在...
安卓系统兼容哪个版本好,哪个版... 你有没有想过,你的安卓手机到底兼容哪个版本的系统最好呢?这可是个技术活儿,得好好研究研究。别急,今天...
安卓平板安装linux桌面系统... 你有没有想过给你的安卓平板来个变身大法?没错,就是给它安装一个Linux桌面系统!想象原本只能刷刷剧...
安卓什么手机系统bug最少,揭... 你有没有发现,用安卓手机的时候,有时候会遇到一些小麻烦,比如系统突然卡顿,或者某个应用突然崩溃,真是...
手机软件安卓下载系统,解锁手机... 你有没有发现,现在的生活越来越离不开手机了?手机里装满了各种各样的软件,让我们的生活变得更加便捷。今...
微软系统和安卓系统的cad软件... 你有没有想过,为什么你的电脑里装的是微软系统,而朋友的手机上却是安卓系统?这背后其实隐藏着一场关于操...