Java中的IO流

发布于:2024-05-02 ⋅ 阅读:(26) ⋅ 点赞:(0)

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();
    }
}
​