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();
    }
}
​
相关推荐
cnxy1888 小时前
围棋对弈Python程序开发完整指南:步骤1 - 棋盘基础框架搭建
开发语言·python
Nonoas8 小时前
动态代理:发布订阅的高级玩法
java·ide·intellij-idea
程序员-周李斌9 小时前
Java 死锁
java·开发语言·后端
皮皮林55110 小时前
Prometheus+Grafana,打造强大的监控与可视化平台
java
JasmineWr10 小时前
CompletableFuture相关问题
java·开发语言
零雲10 小时前
java面试:知道java的反射机制吗
java·开发语言·面试
Jeremy爱编码10 小时前
实现 Trie (前缀树)
开发语言·c#
laocooon52385788610 小时前
插入法排序 python
开发语言·python·算法
你的冰西瓜10 小时前
C++中的list容器详解
开发语言·c++·stl·list
java1234_小锋11 小时前
Java进程占用的内存有哪些部分?
java