java入门与基础(实例之IO)

Posted 晨沉宸辰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java入门与基础(实例之IO)相关的知识,希望对你有一定的参考价值。

package com.example30.io;

import java.io.*;

public class IOTest {
    public static void main(String[] args) throws IOException {
        //getByteStreams();
        //getAutoCloseable();
        // getByteArray();
        getStringsInText();
    }

    /**
     * 仅用于了解IO输入输出过程,勿使用
     * @throws IOException
     */
    private static void getByteStreams() throws IOException {
        try(FileInputStream in = new FileInputStream("D:/test/input.txt");
            FileOutputStream out = new FileOutputStream("D:/test/output.txt");) {
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
        }
    }

    private static void getAutoCloseable() {
        try(MyResource r = new MyResource()) {
            System.out.println("Try");
            throw new Exception();
        } catch (Exception e) {
            System.out.println("Catch");
        } finally {
            System.out.println("Finally");
        }
    }

    /**
     *
     * @throws IOException
     */
    private static void getTryWithResources2() throws IOException {
        FileInputStream in = new FileInputStream("D:/test/input.txt");
        FileOutputStream out = new FileOutputStream("D:/test/output.txt");
        try (in; out) {
            int c = 0;
            while ((c = in.read()) != -1) {
                System.out.println("读取字节的十进制整数:" + c);
                out.write(c);
            }
        }
    }

    /**
     * 基于字节数缓冲区的实现
     * @throws IOException
     */
    private static void getByteArray() throws IOException {
        try(FileInputStream in = new FileInputStream("D:/test/input.txt");
            FileOutputStream out = new FileOutputStream("D:/test/output.txt");) {
            byte[] buffer = new byte[4];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
        }
    }


    /**
     * Java9引入,封装基于字节数组的实现
     * 字节数组长度8192
     */
    private static void getTransferTo() throws IOException {
        try (FileInputStream is =
                     new FileInputStream("D:/test/Twinkle Twinkle Little Star.mp4");
        FileOutputStream os = new FileOutputStream("D:/test/mov.mp4")) {
            is.transferTo(os);
        }
    }

    /**
     * 基于文本文件的具体字符集,读取
     * @throws IOException
     */
    private static void getStringsInText() throws IOException {
        try (FileInputStream is =
                     new FileInputStream("D:/test/cn.txt")) {
            byte[] bytes = is.readAllBytes();
            System.out.println(new String(bytes));
        }
    }
}
package com.example30.io;

public class MyResource implements AutoCloseable{
    @Override
    public void close() throws Exception {
        System.out.println("Closed");
    }
}

以上是关于java入门与基础(实例之IO)的主要内容,如果未能解决你的问题,请参考以下文章

Java入门与基础(线程的入门实例1之QQ)

FlinkFlink基础之WordCount实例(Java与Scala版本)

Java基础入门五)之方法以及递归算法

WebSocket.之.基础入门-后端响应消息

Java入门:基础算法之获取用户输入

Java面向对象之多态(向上向下转型) 入门实例