JavaIO实例

JavaIO实例

文本读写(以"Hello World,and happy everyday!!")为例

1.FileInputStream、FileOutputStream(字节流)

字节流的方式效率低,不推荐

import java.io.*;

public class IOTest {
    public static void main(String[] args) throws IOException {
        //设置文件对象
        File file = new File("test.txt");
        write(file);
        System.out.println(read(file));
    }
    public static void write(File file) throws IOException{
        //创建文件输出流(字节流,节点流)
        OutputStream os = new FileOutputStream(file);
        String a = "Hello World,and happy everyday!!";
        //写入内容到文件(注意是字节流,所以应当写入的是byte数据类型!!)
        //这种写入是不会追加,会覆盖原来的内容
        os.write(a.getBytes());
        //关闭流
        os.close();
    }
    public static String read(File file) throws IOException{
        //创建文件输入流(字节流,节点流)
        InputStream in = new FileInputStream(file);
        //因为是字节流,所以用byte来接收
        //定义一次接收读取多少个字节
        byte[] bytes = new byte[1024];
        //用来接收读取的字节数组
        StringBuffer sb = new StringBuffer();
        //读取到的字节数组长度,-1表示没有数据(因为数组第一个索引是0!)
        int length=0;
        //循环读数据
        while ((length = in.read(bytes))!=-1){
            //把读取的内容(字节byte)转化为字符串
            sb.append(new String(bytes,0,length));
        }
        in.close();
        return sb.toString();
    }
}

2.BufferedInputStream,BufferedOutputStream(缓冲字节流)

缓冲字节流是为高效率而设计的,真正的读写操作还是靠FileOutputStreamFileInputStream,所以其构造方法入参是这两个类的对象也就不奇怪了。

import java.io.*;

public class IOTest1 {
    public static void main(String[] args) throws IOException {
        File file = new File("test,txt");
        write(file);
        System.out.println(read(file));
    }
    public static void write(File file) throws IOException{
        //缓冲字节流(处理流)
        BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(file));
        String a = "Hello World,and happy everyday!!";
        //写入文件
        bis.write(a.getBytes());
        //关闭流
        bis.close();
    }
    public static String read(File file) throws IOException{
        //缓冲字节流(处理流)
        BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
        //同理
        byte[] bytes = new byte[1024];
        StringBuffer sb = new StringBuffer();
        int length = 0;
        while((length=fin.read(bytes))!=-1){
            sb.append(new String(bytes,0,length));
        }
        fin.close();
        return sb.toString();//这里有个注意点,sb是StringBuffer,不是String类型
    }
}

3.InputStreamReader,OutputStreamWriter(字符流)

字符流适用于文本文件的读写OutputStreamWriter类其实也是借助FileOutputStream类实现的,故其构造方法是FileOutputStream的对象

import java.io.*;

public class IOTest2 {
    public static void main(String[] args) throws IOException{
        File file = new File("test.txt");
        System.out.println(read(file));
        write(file);
    }
    public static String read(File file) throws IOException{
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file));

        //字符数组,一次接收多少字符(字符是char类型)
        char[] chars = new char[1024];
        //每次读取的字符数组先append到Stringbuilder中
        StringBuilder sb = new StringBuilder();
        //读取字符数组长度,-1表示没有数据
        int length = chars.length;
        while((length= isr.read(chars))!=-1){
            sb.append(chars,0,length);
        }
        isr.close();
        return sb.toString();
    }
    public static void write(File file) throws IOException{
        //OutputStreamWriter可以显示指定字符集,否则使用默认字符集
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file));
        //要写入的字符串
        String a = "Hello World,and happy everyday!!";
        osw.write(a);
        osw.close();
    }
}

3.FileWriter,FileReader(字符便捷流)

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class IOTest3 {
    public static void main(String[] args) throws IOException {
        File file = new File("test.txt");
        write(file);
        System.out.println(read(file));
    }
    public static void write(File file) throws IOException{
        FileWriter fw = new FileWriter(file);
        String a = "Hello World,and happy everyday!!";
        fw.write(a);
        fw.close();
    }
    public static String read(File file) throws IOException{
        FileReader fr = new FileReader(file);
        //一次性获取的字节数
        char[] chars = new char[1024];
        //用来存储字节数组
        StringBuilder sb = new StringBuilder();
        //读取字节数组长度,-1表示没有数据
        int length;
        while((length = fr.read(chars))!=-1){
            sb.append(chars,0,length);
        }
        fr.close();
        return sb.toString();
    }
}

5、BufferedReader、BufferedWriter(字符缓冲流)

import java.io.*;

public class IOTest4 {
    public static void main(String[] args) throws IOException{
        File file = new File("test.txt");
        write(file);
        System.out.println(read(file));
    }
    public static void write(File file) throws IOException {
        // BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
        // FileWriter可以大幅度简化代码
        BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));//第二个true的意思是可以追加

        // 要写入的字符串
        String string = "Hello World,and happy everyday!!";
        bw.write(string);
        bw.close();
    }
    public static String read(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        // 用来接收读取的字节数组
        StringBuilder sb = new StringBuilder();
        // 按行读数据
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        return sb.toString();
    }
}
posted @ 2022-02-16 00:13  霜鱼CC  阅读(10)  评论(0)    收藏  举报