Java中IO流详细整合(含案例)
本章主要详细讲解IO流
目录
- 文件/目录的基本操作
- IO流基本知识
- 输入输出流原理
- 输入输出流分类
- 节点流类型
- 处理流类型
- FileInputStream
- FileInputStream的基本方法
- FileInputStream的案例
- FileOutputStream
- FileOutputStream的基本方法
- FileOutputStream的基本方法的案例
- 文件拷贝
- FileReader的案例
- FileWriter的案例
- 处理流讲解
- 缓冲流
- BufferedInputStream/BufferedOutputStream的案例
- BufferedReader/BufferedWriter的案例
- 对象流
- 序列化和反序列化
- ObjectOutputStream的案例
- ObjectInputStream的案例
- 对象流处理事项
- 数据流
- DataInputStream/DataOutputStream的案例
- 转换流
- InputStreamReader的案例
- OutputStreamWriter的案例
- 标准输入输出流
- System.in/System.out的案例
- 打印流
- PrintStream的案例
- PrintWriter的案例
文件/目录的基本操作
- 文章 Java 常用类及其常用方法的使用中的File类有详细讲解
IO流基本知识
输入输出流原理
- 从程序的角度来讲,从 计算机读取到的原始数据肯定都是010101这种形式的,一个字节一个字节地往外读,如果你觉得这样的方法不合适,你再在这根管道的外面再包一层比较强大的管道,这个管道可以帮你把 010101转换成字符串。这样你使用程序读取数据时读到的就不再是010101这种形式的数据了,而 是一些可以看得懂的字符串了。
输入输出流分类
- 按数据流的方向不同可以分为输入流和输出流
- 按照处理数据单位不同可以分为字节流和字符流
- 字节流:最原始的一个流,读出来的数据就是010101这种最底层的数据表示形式,只不过它是按照字节来读的
- 字符流:字符流是一个字符一个字符地往外读取数据
- 按照功能不同可以分为节点流和处理流
- 节点流:可以从一个特定的数据源(节点)读取数据
- 处理流:“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更强大的读写能力
节点流类型
处理流类型
FileInputStream
FileInputStream的基本方法
//读取一个字节并以整数的形式返回(0~255) //如果返回-1就说明已经到了输入流的末尾
int read() throws IOException
//读取一系列字节并存储到一个数组buffer //返回实际读取的字节数,如果读取前已到输入流的末尾,则返回-1
int read(byte[] buffer) throws IOException
//读取length个字节
//并存储到一个字节数组buffer,从length位置开始 //返回实际读取的字节数,如果读取前以到输入流的末尾返回-1.
int read(byte[] buffer,int offset,int length) throws IOException
//关闭流释放内存资源
void close() throws IOException
//跳过n个字节不读,返回实际跳过的字节数
long skip(long n) throws IOException
FileInputStream的案例
"/Users/test/iotest.txt"
Hello Worldpublic class FileInputTest {public static void main(String[] args) {int count =0;byte[] buf=new byte[8];FileInputStream ips=null;try{ips=new FileInputStream("/Users/test/iotest.txt");//使用FileInputStream流来读取有中文的内容时,读出来的是乱码,因为使用//InputStream流里面的read()方法读取内容时是一个字节一个字节地读取的,而一个汉字是占用两个字节的,所以读取出来的汉字无法正确显示。//int read() throws IOException//ReadNoPra(count,ips); //输出://Hello World//int read(byte[] buffer) throws IOException//ReadBuf(buf,count,ips);//输出://Hello Wo//rld//int read(byte[] buffer,int offset,int length) throws IOException//long skip(long n) throws IOExceptionReadBufLen(buf,count,ips);//输出://1//ell} catch (Exception e) {System.out.println("读取失败");} finally {try {if (ips!=null){ips.close();}} catch (IOException e) {e.printStackTrace();}}}static void ReadBufLen(byte[] buf,int count,FileInputStream ips) throws IOException {//跳过一个字节不读System.out.println(ips.skip(1));if ((count=ips.read(buf,0,3))!=-1) {System.out.println(new String(buf, 0, count));}}static void ReadBuf(byte[] buf,int count,FileInputStream ips) throws IOException {while ((count=ips.read(buf))!=-1){System.out.println(new String(buf,0,count));}}static void ReadNoPra(int count,FileInputStream ips) throws IOException {while ((count=ips.read())!=-1){System.out.printf("%c",(char) count);}}
}
FileOutputStream
FileOutputStream的基本方法
//向输出流中写入一个字节数据,该字节数据为参数b的低8位
void write(int b) throws IOException
//将一个字节类型的数组中的数据写入输出流
void write(byte[] b) throws IOException
//将一个字节类型的数组中的从指定位置(off)开始的len个字节写入到输出流
void write(byte[] b,int off,int len) throws IOException
//关闭流释放内存资源
void close() throws IOException
//将输出流中缓冲的数据全部写出到目的地 void flush() throws IOException
FileOutputStream的基本方法的案例
"/Users/test/iotest.txt"
Hellopublic class FileOutputTest {public static void main(String[] args) {int count =0;byte[] buf=new byte[8];FileOutputStream ops=null;try{//若没有这个文件,会自动创建文件并写入数据//如果添加true,那新写入的数据不会覆盖原先数据而是追加在后面ops=new FileOutputStream("/Users/test/iotest.txt",true);//void write(int b) throws IOExceptionops.write(' ');//void write(byte[] b) throws IOExceptionString str="World";ops.write(str.getBytes());//void write(byte[] b,int off,int len) throws IOExceptionops.write(' ');String str2="Love China";ops.write(str2.getBytes(),0,4);} catch (Exception e) {System.out.println("写入失败");} finally {try {if (ops!=null){ops.close();}} catch (IOException e) {e.printStackTrace();}}}
}
//输出结果:
"/Users/test/iotest.txt"
Hello World Love
文件拷贝
"/Users/test"
boji.jpegpublic class FileCopy {public static void main(String[] args) {int count=0;FileInputStream ips=null;FileOutputStream ops=null;byte[] buf=new byte[1024];try{ips=new FileInputStream("/Users/test/boji.jpeg");ops=new FileOutputStream("/Users/test/boji2.jpeg");while ((count=ips.read(buf))!=-1){ops.write(buf,0,count);}System.out.println("success");} catch (Exception e) {System.out.println("fail");} finally {try{if (ips!=null){ips.close();}if (ops!=null){ops.close();}} catch (IOException e) {e.printStackTrace();}}}
}
//输出结果:
"/Users/test"
boji.jpeg
boji2.jpeg
FileReader的案例
"/Users/test/iotest.txt"
Hello World
你好,世界public class FileReaderTest {public static void main(String[] args) {ReadOne();System.out.println();System.out.println("-------");ReadMore();}//一个字符为单位读static void ReadOne(){int count=0;FileReader fr=null;try{fr=new FileReader("/Users/test/iotest.txt");while ((count=fr.read())!=-1){System.out.print((char)count);}} catch (Exception e) {System.out.println("读取失败");}finally {try {if (fr!=null){fr.close();}} catch (IOException e) {e.printStackTrace();}}}//字符数组为单位读static void ReadMore(){int count=0;FileReader fr=null;char[] chars=new char[8];try{fr=new FileReader("/Users/test/iotest.txt");while ((count=fr.read(chars))!=-1){System.out.print(new String(chars,0,count));}} catch (Exception e) {System.out.println("读取失败");}finally {try {if (fr!=null){fr.close();}} catch (IOException e) {e.printStackTrace();}}}
}
//输出结果:
Hello World
你好,世界
-------
Hello World
你好,世界
FileWriter的案例
- 注意,FileWriter使用后,必须关闭(close)或刷新(flush),否则写入不到指定文件。
"/Users/test/iotest.txt"
Hellopublic class FileWriterTest {public static void main(String[] args) {int count=0;FileWriter fw=null;char[] chars={'a','b','c'};try{//后面没有true,表示覆盖fw=new FileWriter("/Users/test/iotest.txt");//写入单个字符fw.write('H');//写入字符数组fw.write(chars);//写入指定字符数组的指定长度fw.write("你好呀".toCharArray(),0,2);//写入字符串fw.write("很高兴认识你");//写入字符串指定部分fw.write("杭州北京",0,2);} catch (Exception e) {System.out.println("写入失败");}finally {try {if (fw!=null){fw.close();}} catch (IOException e) {e.printStackTrace();}}}
}
//输出结果:
"/Users/test/iotest.txt"
Habc你好很高兴认识你杭州
处理流讲解
缓冲流
BufferedInputStream/BufferedOutputStream的案例
- 可以通过上述流操作二进制文件[图片、声音、视频、doc、pdf]
"/Users/test/iotest.txt"public class BufInputTest {public static void main(String[] args) {FileInputStream ips=null;FileOutputStream ops=null;BufferedInputStream bis=null;BufferedOutputStream bos=null;try{ops= new FileOutputStream("/Users/test/iotest.txt");bos=new BufferedOutputStream(ops);bos.write("Hello World".getBytes());bos.flush();ips= new FileInputStream("/Users/test/iotest.txt");bis=new BufferedInputStream(ips);int count=0;System.out.println((char) bis.read());//public void mark(int readlimit)//readlimit 参数告知此输入流在标记位置无效之前允许读取的字节数bis.mark(3);for (int i=0;i<=10&&(count=bis.read())!=-1;i++){System.out.print((char) count);}System.out.println();bis.reset();for (int i=0;i<=10&&(count=bis.read())!=-1;i++){System.out.print((char) count);}} catch (Exception e) {e.printStackTrace();} finally {try {if (bos!=null){bos.close();}if (bis!=null){bis.close();}} catch (IOException e) {e.printStackTrace();}}}
}
//输出结果:
H
ello World
ello World
BufferedReader/BufferedWriter的案例
- 如果通过上述流操作二进制文件[图片、声音、视频、doc、pdf],可能会造成文件损坏
"/Users/test/iotest.txt"public class BufReaderTest {public static void main(String[] args) {BufferedWriter bw=null;BufferedReader br=null;try{bw=new BufferedWriter(new FileWriter("/Users/test/iotest.txt"));String s=null;Random random = new Random();for (int i=0;i<3;i++){//随机生成数字s=String.valueOf(random.nextInt(10));bw.write(s);bw.newLine();}bw.flush();br=new BufferedReader(new FileReader("/Users/test/iotest.txt"));while ((s=br.readLine())!=null){System.out.println(s);}} catch (Exception e) {e.printStackTrace();} finally {try {if (bw!=null){bw.close();}if (br!=null){br.close();}} catch (IOException e) {e.printStackTrace();}}}
}
//输出结果:
7
4
9
对象流
序列化和反序列化
- 序列化就是在保存数据时,保存数据的值和数据类型
- 反序列化就是在恢复数据时,恢复数据的值和数据类型
- 需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:
- Serializable 这是一个标记接口,里面没有方法,推荐使用
- Externalizable 该接口有方法需要实现
ObjectOutputStream的案例
public class ObjOutTest {public static void main(String[] args) {ObjectOutputStream oos=null;try{oos=new ObjectOutputStream(new FileOutputStream("/Users/test/iotest.txt"));//序列化数据到iotest.txtoos.writeInt(100);//自动装箱 int -> Integer (实现了Serializable)oos.writeBoolean(true);oos.writeChar('a');oos.writeDouble(3.14);oos.writeUTF("Love Java");//保存一个dog对象Dog dog=new Dog(6,"小花");oos.writeObject(dog);} catch (Exception e) {e.printStackTrace();} finally {try{if (oos!=null){oos.close();}} catch (IOException e) {e.printStackTrace();}}}
}
class Dog implements Serializable{private int age;private String name;public Dog(int age, String name) {this.age = age;this.name = name;}@Overridepublic String toString() {return "Dog{" +"age=" + age +", name='" + name + '\'' +'}';}
}
ObjectInputStream的案例
public class ObjInputTest {public static void main(String[] args) {ObjectInputStream ois=null;try{ois=new ObjectInputStream(new FileInputStream("/Users/test/iotest.txt"));System.out.println(ois.readInt());System.out.println(ois.readBoolean());System.out.println(ois.readChar());System.out.println(ois.readDouble());System.out.println(ois.readUTF());Dog dog= (Dog)ois.readObject();System.out.println(dog);System.out.println(dog.getClass());} catch (Exception e) {e.printStackTrace();} finally {try{if (ois!=null){ois.close();}} catch (IOException e) {e.printStackTrace();}}}
}
//输出结果:
100
true
a
3.14
Love Java
Dog{age=6, name='小花'}
class javabase.javaio.Dog
对象流处理事项
- 读取顺序要一致
- 要求序列化或反序列化对象,需要实现Serializable
- 序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
- 序列化对象时,默认将里面所有的属性都进行序列化,但出了static或transient修饰的成员
- 序列化对象时,要求里面属性的类型也需要实现序列化接口
- 序列化具备可继承性,也就是说如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化
数据流
DataInputStream/DataOutputStream的案例
- DataOutputStream数据输出流允许应用程序将基本Java数据类型写到基础输出流中
- DataInputStream数据输入流允许应用程序以机器无关的方式从底层输入流中读取基本的Java类型
//例子1
public class DataStreamTest {public static void main(String[] args) throws IOException {ByteArrayOutputStream boutput = new ByteArrayOutputStream();DataOutputStream dop=new DataOutputStream(boutput);Random random=new Random();dop.writeByte('a');dop.writeByte('b');dop.writeByte('c');dop.writeByte('d');byte[] b= boutput.toByteArray();System.out.println("Print the content");for(int x= 0;x<b.length;x++) {//打印字符System.out.print((char)b[x] + " ");}System.out.println();int count;ByteArrayInputStream binput = new ByteArrayInputStream(b);DataInputStream dip=new DataInputStream(binput);System.out.println("Converting characters to Upper case " );while((count= dip.read())!= -1) {System.out.print(Character.toUpperCase((char)count)+" ");}}
}
//输出结果:
Print the content
a b c d
Converting characters to Upper case
A B C D //例子2
public class DataStreamTest {public static void main(String[] args) throws IOException {DataOutputStream dos = new DataOutputStream(new FileOutputStream("/Users/test/iotest.txt"));dos.writeUTF("Love Java");dos.writeInt(3);dos.writeBoolean(true);dos.writeShort((short)123);dos.writeLong((long)456);dos.writeDouble(3.14);DataInputStream dis = new DataInputStream(new FileInputStream("/Users/test/iotest.txt"));System.out.println(dis.readUTF());System.out.println(dis.readInt());System.out.println(dis.readBoolean());System.out.println(dis.readShort());System.out.println(dis.readLong());System.out.println(dis.readDouble());dis.close();dos.close();}
}
//输出结果:
Love Java
3
true
123
456
3.14
转换流
- 转换流非常的有用,它可以把一个字节流转换成一个字符流
InputStreamReader的案例
"/Users/test/iotest.txt"
Hello Worldpublic class ISReaderTest {public static void main(String[] args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("/Users/test/iotest.txt"),"utf-8"));String s=br.readLine();System.out.println(s);br.close();}
}
//输出结果:
Hello World
OutputStreamWriter的案例
"/Users/test/iotest.txt"public class OSWriterTest {public static void main(String[] args) throws IOException {BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/Users/test/iotest.txt"),"utf-8"));bw.write("你好,世界!");bw.close();}
}
//输出结果:
"/Users/test/iotest.txt"
你好,世界!
标准输入输出流
System.in/System.out的案例
public class InAndOutTest {public static void main(String[] args) {//System.in 编译类型 InputStream//System.in 运行类型 BufferedInputStreamSystem.out.println(System.in.getClass());//System.out 编译类型 PrintStream//System.out 运行类型 PrintStreamSystem.out.println(System.out.getClass());}
}
//输出结果:
class java.io.BufferedInputStream
class java.io.PrintStream
打印流
- 打印流只有输出流没有输入流
PrintStream的案例
"/Users/test/iotest.txt"public class PrintStreamTest {public static void main(String[] args) throws IOException {PrintStream pr=new PrintStream(System.out);pr.println("Love Java");pr.write("Love Java".getBytes());pr.close();System.setOut(new PrintStream("/Users/test/iotest.txt"));System.out.println("这里没有东西");}
}
//输出结果:
Love Java
Love Java"/Users/test/iotest.txt"
这里没有东西
PrintWriter的案例
"/Users/test/iotest.txt"public class PrintWriterTest {public static void main(String[] args) throws FileNotFoundException {PrintWriter pw=new PrintWriter("/Users/test/iotest.txt");pw.println("你好,世界!");pw.write("你好,世界!");pw.close();}
}
//输出结果:
"/Users/test/iotest.txt"
你好,世界!
你好,世界!
Hi, welcome to JasperのBlog!
Java中IO流详细整合(含案例)相关推荐
- Java中IO流,输入输出流概述与总结(转载自别先生文章)
Java中IO流,输入输出流概述与总结 总结的很粗糙,以后时间富裕了好好修改一下. 1:Java语言定义了许多类专门负责各种方式的输入或者输出,这些类都被放在java.io包中.其中, 所有输入流类都 ...
- Java中IO流体系
转载: https://mp.weixin.qq.com/s?__biz=MzA5NzgzODI5NA==&mid=2454030958&idx=1&sn=df27aadb92 ...
- 【Java网络编程与IO流】Java中IO流分为几种?字符流、字节流、缓冲流、输入流、输出流、节点流、处理流
Java网络编程与IO流目录: [Java网络编程与IO流]Java中IO流分为几种?字符流.字节流.缓冲流.输入流.输出流.节点流.处理流 [Java网络编程与IO流]计算机网络常见面试题高频核心考 ...
- Java中IO流的总结
有关Java中IO流总结图 流分类 按方向分 输入流 输出流 按单位分 字节流 字符流 按功能分 节点流 处理流(过滤流) 其他 所有的流继承与这四类流:InputSteam.OutputStream ...
- Java中IO流面试题
Java 中 IO 流分为几种? Java IO与 NIO的区别? 常用io类有那些? 字节流与字符流的区别? 说说阻塞 IO 模型? 说说非阻塞 IO 模型? 说说多路复用 IO 模型? 说说信号驱 ...
- java中io流是类吗_Java中的IO流
今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬板.内存.键盘等处理 ...
- java中IO流详解
不断更新中!!! 1.流的定义及分类 流是个抽象的概念,是对输入输出设备的抽象,Java程序中,对于数据的输入/输操作都是以"流"的方式进行.设备可以是文件,网络,内存等.将数据从 ...
- Java基础IO流(二)字节流小案例
JAVA基础IO流(一)https://www.cnblogs.com/deepSleeping/p/9693601.html ①读取指定文件内容,按照16进制输出到控制台 其中,Integer.to ...
- Java中IO流的分类和BIO,NIO,AIO的区别
到底什么是IO 我们常说的IO,指的是文件的输入和输出,但是在操作系统层面是如何定义IO的呢?到底什么样的过程可以叫做是一次IO呢? 拿一次磁盘文件读取为例,我们要读取的文件是存储在磁盘上的,我们的目 ...
- java中io流,Reader和Writer,InputStream和OutputStream,转换流 InputStreamReader 和 OutputStreamWriter
java中所谓流通俗来讲就是数据源的传输,我们的文件,视屏,对象都可以叫做数据源,io将他们那转换为可以在不同程序中传输的数据,众所周知电脑存储是二进制,而处理时常遇到字节和字符的写入和写出,分别以8 ...
最新文章
- oracle类型sql转为mysql_Oracle和MySql之间SQL区别(等效转换以及需要注意的问题)...
- linux下如何判断oracle数据库tns是否设置正常
- ABCpdf.NET 的简易使用指南
- Object-C-block
- 积跬步,聚小流------html知识大纲归纳总结
- 开源开放 | 开源网络通信行业知识图谱(新华三)
- kafka 主动消费_Kafka核心API——Consumer消费者
- STM32——库函数版——数码管流动显示程序
- 计算机音乐对应的数字,音乐和数字之间的关系
- qq互联php教程,thinkphp5怎么整合qq互联登录教程
- 数据库COUNT(*)、COUNT(字段)和COUNT(1)的异同
- 苹果开发者账号开启双重认证教程
- linux连公共wifi怎么输密码,公共Wifi密码怎么用 公共Wifi密码使用方法
- 如何关闭coreldraw x4检测弹出非法软件您的产品已被禁用怎么办?
- VMware ESxi 7.0定时关机
- 百度地图标记点中添加echarts图表
- 基于可编程图形处理器的骨骼动画算法及其比较
- mysql中的表自增的id太大了,可以重新设置自增起始值
- Numpy之logspace
- SQLServer2008 快捷键集合