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

java I/O

 文件字符流

字符流不同于字节,字符流是以一个具体的字符进行读取,因此它只适合读纯文本的文件,如果是其他类型的文件不适用。

字节流;英文1个字节,中文3个字节。 字符流:中英文都是2个字节

    public static void main(String[] args){try (FileReader reader = new FileReader("test.txt")){System.out.println((char) reader.read());}catch (IOException e){e.printStackTrace();}try (FileWriter writer = new FileWriter("test.txt")){writer.write("eeee");}catch (IOException e){e.printStackTrace();}//拷贝try (FileReader reader1 = new FileReader("test.txt");FileWriter writer1 = new FileWriter("test1.txt")){char[] chars = new char[3];int len;while ((len=reader1.read(chars))!= -1){writer1.write(chars,0,len);}}catch (IOException e){e.printStackTrace();}}
}

文件对象

    public static void main(String[] args) throws IOException {//文件对象File file = new File("test.txt");System.out.println(file.exists()); //判断文件是否存在System.out.println(file.getAbsoluteFile());  //获取绝对路径file.createNewFile();  //创建文件file.length();  //获取字节长度File file1=new File("hello/test2");System.out.println(file1.mkdirs()); //创建文件夹}
}

    public static void main(String[] args){File file=new File("Honor(1).mp3");try (FileInputStream in = new FileInputStream("Honor(1).mp3");FileOutputStream out =new FileOutputStream("xxx.mp3")){byte[]bytes=new byte[1024*1024];int len;long total =file.length(),sum=0;while ((len=in.read(bytes))!=-1){out.write(bytes,0,len);sum+=len;System.out.println("文件已拷贝"+(sum*100/total)+"%");}}catch (IOException e){e.printStackTrace();}}/*输出
文件已拷贝10%
文件已拷贝21%
文件已拷贝31%
文件已拷贝42%
文件已拷贝52%
文件已拷贝63%
文件已拷贝73%
文件已拷贝84%
文件已拷贝95%
文件已拷贝100%
*/

缓冲流

当调用mark()之后,输入流会以某种方式保留之后读取的readlimit数量的内容,当读取的数量超过readlimit则之后的内容不会被保留,当调用reset()之后,会使得当前的读取位置回到mark()调用时的位置。

        try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream("test.txt"))){stream.mark(0);System.out.print((char) stream.read());System.out.print((char) stream.read());System.out.println((char) stream.read());stream.reset();System.out.print((char) stream.read());System.out.print((char) stream.read());System.out.print((char) stream.read());System.out.print((char) stream.read());}catch (IOException e){e.printStackTrace();}

转换流

读取的是一个字符串或一个个字符,但是只能往一个OutputStream里输出,但是OutputStream只支持byte类型,如果要往里面写入内容,进行数据转换就会很麻烦,可以使用转换流使之简洁:

        try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("test.txt"))){writer.write("aaabbbbccc");}catch (IOException e){e.printStackTrace();}

同样的,现在只拿到一个InputStream,希望能按照字符否方式读取:

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")))){System.out.println(reader.readLine());}catch (IOException e){e.printStackTrace();}

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

相关文章:

  • 【补题】The 2021 ICPC Asia Nanjing Regional Contest Problem J. Xingqiu’s Joke
  • [Java][Leetcode middle] 6. Z 字形变换
  • TCP与UDP协议全面对比:从原理到应用场景深度解析
  • ROS2 camera_calibration 双目相机标定指令
  • 监控易一体化运维:网络拓扑管理,网络管理高效之道
  • 异常数据的检测
  • 【基础】Windows开发设置入门11:hyper-v虚拟机创建
  • 批处理操作优化思路
  • 使用Pyinstaller打包python,全过程解析【2025最详细】
  • 湖北理元理律师事务所:专业债务优化如何助力负债者重获生活掌控权
  • CS和BS架构
  • 《数据结构笔记一》: 指针、结构体、动态内存分配、算法时间复杂度。
  • Python绘制3D图表
  • PostgreSQL 18 升级新体验:pg_upgrade --swap 极简教程
  • BGE-M3 文本情感分类实战:预训练模型微调,导出ONNX并测试
  • b/s开发 1.0
  • DDS与PLL技术
  • 力扣HOT100之二叉树: 437. 路径总和 III
  • 8天Python从入门到精通【itheima】-29~31
  • dify介绍(优势与作用)
  • 小样本百分比的统计检验
  • AbMole推荐Rapamycin: 自噬、肿瘤、免疫、衰老研究的关键工具
  • 干货分享:90+深度学习开源数据集
  • React 第四十五节 Router 中 useHref() Hook的使用详解及注意事项
  • session、cookie或者jwt 解释一下
  • 十五、Hive 窗口函数
  • java基础(方法)
  • Ubuntu18.04安装ros
  • -40℃到+125℃全温域稳定!车规级晶振如何突破温度极限
  • 27-FreeRTOS的任务管理