《JAVA300集》IO流_入门-day14

目录

IO入门

文件编码与解码

四个抽象类

文件字节输入流

正式规范代码

文件字节输出流


IO入门

文件编码与解码

字符集:Java字符使用16位的双字节存储,实际文件存储的数据有各种字符集。

编码(encode):字符 —》字节。

解码(decode):字节 —》字符。

不同的编码方式对应不同的字符集

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class TestEncode {
    public static void main(String[] args) throws UnsupportedEncodingException {  //throws抛出异常
        String msg = "性命生命使命";
        byte[] datas = msg.getBytes();  //默认是utf8
        System.out.println(datas.length);

        //编码其他字符集
        byte[] datas2 = msg.getBytes(StandardCharsets.UTF_16LE);
        System.out.println(datas2.length);

        //解码为字符串
        String msg1 = new String(datas,0,datas.length,"utf8");
        System.out.println(msg1);

        //解码为字符串 (字符集与编码方式不对应,乱码)
        String msg2 = new String(datas,0,datas.length,"UTF_16LE");
        System.out.println(msg2);
    }
}

运行结果:

四个抽象类

字节流和字符流

注:(1)close() 是通知 操作系统 可以去释放文件了。 (2)flush()是强制输出缓冲里的东西。

文件字节输入流

不规范代码

import java.io.*;

public class IOTest01 {
    public static void main(String[] args) throws IOException {
        File src = new File("D:/Java/IO/abc.txt");  // 1.创建源
        InputStream is = new FileInputStream(src);  // 2.选择流
        int data1 = is.read();                      // 3.操作
        int data2 = is.read();
        System.out.println(data1);
        System.out.println((char)data1);
        System.out.println((char)data2);
        is.close();                                 // 4.释放资源

    }
}

运行结果:

较为规范代码

import java.io.*;

public class IOTest02 {
    public static void main(String[] args) throws IOException {
        File src = new File("D:/Java/IO/abc.txt");  // 1.创建源
        InputStream is = new FileInputStream(src);  // 2.选择流
        int temp;
        while((temp=is.read()) != -1) {//编译器里空的是-1
            System.out.print((char)temp);   // 3.操作
        }
        if(null!=is){
            is.close();                     // 4.释放资源
        }
    }
}

正式规范代码

import java.io.*;

public class IOTest03 {
    public static void main(String[] args) {
        File src = new File("D:/Java/IO/abc.txt");  // 创建源
        InputStream is = null;
        try{
            is = new FileInputStream(src); // 选择流
            int temp;
            while((temp=is.read())!=-1){
                System.out.print((char)temp);
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try{
                if(null!=is){
                    is.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

(这个代码是读一个,打一个)

(下面的代码是读一段,打一段)

import java.io.*;

public class IOTest03 {
    public static void main(String[] args) {
        File src = new File("D:/Java/IO/abc.txt");
        InputStream is = null;
        try{
            is = new FileInputStream(src); //选择流
            byte[] flush = new byte[3]; //缓冲容器,常用1024
            int len = -1;  //接受长度
            while((len=is.read(flush))!=-1){
                // 字节数组—》字符串
                String str = new String(flush,0,len);
                System.out.println(str);
            }
        } catch(IOException e){
            e.printStackTrace();
        } finally {
            try{
                if(null!=is){
                    is.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

文件字节输出流

import java.io.*;

public class IOTest04 {
    public static void main(String[] args) {
        File src = new File("D:/Java/IO/test.txt"); //创建源
        OutputStream os = null; //选择流

        try{
            os = new FileOutputStream(src); //默认是false重新写,如果是ture,则是追屁股写
            String msg = "IO is easy";
            byte[] datas = msg.getBytes(); //编码
            os.write(datas,0,datas.length);
            os.flush(); //养成刷新的习惯
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                if(null!=os){
                    os.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值