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

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();}

相关文章:

  • SQLSERVER数据库表分区学习(未在项目上使用)
  • 地信GIS专业关于学习、考研、就业方面的一些问题答疑
  • HCIP-AI培养计划,成为新时代AI解决方案架构高级工程师
  • 【Dify学习笔记】:dify通过ollama加载DeepSeek-R1-32B模型无法加载!终于解决了!!
  • DL00786-基于RTDETR的水稻病害检测含完整数据集
  • C++函数封装和绑定
  • JWT了解
  • 有了CodeBuddy,10分钟上线MBTI测试网站
  • PiliPlus 非常好用的开源软件第三方B站哔哩哔哩 v1.1.3.35
  • upload-labs通关笔记-第18关文件上传之条件竞争
  • 文件操作和IO-3 文件内容的读写
  • QScrollArea内容增加后自动跳到底部
  • Python生成物理引擎的简单知识图谱
  • JavaScript 垃圾回收与内存泄漏
  • JavaScript中从原数组中删除某个元素
  • STM32 CAN CANAerospace
  • 使用 Docker 搭建 PyWPS 2.0 服务全流程详解
  • matIo库及.mat数据格式介绍
  • AI智能分析网关V4区域入侵检测算法:全功能覆盖,多场景守护安防安全
  • 【技术追踪】ADDP:通过交替去噪扩散过程学习用于图像识别和生成的通用表示(ICLR-2024)
  • 网站url/上海网络推广排名公司
  • 绍兴网站建设推广/搜索引擎营销的优缺点
  • 微信商城网站哪家做的好/活动营销
  • 国际业务网站有哪些/现在最好的营销方式
  • 太原网站建设方案维护/上海比较好的seo公司
  • 沈阳三好街做网站公司/小时seo百度关键词点击器