(八) IO

一、说明

  • IO(Input/Output),输入输出,指Java与其他设备文件交互。如读文件,写文件。
  • 对于Java的IO工具类,都在java.io.* 包下。

二、IO相关类的使用

2.1 File类:

2.1.1 类说明
  • 文件和目录路劲名的抽象表示,与平台无关
  • 只能操作文件面上的东西,不能访问文件内部(需使用IO流)
  • File对象可以作为io流具体类的构造器的形参
2.1.2 常用方法:
  • 文件名/文件路径相关操作:
    • getName():获取文件名
    • getPath():获取文件路径
    • getAbsoluteFile():绝对文件名
    • getAbsolutePath():绝对文件路径
    • getParent():上一级目录
    • renameTo(File newName):重命名+移动,类似Linux中mv指令,只能进行同盘移动 eg:file1.renameTo(new File("G:/test/test.txt"))
  • 文件操作:
    • createNewFile() : 创建文件,eg:
      File file2=new File("g:\\hello.doc");
      file2.createNewFile();
      delete()
  • 目录操作
    • mkDir() : 上层文件目录需存在
    • mkDirs() : 如不存在上层文件目录,则创建
    • list() : 返回String
    • listFiles() : 返回File
  • 文件检测:
    • exists()
    • canWrite()
    • canRead()
    • isFile()
    • isDirectory()
    • lastModified()
    • length()

2.2 IO原理及流的分类

IO流的作用:处理设备之间的数据传输

按数据单位分:

四个基本抽象类:

抽象类
字节流(8bit)(InputStream,OutputStream)
字符流(16bit)(Reader,Writer)(多用在文本文件)
按流向:

input,output

按角色:

节点流(4个)和处理流(处理节点流))

节点流之文件流(文件流是一种节点流):

  • 字节流

    • FileInputStream
      要读取的文件要存在,否则FileNorFoundException
      使用步骤:
      1.创建文件对象
      2.创建流
      3.流操作
      4.关闭流
      read()—->!=-1;读取该byte,指针下移,到结尾时下个read()返回-1
      read(Byte[] b):一次读多个byte,存入b中,返回读取到的个数
      close();流使用完应该关闭,先关输出,再关输入
      置于try-catch-finally语句中,包装流的关闭正常执行

    • FileOutputStream
      物理文件不存在则创建,存在则覆盖
      输出流程同同输入类似
      write()
      write(Byte[] b)
      close()

  • 字符流,使用方法与字节流类似,byte[]—>char[]
    FileReader
    FileWriter

处理流:

  • 缓冲流(用来加速节点流):
    BufferedInputStream
    BufferedOutputStream flush();
    BufferedReader readLine()---->!=null
    BufferedWriter write(char c) write(String str) flush();

  • 转换流(前提是文件为文本文件):
    InputStreamReader : 解码,字节转字符
    InputStreamReader isr = new InputStreamReader(fis, "GBK");
    OutputStreamWriter : 编码,字符转字节
    OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK")

  • 标准输入输出流
    System.in:InputStream: 标准输入,从键盘输入数据
    System.out:PrintStream : 标准输出,从显示器输出数据

  • 打印流:
    字节流:PrintStream PrintStream ps=new PrintStream(OutputStream outputStream)
    PrintStream是OutputStream的一个子类
    System.out的out是一个OutputStream
    ps = new PrintStream(bos, true);
    System.setOut(ps);
    字符流:PrintWriter 与上同理

  • 数据流:(用来处理基本数据类型,String,字节数组)
    套接在节点流之上
    DateInputStream
    read方法需与write方法顺序一致,否则异常

    dos.writeUTF("This is UTF");
    dos.writeLong(12344);
    dis.readUTF();
    dis.readLong();
    boolean readBoolean();
    char readChar();
    double readDouble();
    long readLong();
    byte readByte();
    folat readFloat();
    short readShort();
    int readInt();
    String readUTF();
    void readFully(byte[] b);

    DateOutputStream()
    write方法,上面read改成write

  • 对象流(存取对象):
    序列化:将对象通过ObjectOutputStream转换为与平台无关二进制流,存储在硬盘文件中
    反序列化:从文件恢复对象

    要序列化的类需实现:
    1.类和属性都要实现Serializable或Externalizable接口
    2.提供一个版本号:public static final long serialVersionUID=xxxxxL;
    3.不能序列化用static或transient修饰的成员变量
    4.存几个就只能读几个

ObjectInputStream(InputStream in)
        readObject():Object
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream());
    ObjectOutputStream(OutputStream out)
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream());
        writeObject():Object
  • 随机存取文件流
    支持随机访问
    既可以充当输入流,也一充当一个输出流
    支持从文件任意位置的读取和写入
    若文件不存在,则创建;若不存在,则是覆写
RandomAccessFile()
    RandomAccessFile(String , String)
    RandomAccessFile(File , String)
    //r:读,rw:读写,rwd:读写+同步文件内容,rws:读写+同步文件内容+元数据
    raf.read();
    raf.write();
    long getFilePoint()//获取文件记录指针当前位置
    void seek(long pos)//将文件记录指针定位到pos位置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值