JavaLearning-IO操作

异常

  • Throwable类
    Throwable类,已知子类Error类,Exception类
    java.lang.Throwable
  • Error类
  • 错误非常严重必须修改程序
  • Exception类
    异常可以修正将异常处理,可以继续执行
  • RuntimeException类
    继承自Exception类
    运行时异常表示虚拟机的通常操作中可能遇到的异常,是一种常见运行错误。
    java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。
    NullPointerException,
    IndexOutOfBoundsException,
    NoSuchElementException,
  • 产生异常原因以及处理机制
    ArrayIndexOutOfBoundsException
    JVM能够检测到出现数组的越界问题。
    JVM:创建了异常对象 new ArrayIndexOutOfBoundsException();
    将异常对象抛出,抛给方法的调用者(注意: 一旦异常被抛出,后续程序都不再执行。)
    一直向上级调用者抛,直到抛给JVMJVM收到异常后,将异常信息输出到控制台JVM将程序停止。

File流

OutputStream字节输出流


  • java.io.OutputStream
    类是抽象类
  • 方法
    write()
    close()
  • 实现类
    ByteArrayOutputStream
    FileOutputStream
    FilterOutputStream
    ObjectOutputStream
  • 实现文件的续写
    FileOutputStream的构造方法,第二个参数,加入true(append)在文件中写入换行(\r\n)- IO流异常处理
    try catch finally
    try外面声明变量,try里面建立对象
    1.保证流对象变量,作用域足够
    2.catch块,怎么处理异常
    3.(IO写数据可能往外界设备写,写过程中外接设备断了连接)throw new RuntimeException(……);——停下程序,重新尝试
    4.如果流对象建立失败,需要关闭资源吗。new对象失败,没有占用资源
    在释放资源时,需要判断对象是不是null,据此判断对象是否建立成功。

InputStream字节输入流


  • java.io.InputStream
    抽象类,超类
  • 方法
    read()——读到文件结尾,返回-1
    close()
  • 注意
    read会总动向后读一个字节,while判断条件的len不能少
public static void main(String[] args) {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try{
        fileInputStream = new FileInputStream("d:\\test.txt");
        fileOutputStream  = new FileOutputStream("d:\\a.txt");
        int len = 0;
        while ((len=fileInputStream.read())!=-1){
            fileOutputStream.write(len);
        }

    }catch (IOException e){
        System.out.println(e);
        throw new RuntimeException("文件复制失败!");
    }finally {
        if(fileInputStream!=null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("释放资源失败!");
            }finally {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException("释放资源失败!");
                }
            }
        }
    }
public static void main(String[] args) {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try{
        fileInputStream = new FileInputStream("d:\\test.txt");
        fileOutputStream = new FileOutputStream("d:\\a.txt");

        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = fileInputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes);
        }
    }catch(IOException e){
        System.out.println(e);
    }finally {
        try {
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

数组作用:缓冲,提高系统效率。一般写byte[1024*n]——n表示字节的整数倍,一般n取1

字符输出流

超类java.io.Writer写文件,写文本文件

  • 方法
    write(int c)——写一个字符
    write(char[] c)——写字符数组(注意字节输出流是byte[])
    write(char[] c,int ,int)——写字符数组的一部分write(String s)——写入字符串
  • 子类
    FileWriter(字符输出流必须运行一个功能,刷新功能,flush())

字符输入流

超类java.io.Reader只能读取文本文件,不能读取音乐之类的文件
专门读取文本文件

  • 方法
    read()——读取一个字符,字符数组(没有读取字符串的类型,对于文本来讲,字符串的概念比较广泛)
    flush()和close()——flush()对缓冲区进行刷新,close()关闭前也会刷新。刷新的只有Writer写才刷新。

流的操作规律

  1. 明确是要读还是要写
    源:
    InputStream
    Reader
    目的:
    OutputStream
    Writer
  2. 要操作的是数据是字节还是文本
    源:
    字节 InputStream
    文本Reader
    目的:
    字节OutputStream
    文本Writer
  3. 明确数据具体所在位置
  • 源设备
    硬盘:
    文件 File
    内存:数组,字符串ByteArrayInputStream,CharArrayWriter
    键盘:System.in
    网络:Socket(网络中的套接字,两台主机都有Socket就可以互联,必须用字节)
  • 目的设备
    键盘:
    文件 File
    内存: 数组,字符串
    屏幕:System.out
    网络:Socket
  1. 是否需要额外功能提高效率:缓冲区对象,Bufferedxxxxx转换:转换流,InputStreamReader OutputStreamWriter

[InputStream->FileInputStream->BufferedInputStream
OutputStream->FileOutputStream->BufferedOutputStream
Reader->InputStreamReader->FileReader(文本)
Writer->OutputStreamWriter->FileWriter]

对象序列化与反序列化

序列化:

写对象
对象中的数据,以流的形式,写入到文件中保存的过程称为写出对象,对象序列化ObjectOutputStream,将对象写入到文件中,实现序列化(对象-》字节输出流-》文件)

反序列化:

读对象
在文件中,以流的形式将对象读取出来,读取对象,对象反序列化
ObjectInputStream,将文件中的内容写入对象,实现反序列化

异常ObjectStreamException

当实例需要具有序列化接口时,抛出此异常。
Serializable(可串行的)接口(标记型接口,接口内容为空)

注意:

序列化针对对象.
类中的静态成员变量,成员方法都不能序列化。

transient

阻止成员变量序列化
瞬态关键字,可以保证成员变量不被序列化(成员变量是默认值)

编码表

  • ASCII7位,0xxxxxxx,没有负数
  • ISO-8859-1拉丁文编码表,可以表示负数
  • GB2312简体中文表,早期中文编码表,两个字节,第一个字节是负,第二个字节可正可负- GBK最常用
  • GB13080最新中文码表,目前还没有正式使用
  • unicode国际标准编码表,char占两个字节。
  • UTF-8基于unicode,能用一个字节表示,就不用两个字节。更加标准化。

能识别中文

GBK,UTF-8

常用编码

GBK,UTF-8,ISO-8859-1
(互联网开发,写好的类放在服务器运行,中文运行上去会变成乱码)

编码/解码

文字→数字:编码 “abc”.getBytes() byte[]
数字→文字:解码 byte[] b = {97,98,99} new String()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值