字节流和字符流小练习

1 FileOutputStream

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class test {
    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("/Users/mima000000/Desktop/1.txt",false);  //true表示追加
//        每次写出一个字节数据
        fos.write(97); //a
        fos.write(98);  //b

        //换行
        fos.write("\r".getBytes());


//        写出字节数组
        byte[] bytes = " 不想加班".getBytes();
        fos.write(bytes);
        fos.close();
    }
}

2 FileInputStream

2-1 每次读取1个字节

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class test {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("/Users/mima000000/Desktop/1.txt");
        //每次读取一个字节数据
        int b;
        while ((b=fis.read())!=-1){
            System.out.println((char)b);
        }
        fis.close();
    }
}

2-2 使用有效字节数组读取

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class test {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("/Users/mima000000/Desktop/1.txt");
        //每次读取一个字节数据
        int b;
//        使用字节数组读取,每次最多读取2个字节
        byte[] bytes = new byte[2];
        while ((b= fis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,b));
        }
        fis.close();
    }
}

3 FileReader

3-1 每次读取一个字符数据

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;

public class test {
    public static void main(String[] args) throws Exception {
        FileReader fileReader = new FileReader("/Users/mima000000/Desktop/1.txt");
        int b;
        while ((b=fileReader.read())!=-1){
            System.out.println((char)b);
        }

    }
}

3-2 使用字符数组读取

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;

public class test {
    public static void main(String[] args) throws Exception {
        FileReader fileReader = new FileReader("/Users/mima000000/Desktop/1.txt");
        int b;
        char[] temp = new char[2];
        while ((b=fileReader.read(temp))!=-1){
            String res = new String(temp, 0, b);
            System.out.println(res);
        }

    }
}

4 FileWriter

import java.io.*;

public class test {
    public static void main(String[] args) throws Exception {
        FileWriter fileWriter = new FileWriter("/Users/mima000000/Desktop/1.txt");
//        写字符串
        fileWriter.write("你好");
        //写字符数组
        char[] temp = {'e','t'};
        fileWriter.write(temp);
        //写单个字符
        fileWriter.write('g');

        //刷新缓冲
        fileWriter.flush();
        //关闭
         fileWriter.close();



    }
}

posted @ 2020-03-13 23:12  姚狗蛋  阅读(175)  评论(0编辑  收藏  举报