当前位置: 首页 > news >正文

文件字节流

 文件字节流

在Windows下

绝对路径:C://User/test.txt 相对路径:test.txt

文件字节输入流:

InputStream只是⼀个抽象类,要使⽤还需要具体的实现类。关于InputStream的实现类有很多,基 本可以认为不同的输⼊设备都可以对应⼀个InputStream类,我们现在只关⼼从⽂件中读取,所以使 ⽤FileInputStream

当使用完成一个流之后,必须关闭这个流来完成对资源的释放,否则资源会一直被占用:

    public static void main(String[] args) {FileInputStream inputStream = null;try{inputStream=new FileInputStream("test.txt");} catch (FileNotFoundException e) {throw new RuntimeException(e);}finally {if(inputStream!=null){try {inputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}}

简化写法(语法糖):

    public static void main(String[] args) {try (FileInputStream stream=new FileInputStream("test.txt")){System.out.println(stream);}catch (IOException e){e.printStackTrace();}}

英文字母占一个字节,中文占三个字节

    public static void main(String[] args) {try (FileInputStream stream=new FileInputStream("test.txt")){//剩余多少个字节System.out.println(stream.available());//读一个字符int x =stream.read();System.out.println((char) x);//读所有的,前面读取了第一个字母H,此处从后开始读取int i;while ((i=stream.read())!=-1){System.out.print((char) i);}//所有内容读取完毕,后面的都不输出了//分开看方法byte[] bytes =new byte[3];while (stream.read(bytes)!=-1)System.out.println(new String(bytes)); //仅限纯文本变成字符串打印出来byte[] bytes1=new byte[stream.available()];stream.read(bytes1);System.out.println(new String(bytes1));//跳过n个字节读取stream.skip(3);System.out.println((char) stream.read());}catch (IOException e){e.printStackTrace();}}

文件字节输出流:

        //文件字节输出流try(FileOutputStream stream1=new FileOutputStream("test1.txt",true)) { //true表示开启追加模式stream1.write('c');stream1.write("Hello World!".getBytes());stream1.write("Hello World!".getBytes(),3,3);//从3开始写3个长度:lo ;stream1.flush();//建议在最后执行一次刷新操作(强制写入)来保证数据正确写入到硬盘文件当中}catch (IOException e){e.printStackTrace();}

拷贝文件: 

        //拷贝文件try (FileInputStream in =new FileInputStream("test,txt");FileOutputStream out=new FileOutputStream("test2.txt")){int s;while ((s=in.read())!=-1)out.write(s);//如果文件很大,加快拷贝速度:byte[] bytes2=new byte[1024];int len;while ((len=in.read(bytes2))!=-1){out.write(bytes2,0,len);}}catch (IOException e){e.printStackTrace();}

http://www.xdnf.cn/news/530821.html

相关文章:

  • LLM笔记(九)KV缓存(2)
  • RK3568解码1080P视频时遇到系统崩溃内核挂掉的解决方案
  • C语言:在操作系统中,链表有什么应用?
  • 安全强化的Linux
  • RLᵛ_ Better Test-Time Scaling by Unifying LLM Reasoners With Verifiers
  • 【TTS回顾】Bert-VITS2深度解析:融合BERT的多语言语音合成模型
  • 详细总结和讲解redis的基本命令
  • JavaScript 性能优化实战指南
  • Unity3D HUD UI性能优化方案
  • 卓力达手撕垫片:精密制造的创新解决方案与多领域应用
  • Unreal Engine: Windows 下打包 AirSim项目 为 Linux 平台项目
  • 【成品设计】STM32和UCOS-II的项目
  • 软考教材重点内容 信息安全工程师 25章 移动安全 26章 大数据安全
  • Flask 与 Django 服务器部署
  • 【成品设计】基于STM32的的宠物看护系统
  • 论文阅读--Logical quantum processor based on reconfigurable atom arrays
  • ModbusTCP转 Profinet网关:热收缩包装机智能化改造核心方案
  • 深入理解 Redisson 看门狗机制:保障分布式锁自动续期
  • chirpstack v4版本 全流程部署[ubuntu+docker]
  • Linux 移植 Docker 详解
  • LeetCode 925. 长按键入 java题解
  • MIME类型详解及应用案例
  • JVM频繁FullGC:面试通关“三部曲”心法
  • 力扣992做题笔记
  • P2P最佳网络类型
  • YOLO11解决方案之实例分割与跟踪探索
  • 2025.05.01【Barplot】柱状图的多样性绘制
  • 【图像大模型】FLUX.1-dev:深度解析与实战指南
  • 五分钟本地部署大模型
  • stata入门学习笔记——导入数据