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

相关内容

热门资讯

电视安卓系统哪个品牌好,哪家品... 你有没有想过,家里的电视是不是该升级换代了呢?现在市面上电视品牌琳琅满目,各种操作系统也是让人眼花缭...
安卓会员管理系统怎么用,提升服... 你有没有想过,手机里那些你爱不释手的APP,背后其实有个强大的会员管理系统在默默支持呢?没错,就是那...
安卓系统软件使用技巧,解锁软件... 你有没有发现,用安卓手机的时候,总有一些小技巧能让你玩得更溜?别小看了这些小细节,它们可是能让你的手...
安卓系统提示音替换 你知道吗?手机里那个时不时响起的提示音,有时候真的能让人心情大好,有时候又让人抓狂不已。今天,就让我...
安卓开机不了系统更新 手机突然开不了机,系统更新还卡在那里,这可真是让人头疼的问题啊!你是不是也遇到了这种情况?别急,今天...
安卓系统中微信视频,安卓系统下... 你有没有发现,现在用手机聊天,视频通话简直成了标配!尤其是咱们安卓系统的小伙伴们,微信视频功能更是用...
安卓系统是服务器,服务器端的智... 你知道吗?在科技的世界里,安卓系统可是个超级明星呢!它不仅仅是个手机操作系统,竟然还能成为服务器的得...
pc电脑安卓系统下载软件,轻松... 你有没有想过,你的PC电脑上安装了安卓系统,是不是瞬间觉得世界都大不一样了呢?没错,就是那种“一机在...
电影院购票系统安卓,便捷观影新... 你有没有想过,在繁忙的生活中,一部好电影就像是一剂强心针,能瞬间让你放松心情?而我今天要和你分享的,...
安卓系统可以写程序? 你有没有想过,安卓系统竟然也能写程序呢?没错,你没听错!这个我们日常使用的智能手机操作系统,竟然有着...
安卓系统架构书籍推荐,权威书籍... 你有没有想过,想要深入了解安卓系统架构,却不知道从何下手?别急,今天我就要给你推荐几本超级实用的书籍...
安卓系统看到的炸弹,技术解析与... 安卓系统看到的炸弹——揭秘手机中的隐形威胁在数字化时代,智能手机已经成为我们生活中不可或缺的一部分。...
鸿蒙系统有安卓文件,畅享多平台... 你知道吗?最近在科技圈里,有个大新闻可是闹得沸沸扬扬的,那就是鸿蒙系统竟然有了安卓文件!是不是觉得有...
宝马安卓车机系统切换,驾驭未来... 你有没有发现,现在的汽车越来越智能了?尤其是那些豪华品牌,比如宝马,它们的内饰里那个大屏幕,简直就像...
p30退回安卓系统 你有没有听说最近P30的用户们都在忙活一件大事?没错,就是他们的手机要退回安卓系统啦!这可不是一个简...
oppoa57安卓原生系统,原... 你有没有发现,最近OPPO A57这款手机在安卓原生系统上的表现真是让人眼前一亮呢?今天,就让我带你...
安卓系统输入法联想,安卓系统输... 你有没有发现,手机上的输入法真的是个神奇的小助手呢?尤其是安卓系统的输入法,简直就是智能生活的点睛之...
怎么进入安卓刷机系统,安卓刷机... 亲爱的手机控们,你是否曾对安卓手机的刷机系统充满好奇?想要解锁手机潜能,体验全新的系统魅力?别急,今...
安卓系统程序有病毒 你知道吗?在这个数字化时代,手机已经成了我们生活中不可或缺的好伙伴。但是,你知道吗?即使是安卓系统,...
奥迪中控安卓系统下载,畅享智能... 你有没有发现,现在汽车的中控系统越来越智能了?尤其是奥迪这种豪华品牌,他们的中控系统简直就是科技与艺...