Java中的IO流

IO流的概述和分类

IO流分为输入输出流

输入流:读数据

输出流:写数据

流:是一种抽象的概念,是对数据传输的总称,流的本质是数据传输

按照数据类型来分

字节流:字节输入流,字节输出流

字符流:字符输入流,字符输出流

字节流和字符流怎么使用:

如果是通过记事本能够读懂的(比如汉字),我们用字符流,否则就用字节流。当我们不知道使用字节流还是字符流是就要用字节流。

字节流写数据

字节流的抽象基类

InputStream :这个抽象类是所有字节输入流的父类

OutputStream:这个类是所有字节输出流的父类

子类名特点:子类名称都是以其父类作为子类名

在IO流写之前 我们是没有创建文件的

复制代码
package day1;
​
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class IOStreamTest {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("java.txt");
        fos.write(97);
        fos.write(97);
        fos.close();
    }
}
​

当我们运行的时候自动创建了文件夹

FileOutputStream的作用

  1. 调用系统创建了文件

  2. 创建了字节输出流对象

  3. 让字节输出流对象指向创建好的文件

最后

一定要记得关闭文件输出流并释放此流相关的任何系统资源

字节流写数据的三种方式

  1. void write(int b)将指定字节写入此文件输出流

  2. void write (byte[] b)将b.length字节从指定的字节数组写入此文件输出流一次写一个字节数组数据

  3. void write (byte[] b ,int off,int len)将len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流,一次写一个字节数组数据

复制代码
package day1;
​
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class IOStreamTest {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("java.txt");
        byte[] bytes = "abcABC".getBytes();
        fos.write(bytes);
        fos.close();
    }
}
​
复制代码
package day1;
​
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class IOStreamTest {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("java.txt");
        byte[] bytes = "abcABC".getBytes();
        fos.write(bytes,1,3);
        fos.close();
    }
}
​

fos.write(bytes,1,3);是指从byte[]数组的第一个开始写三个

字节流如何换行

复制代码
package day1;
​
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class IOStreamTest {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("java.txt");
        byte[] bytes = "abcABC".getBytes();
        fos.write(bytes);
        fos.write("\r".getBytes());
        fos.write(bytes);
        fos.write("\t".getBytes());
        fos.write(bytes);
        fos.close();
    }
}
​

字节流如何追加写入

复制代码
package day1;
​
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class IOStreamTest {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("java.txt",true);
        byte[] bytes = "abcABC".getBytes();
        fos.write(bytes);
        fos.write("\r".getBytes());
        fos.write(bytes);
        fos.write("\t".getBytes());
        fos.write(bytes);
        fos.close();
    }
}
​

其中FileOutputStream fos = new FileOutputStream("java.txt",true);后面的true就是追加写入,默认为false

字节流读数据

FileInputStream(String name)

复制代码
package day1;
​
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class IOStreamTest {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("java.txt");
        int by=fis.read();
        while (by!=-1){
            System.out.print((char)by);
           by= fis.read();
        }
        fis.close();
    }
}
​

注意 by= fis.read();这一行一定不能少 否则代码会一直循环读第一个字节

对于读字节的优化如下

复制代码
 int by;
        while ((by=fis.read())!=-1){
            System.out.print((char)by);
        }

复制文件

复制代码
package day1;
​
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class IOStreamTest {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("java.txt");
        FileOutputStream fos = new FileOutputStream("write.txt");
        int by;
        while ((by=fis.read())!=-1){
            fos.write(by);
        }
        fis.close();
        fos.close();
    }
}
​
相关推荐
_F_y7 分钟前
C++重点知识总结
java·jvm·c++
打工的小王8 分钟前
Spring Boot(三)Spring Boot整合SpringMVC
java·spring boot·后端
毕设源码-赖学姐10 分钟前
【开题答辩全过程】以 高校体育场馆管理系统为例,包含答辩的问题和答案
java·spring boot
我真会写代码11 分钟前
SSM(指南一)---Maven项目管理从入门到精通|高质量实操指南
java·spring·tomcat·maven·ssm
vx_Biye_Design12 分钟前
【关注可免费领取源码】房屋出租系统的设计与实现--毕设附源码40805
java·spring boot·spring·spring cloud·servlet·eclipse·课程设计
java干货16 分钟前
为什么 “File 10“ 排在 “File 2“ 前面?解决文件名排序的终极算法:自然排序
开发语言·python·算法
_F_y16 分钟前
C语言重点知识总结(含KMP详细讲解)
c语言·开发语言
DN金猿17 分钟前
接口路径正确,请求接口却提示404
java·tomcat
毕设源码-郭学长18 分钟前
【开题答辩全过程】以 基于python的二手房数据分析与可视化为例,包含答辩的问题和答案
开发语言·python·数据分析
无小道41 分钟前
Qt——常用控件
开发语言·qt