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 |
| 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 { |
| |
| |
| |
| |
| |
| |
| |
| FileOutputStream fos; |
| fos = new FileOutputStream("code\\2.txt"); |
| |
| |
| fos.write(100); |
| 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 字节流写数据实现追加
| 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 { |
| |
| |
| |
| |
| |
| |
| |
| FileOutputStream fos; |
| fos = new FileOutputStream("code\\2.txt",true); |
| |
| |
| fos.write("\r\n".getBytes()); |
| fos.write(100); |
| 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 { |
| if(fos!=null){ |
| try { |
| |
| fos.close(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
| } |
| |

1.6 字节流复制文件
| 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(); |
| |
| 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; |
| |
| length=fis.read(bt); |
| while(length!=-1){ |
| fos.write(bt,0,length); |
| length=fis.read(bt); |
| } |
| |
| fis.close(); |
| fos.close(); |
| } |
| } |
| |
- 效果图

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!