JavaNote 2.5IO流

一、字节流继承图解

JavaNote 2.5IO流

JavaNote 2.5IO流JavaNote 2.5IO流

二、字符流继承图解

JavaNote 2.5IO流

JavaNote 2.5IO流

三、实例

例1
import java.io.*;

public class Test18 {
    public static char charToByteAscii(byte b) {//ASCII码转换成字符串
        char ch = (char) b;
        return ch;
    }

    public static void main(String args[]) {
        String name = "hello,world.";
        byte[] b = name.getBytes();

        try {
            FileOutputStream fileOutputStream = new FileOutputStream("helloworld.txt",true);//如果不存在指定的文件,则会新建一个
            fileOutputStream.write(100);//这里的整型对应的是ASCII表,写入ascii码为100的值
            fileOutputStream.write(b, 0, 8);//将b[off]开始,长度为8个字节的数据写入helloworld.txt
            FileInputStream fileInputStream = new FileInputStream("helloworld.txt");//如果文件不存在会报错
            fileInputStream.read(b, 0, 7);//从helloworld.txt读取7个字节存储在b中,不影响b[len]之后的内容
            int c = 0;
            while ((c = fileInputStream.read()) != -1) {//读取helloworld.txt字节,读到文件末尾返回-1
                System.out.print((char) c);
            }
            DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
            dataOutputStream.writeUTF(name);//将name写入helloworld.txt
            dataOutputStream.write(100);// 此方法与dataOutputStream.writeByte(100);作用是一样的
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(10);//size是缓存区大小,动态增加;
            DataOutputStream dataOutputStream1 = new DataOutputStream(byteArrayOutputStream);
            dataOutputStream1.write(b, 0, 11);//将字节数组数据写入字节流缓冲区
            byteArrayOutputStream.writeTo(fileOutputStream);//将字节数组输出到helloworld.txt中
            byte d[] = byteArrayOutputStream.toByteArray();//字节数组流转换成字节数组
            for (byte x : d) {
                System.out.print((char) x);
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
例2
public class Test18 {
    public static char charToByteAscii(byte b) {//ASCII码转换成字符串
        char ch = (char) b;
        return ch;
    }

    public static void main(String args[]) {
        String name = "hello,world.";
        byte[] b = name.getBytes();

        try {
            FileOutputStream fileOutputStream = new FileOutputStream("helloworld.txt",true);//如果不存在指定的文件,则会新建一个
            PrintStream printStream = new PrintStream(new FileOutputStream("my.txt"));//建立指向my.txt的打印流
            printStream.print(name);//将name的值打印到my.txt中
            System.setOut(printStream);
            System.out.print("将此信息输出到my.txt中");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print(bufferedReader.readLine());//将用户输入的一行信息,输出到my.txt中
            DataInputStream dataInputStream = new DataInputStream(System.in);
            dataInputStream.read(b);//将用户输入的数据存储在b中,长度为b的长度
           // dataInputStream.read(b,0,5);//将用户输入的数据截取len存储在b中,b[len]之后的数据不受影响
            for(int i = 0; i<b.length; i++){
                System.out.print((char) b[i]);//将b中的数据打印到my.txt中
            }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
        }
        
    }
}