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();
    }
}
​
相关推荐
辞旧 lekkk3 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
zc.z3 小时前
JAVA实现:纯PCM格式音频转换成BASE64
java·音视频·pcm
mask哥4 小时前
力扣算法java实现汇总整理(上)
java·算法·leetcode
2zcode4 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19974 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
Aaswk5 小时前
Java Lambda 表达式与流处理
java·开发语言·python
是宇写的啊5 小时前
Spring AOP
java·spring
万邦科技Lafite5 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
Cyber4K6 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
Mr_pyx6 小时前
Spring AI 入门教程:Java开发者的AI应用捷径
java·人工智能·spring