java中IO流的操作
创始人
2025-05-30 03:22:55

对于java中io流的一些操作和类进行总结

io流的分类:

 字节流:用来处理二进制文件

字符流:用来处理文本数据

文件io流:

字节流:

InputStream的子类:

FileInputStream类:

读的方式:read

  • 一个字节的读取:
public class test_FileInputStream {public static void main(String[] args) {}@Testpublic void test_FileInputStream() throws IOException {String filepath="D:\\网盘文件\\hello.txt";FileInputStream fileInputStream=null;int read_data=0;//读取文件字节数try {fileInputStream=new FileInputStream(filepath);//read读取的字节数:如果读取失败的话就直接返回-1while ((read_data=fileInputStream.read())!=-1){System.out.print((char) read_data);}}catch (Exception e){e.printStackTrace();}finally {fileInputStream.close();}}
}
  • 使用byte数组来读取:
public class test_FileInputStream02 {public static void main(String[] args) {}@Testpublic void test_FileInputStream02() throws IOException {String filepath="d:\\网盘文件\\hello.txt";byte[] buf=new byte[10];FileInputStream fileInputStream=null;int read_len=0;//读取文件字节数try {fileInputStream=new FileInputStream(filepath);//按位数组读取元素:while ((read_len=fileInputStream.read(buf))!=-1) {System.out.print(new String(buf,0,read_len));}}catch (Exception e){e.printStackTrace();}finally {fileInputStream.close();}}
}

OutputStream子类:

FileOuputStream类:

写入的方式:

  •  写入文件内容:
public class test_FileOutputStream {public static void main(String[] args) {}@Testpublic void fileOutputStream() throws IOException {String filename="D:\\网盘文件\\hello.txt";FileOutputStream fileOutputStream=null;//写入文件的内容:String str="hello world";try {//写入文件使用追加的方式:fileOutputStream=new FileOutputStream(filename,true);fileOutputStream.write(str.getBytes());System.out.println("写入文件成功!");}catch (Exception e){e.printStackTrace();}finally {fileOutputStream.close();}}
}

文件拷贝:

public class test_拷贝文件 {public static void main(String[] args) {}@Testpublic void copyfile(){String srcfile="d:\\file.txt";String desfile="d:\\网盘文件\\file.txt";FileInputStream fileInputStream=null;FileOutputStream fileOutputStream=null;int read_len=0;byte [] buf=new byte[1024];try {fileInputStream=new FileInputStream(srcfile);fileOutputStream=new FileOutputStream(desfile);while ((read_len=fileInputStream.read(buf))!=-1){//使用位数组的方式拷贝文件:fileOutputStream.write(buf,0,read_len);System.out.println("拷贝文件完成!");}}catch (Exception e){e.printStackTrace();}finally {}}
}

字符流:Reader 和Writer

Reader:实现的子类

FileReader类:

 public class test_FileReader {//Reader的实现子类;/***** 使用读取单个字符的方式* ***/@Testpublic void file_reader01() throws IOException {String filepath="d:\\file.txt";FileReader fileReader =null;int read_len=0;try {fileReader=new FileReader(filepath);System.out.print("读取字符为:");while ((read_len=fileReader.read())!=-1){System.out.print((char)read_len);}}catch(Exception e){e.printStackTrace();}finally {fileReader.close();}}/***** 使用字符数组的方式进行读取文件* **/@Testpublic void file_reader02() throws IOException {String filepath="d:\\file.txt";FileReader fileReader =null;//使用字符数组的方式进行读char [] chars=new char[20];int read_data=0;try{fileReader=new FileReader(filepath);while ((read_data=fileReader.read(chars))!=-1){System.out.print(new String(chars,0,read_data));}} catch (Exception e) {e.printStackTrace();} finally {fileReader.close();}}}

Writer:实现的子类

Filewriter类:

public class test_FileWriter {//Writer的实现子类@Testpublic void file_writer01() throws IOException {String filepath="d:\\file.txt";FileWriter fileWriter=null;//        char [] str={'a','b','c'};try{fileWriter=new FileWriter(filepath,true); //以追加的方式写入;
//            fileWriter.write(str); 以字符数组的方式写入fileWriter.write("你好java"); //以字符串的方式写入System.out.println("写入文件成功");}catch (Exception e){e.printStackTrace();}finally {fileWriter.close();}}}

节点流和处理流:

节点流:

从一个特定的数据源(比如是FileReader和FilerWriter)进行读写文件,比如说文件。直接跟数据源相连。

处理流:(BufferedReader、BufferedWriter、BufferedInputStream、BufferedOutputStream)继承了Reader和Writer类

对某一个节点流进行包装,可以封装任意一个节点流,使处理读写更加方便。

字符处理流:

BufferReader:(用来处理字符)

public class test_BufferReader {public static void main(String[] args) throws Exception{String filepath="d:\\file.txt";String line=null; //按行读取BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath));//readLine() 是按一行来读取的;// 当返回null 表示读取完毕;while ((line=bufferedReader.readLine()) !=null){System.out.println(line);}bufferedReader.close();}
}

BufferWriter:(用来处理字符)

public class test_BufferWriter {public static void main(String[] args) throws IOException {String filepath="d:\\file.txt";//new FileWriter(filepath,true) //以追加的方式:BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filepath,true));bufferedWriter.write("要努力变强啊!");bufferedWriter.newLine(); //进行换行操作;System.out.println("写入文件成功!");bufferedWriter.write("那是肯定的");//关闭流的操作:bufferedWriter.close();}
}

字节处理流(用来处理二进制数据):

BufferedInuputStream:(用来处理字节,只要给它传入一个InputStream的节点流)

BufferedOutputStream:(用来处理字节,只要给它传入一个OutputStream节点流)

public class test_BinputStream_BoutputStream {public static void main(String[] args) throws IOException {String srcpath="d:\\test.jpg";String despath="d:\\test01.jpg";int read_len=0;//读取数据的长度://字节处理流,就是将其他的节点字节处理进行包装,然后操作的BufferedInputStream bufferedInputStream = null;BufferedOutputStream bufferedOutputStream=null;byte[] buf=new byte[1024];//以字节数组的方式读取:try {//新建处理流的对象:bufferedInputStream=new BufferedInputStream(new FileInputStream(srcpath));bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(despath));//读取文件:while ((read_len=bufferedInputStream.read(buf))!=-1){//写入文件bufferedOutputStream.write(buf,0,read_len);}System.out.println("读取和写入二进制图片成功!");}catch (Exception e){e.printStackTrace();}finally {bufferedInputStream.close();bufferedOutputStream.close();}}
}

相关内容

热门资讯

在pycharm中使用chat... 目录 前言 一、插件安装 二、使用步骤 总结 前言 ChatGPT是目前最强大的AI,...
Codeforces Roun... G. Subsequence Addition 标签 规律、数学 链接 传送门、 结论 当前前缀和小...
算法leetcode|42. ... 文章目录42. 接雨水:样例 1:样例 2:提示ÿ...
【项目设计】负载均衡在线OJ 🎇Linux: 博客主页:一起去看日落吗分享博主的在L...
Java开发 | 重写 | 多... 前言 大家好,我是程序猿爱打拳,今天给大家带来的是面向对象之封装继承多...
【Unity】NavMesh ... 在Unity中,可以使用自带导航系统(Navigation System...
由文心一言发布会引发的思考,聊... 文章目录前言一. 文心一言的试用1.1 文心一言发布会1.2 文心一言图片生成功能试用1.3 文心一...
java线程之Thread类的... Thread类的基本用法1. Thread类的构造方法2. Thread的几个常见属性常见属性线程中...
css实现3D弹性按钮以及bo... box-shadow 在实现案例之前先了解css的阴影属性box-shadow,该属性...
【Linux】基础命令大全、实... 个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主&#...
R语言基础教程4:列表和数据框 文章目录列表数据帧表头 R语言系列:1 编程基础💎2 循环语句...
Git基础知识 Git基础知识前言一、Git基本概念1、分布式版本控制系统--Git2、Git配置命令3、Git原理...
【JavaWeb】MySQL 一、数据库的相关概念 1.数据库(DB) ==存储和管...
CPU 是如何执行程序的 代码写了那么多,你知道 a = 1 + 2 这条代码是怎么被 CPU ...
从产品的角度看缓存 文章目录 1. What——什么是缓存?2. Why——为什么需要使用缓存?2.1 什么是用户体验2...
vivado 开发过程中所遇错...  [Synth 8-4556] 开辟的数组内存空间大小问题 [Synth 8-4556] size...
1.4 K8S入门之POD和网... POD 分类 自主式POD控制器管理的POD 容器 每个容器独立存在,有自己的IP地址...
【二】一起算法---队列:ST... 纸上得来终觉浅,绝知此事要躬行。大家好!我是霜淮子,欢迎订...
在使用fastjson中遇到的... 一、在使用fastjson中遇到的问题 导论:最近在写一个JavaFx项目的时候使用...
HJ31 单词倒排 描述 对字符串中的所有单词进行倒排。 说明: 1、构成单词的字符只有26个大写或小写英...
普通插槽、具名插槽、作用域插槽 插槽 插槽就是子组件提供给父组件的占位符,用slot来表示,父组件可以在...
Go语言必知必会100问题-0... 减少代码的嵌套层数 软件开发中的“心智模型”用于描述开发人员在编码时心理活动,每段代码...
CSRF漏洞的概念、利用方式、... CSRF漏洞1.CSRF的概念1.1 什么是CSRF?1.2 基本攻击流程2.CSRF...
基于springboot开发的... 基于springboot开发的学生考勤管理系统 如需更多资料请前往文章底部获取联系方式 系统设计主要...
cocosCreator 之 ... cocosCreator版本: 3.7 简介 cocosCreator的工作流程是通...
vue2中$set的原理_它对... $set的作用背景动态添加属性,不使用$set动态添加属性,使用$set...
3/19考后总结 时间安排 8:30–8:50 读题,T1 感觉是乱搞题,T2 貌似可以二...
【JavaWeb】JDBC 目录 一、JDBC概述 1 JDBC概念 2 JDBC本质 3 JDBC好处 二,J...
python 多任务 一些概念 1.多任务 简单地说,就是同时可以运行多个任务。打个比方,你一...
基于springboot框架实... 基于springboot框架实现自习室预订系统的设计与实现 开发语言:Java 框架...