前言
此篇文章属于Java语言必学基础内容
文章目录
目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、什么是JavaIO流?
Java IO流是指在Java程序中操作输入和输出的机制。它是一种基于流的输入/输出模型,将数据视为一系列按顺序排列的字节,从一个流中读取,在一个流中写入。Java提供了多种类型的流,包括字节流和字符流,以适应不同的数据类型和输入/输出方式。它在Java的文件读写、网络传输、数据处理等方面都有广泛应用。
二、JavaIO流的种类
1、根据数据流向分类:
1.1 输入流:读数据,将硬盘中的数据读取到内存中
1.2 输出流:写数据,将程序中的数据写入到硬盘中
2、按照数据类型来分
2.1 字节流:字节输入流/字节输出流
2.2 字符流:字符输入流/字符输出流
3、按照IO流应用场景:
3.1 纯文本文件,优先使用字符流
3.2 图片、视频、音频等二进制文件,优先使用字节流
3.3 不确定文件类型,优先使用字节流,字节流是万能的流
三、字节输入输出流案例
1、FileOutputStream类中的write方法
拥有四个重载函数
void write(int, boolean)
void write(int)
void write(byte[])
void write(byte[], int, int)
该方法为重新写入,传入的int值会自动转换为ASCII码对应的字符,第一个函数不可调用,为private权限
第三个方法就是写入一串字符,第四个方法为 写入字符数组byte的第一个int为偏移量(即为从第几个字符开始写起),第二个int为写入长度。
创建demo1测试:
public class demo1 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("D:\\sjava\\demo1.txt");
byte[] b = new byte[]{'a','b','c','d','e'};
fileOutputStream.write(b);
fileOutputStream.close();
}
}
注意:FileOutputStream继承自OutputStream类,OutputStream为一个抽象类,我们在这里new出FileOutputStream实现类,在传入构造函数的参数时,如果没有该文件,会先调用系统创建文件功能创建出该文件,然后再new出对象,再让该对象指向该文件
程序运行结果:
可以写入\n为换行
1.1、如何写入一个字符串并追加?
写入字符串:通过调用String的getByte()方法传入上面参数为byte[]的方法即可实现。
追加:在new出FileOutputStream对象时,在后面填入true
FileOutputStream fileOutputStream = new FileOutputStream("demo1.txt",true);
注意:在写完数据后,可使用close()方法来关闭该流,若在程序运行中出现了异常可使用finally关键字来最终释放该流:
public class demo1 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("demo1.txt",true);
byte[] b = new byte[]{'a','b','c','d'};
fileOutputStream.write(b);
int j = 1/0;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}finally {
fileOutputStream.close();
System.out.println("关闭流成功!!!");
}
}
}
程序运行结果:
2.FileInputStream及其API
2.1、FileInputStream类
继承自InputStream
构造函数:
FileInputStream(String name):通过文件名创建输入流。
FileInputStream(File file):通过文件对象创建输入流。
常用方法:
int read():读取一个字节的数据。返回值为-1时,文件读取完毕
int read(byte[] b):从输入流中读取一定数量的字节,并将其存储到缓冲区数组b中。
int read(byte[] b, int off, int len):从输入流中读取最多len个字节的数据到缓冲区数组b中,并从offset位置开始存储数据。
void close():关闭输入流。
常用属性:
FileDescriptor fd:文件描述符。
FileChannel getChannel():返回与此文件输入流相关联的唯一 FileChannel 对象。
测试:已知在demo1.txt文件中存入了一个字符a
FileInputStream fileInputStream = new FileInputStream("demo1.txt");
int read = fileInputStream.read();
System.out.println((char)read);
程序运行结果:
它的重载函数使用方式跟它差不多
2.2 read方法循环读取:
FileInputStream fileInputStream = new FileInputStream("demo1.txt");
int read;
while ((read = fileInputStream.read())!=-1){
System.out.println((char)read);
}
fileInputStream.close();
程序运行结果:
可以看到,正确读取到文件中的内容。
也可以使用read的重载函数read(byte[])来将读取结果直接存到byte数组中:
public class demo2 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("demo1.txt");
byte[] b = new byte[20];
fileInputStream.read(b);
for (byte b1: b) {
System.out.print((char)b1+" ");
}
fileInputStream.close();
}
}
运行结果:
该重载函数就是说在调用read方法读文件时,可以将读到的内容存到参数byte数组中。
2.3 通过字节流来复制图片:
前面提到,字节流基本上时万能的读取读入方法,下面我们就来通过字节流来复制图片到另一个目录。
将当前目录的1.jpg复制到D:\\sjava目录下为copy.jpg:
public class demo2 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("1.jpg");
int len;
byte[] b = new byte[1024];
FileOutputStream fileOutputStream = new FileOutputStream("D:\\sjava\\copy.jpg");
while((len=fileInputStream.read(b))!=-1){
fileOutputStream.write(b,0,len);
}
fileInputStream.close();
fileOutputStream.close();
}
}
运行结果:
四、字节缓冲流:
对传统字节输入输出流做了封装。
通过该流来复制140M视频和传统字节流比较时间对比:
public class demo2 {
public static void main(String[] args) throws IOException {
long l = System.currentTimeMillis();
a1();
long l1 = System.currentTimeMillis();
System.out.println(l1-l+"毫秒");
}
public static void a1() throws IOException {
FileInputStream fileInputStream = new FileInputStream("1.wmv");
int len;
byte[] b = new byte[1024];
FileOutputStream fileOutputStream = new FileOutputStream("D:\\sjava\\copy.wmv");
while((len=fileInputStream.read(b))!=-1){
fileOutputStream.write(b,0,len);
}
fileInputStream.close();
fileOutputStream.close();
}
public static void a2() throws IOException {
FileInputStream fileInputStream = new FileInputStream("1.wmv");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
int len;
byte[] b = new byte[1024];
FileOutputStream fileOutputStream = new FileOutputStream("D:\\sjava\\copy.wmv");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
while((len=bufferedInputStream.read(b))!=-1){
bufferedOutputStream.write(b,0,len);
}
bufferedOutputStream.close();
fileOutputStream.close();
bufferedInputStream.close();
fileInputStream.close();
}
}
分别调用a1()a2()方法和获取系统时间戳方法来比较
传统字节流方法时间大概是1079毫秒
字节缓冲流时间大概是265毫秒,可见提升还是很大的。推荐使用字节缓冲流。
Java中,按照编码的不同,汉字占用字节也不同:
GBK编码----2个字节
UTF-8编码-----3个字节
五、字符流
字符流是指一连串的字符组成的流,包含输入流和输出流两种类型。输入流是指从外部读入字符数据到程序中,输出流是指将程序中的字符数据输出到外部。在Java中,字符流主要是通过Reader和Writer这两个类实现的。其中,Reader类是用于读取字符流的抽象类,而它的子类有FileReader、BufferedReader等;而Writer类是用于写字符流的抽象类,它的子类有FileWriter、BufferedWriter等。使用字符流相对于字节流的优点是可以自动进行字符编码和解码,因此处理文本文件时更为方便。
字符流包含的类跟字节流的类及API使用方法基本一样。
六、总结
IO流是Java中提供的处理输入输出数据的机制,它是以流的方式来处理数据。其中,输入流是从外部读取数据到程序中,输出流是将程序中的数据写入到外部。IO流按照方向分为输入流和输出流;按照操作数据的类型又分为字节流和字符流。字节流处理的数据单元为字节,适用于处理二进制文件和图片等数据;而字符流处理的数据单元为字符,适用于处理文本文件等数据。
Java中常见的IO流类有InputStream、OutputStream、Reader、Writer等。其中,InputStream和Reader是用来读取数据的,而OutputStream和Writer则是用来输出数据的。Java中还提供了缓冲流、转换流、对象流等高级IO流,可以提高IO流的效率和功能。
使用IO流的步骤一般为:创建输入流或输出流对象、读取或写入数据、关闭IO流。
总之,IO流在Java中的应用非常广泛,是Java中不可或缺的重要部分。
如果对于File类不太熟悉,我其它文章有记录到,感兴趣的小伙伴可以去浏览 ----->JavaSe基础File类相关知识_Jay/.的博客-CSDN博客