IO流的简单实现

学习目标:

熟练掌握IO流的基本实现方式

例题:

字节输出流

代码如下:

public class OutputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        //1):创建源或者目标对象
        File file = new File("填你要写入文件的绝对路劲/相对路径,后面都一样,不再提示,例如:G:\\workspace\\test.txt");
        //2):创建IO流对象
        OutputStream outputStream = new FileOutputStream(file);
        //3):具体的IO操作
        outputStream.write("12".getBytes()); // 写入一个字节数组
        outputStream.write(122); // 写入一个整形数字,会被转化成Unicode码
        outputStream.write(new byte[]{'a', 'g', 'a'}); // 传入字节数组
        //4):关闭资源
        outputStream.close();
    }
}

运行结果:

12zaga

字节输入流

代码如下:

public class InputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        //1):创建源或者目标对象
        File file = new File("G:\\aaa.txt");
        //2):创建IO流对象
        InputStream inputStream = new FileInputStream(file);
        //3):具体的IO操作
        byte[] buffer = new byte[(int) file.length()];
        int len = 0;
        while((len = inputStream.read(buffer)) != -1) {
            String str = new String(buffer, 0, len);
            System.out.println("str = " + str);
        }
        //4):释放资源
        inputStream.close();
    }
}

运行结果:

"C:\Program Files\Java\jdk-11.0.9\bin\java.exe" "-"
str = ZafdsZAfaF
Process finished with exit code 0

字符输入流

代码如下:

public class FileReaderDemo01 {
    public static void main(String[] args) throws IOException {
        // 1)创建file对象,确定要操作的文件
        File file = new File("G:\\aaa.txt");
        // 2)获取字符流
        // FileleReader reader = new FileReader(file);   (直接获取的方式)
        // 桥接的方式
        InputStream in = new FileInputStream(file);
        Reader reader = new InputStreamReader(in);
        char[] buffer = new char[1024];
        // 如果文件长度小于buffer数组长度,则第一次读取len长度为buffer的长度,第二次读取为-1
        // 如果文件长度大于buffer数组长度,则第一次读取len长度为buffer的长度,第二次读取继续判断剩余文件长度,直到返回-1
        int len = reader.read(buffer, 0, buffer.length);
        while (len > 0) {
            System.out.println(Arrays.toString(Arrays.copyOf(buffer, len)));
            System.out.println("readLength: " + len);
            len = reader.read(buffer, 0, buffer.length);
        }
        // 3)释放资源
        reader.close();
    }
}

运行结果:

"C:\Program Files\Java\jdk-11.0.9\bin\java.exe"
[Z, a, f, d, s, Z, A, f, a, F]
readLength: 10

Process finished with exit code 0

字符输出流

代码如下:

public class FileWriterDemo01 {
    public static void main(String[] args) throws IOException {
        //1):创建源或者目标对象
        File file = new File("G:\\aaa.txt");
        //2):创建IO流对象
        FileWriter out = new FileWriter(file);
        //3):具体的IO操作
        out.write('光');//输出A
        out.write('头');//输出B
        out.write('强');//输出C
        out.write(':');//输出D
        out.write(' ');//输出D
        String str = "众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。";
        out.write(str.toCharArray());
        out.write(str);//String的本质就是char[]
        //4):关闭资源(勿忘)
        out.close();


    }
}

运行结果:

光头强: 众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。

# 总结: 以上就是IO流的简单实现,代码仅供参考。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值