Java io流(一)

java IO流

1. io流

1.1 io流概述

  • 根据流向
    • 输出流:写数据
    • 输入流:读数据
  • 根据类型
    • 字节流:二进制文件(图片,视频,音频)(万能流)
    • 字符流:纯文本

1.2 字节流写数据

  • 字节输出流(文件)
    • FileOutputStream(String name):创建文件输出流以指定的名称写入(OutputStream 是基类)
  • 字节输入流
    • FileInputStream(String name):创建文件输入流对象。
  • 写数据的方法
方法名说明
void write(int b)一次一个字节的写
void write(byte[]b)一次写入一个字节数组
void write(byte[] b,int off,int len)写入截取的字节数组,开始off,长度len
  • 读数据的方法
方法名说明
read()查阅jdk文档
  • 示例
package first;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo6 {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
/*
做了3件事情
1.创建了2.txt文件
2、创建了输出字节流对象
3.该对象指向创建的文件
*/
FileOutputStream fos;
fos = new FileOutputStream("code\\2.txt");
// 将指定的字节写入字节输出流
fos.write(100);//d的acall码
fos.write(55);
// 写入一个字节数组
byte[] bytes = "abcde".getBytes();
fos.write(bytes,1,3);
// 关闭输出字节流并释放与该字节流相关的所有资源
fos.close();
}
}
  • 效果图

在这里插入图片描述

1.3 字节流写数据如何实现换行

  • windows:"\r\n.getBytes()"
  • linux:"\n.getBytes()"
  • mac:"\r.getBytes()"

1.4 字节流写数据实现追加

  • 创建对象时 ,设置第二个参数为true
fos = new FileOutputStream("code\\2.txt",true);
  • 结果:原有内容不会被覆盖
package first;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo6 {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
/*
做了3件事情
1.创建了2.txt文件
2、创建了输出字节流对象
3.该对象指向创建的文件
*/
FileOutputStream fos;
fos = new FileOutputStream("code\\2.txt",true);
// 将指定的字节写入字节输出流
fos.write("\r\n".getBytes());
fos.write(100);//d的acall码
fos.write(55);
// 写入一个字节数组
byte[] bytes = "abcde".getBytes();
fos.write(bytes,1,3);
fos.write("\r\n".getBytes());
// 关闭输出字节流并释放与该字节流相关的所有资源
fos.close();
}
}
  • 效果图

在这里插入图片描述

1.5 字节流添加异常处理程序

package first;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo7 {
public static void main(String[] args) {
FileOutputStream fos=null;
// 设置异常处理程序
try {
fos=new FileOutputStream("code\\2.txt",true);
fos.write("bytes".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
// finally 部分代码一定会执行,除非 jvm关闭
}finally {
if(fos!=null){
try {
// 释放资源
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
  • 效果图

在这里插入图片描述

1.6 字节流复制文件

  • 将2.txt的文件复制到3.txt
package first;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo8 {
public static void main(String[] args) throws IOException {
// 创建输入流对象
FileInputStream fis=new FileInputStream("code\\2.txt");
FileOutputStream fos = new FileOutputStream("code\\3.txt");
// 获取文件字节
int bt;
bt=fis.read();
// 如果字节不为-1,继续读,获取的数据输出到3.txt
while(bt!=-1){
fos.write(bt);
bt= fis.read();
}
}
}
  • 效果图

在这里插入图片描述

1.7 字节流复制图片

  • 将一张图片复制到1.png,使用字节数组的方式读取
package first;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo9 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\86183\\Desktop\\java笔记\\day08\\QQ截图20220225171952.png");
FileOutputStream fos = new FileOutputStream("code\\1.png");
// 定义一个字节数组
byte bt[]=new byte[1024];
int length;
// 获取读取的字节长度,bt.length
length=fis.read(bt);
while(length!=-1){
fos.write(bt,0,length);
length=fis.read(bt);
}
fis.close();
fos.close();
}
}
  • 效果图
    在这里插入图片描述
posted @   凌歆  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示