• 技术文章 >java >java基础

    java中File类的使用

    小妮浅浅小妮浅浅2021-02-09 14:05:24原创3925

    本教程操作环境:windows7系统、java10版,DELL G3电脑。

    1.File类概念

    File类是用来操作文件的类,但它不能操作文件中的数据。

    public class File extends Object implements Serializable, Comparable<File>

    File类实现了Serializable、 Comparable<File>,说明它是支持序列化和排序的。

    2. FileInputStream 和 FileOutPutStream概念

    FileInputStream是文件字节输入流,就是对文件数据以字节的方式来处理,如音乐、视频、图片等。

    FileOutPutStream是文件字节输出流,

    3.FileInputStream里面的方法

    //通过文件的名字来创建一个对象
    public FileInputStream(String name) throws FileNotFoundException{}
    //通过File对象来创建一个对象
    public FileInputStream(File file) throws FileNotFoundException{}
     /**
       * 通过FileDescriptor来创建一个对象
       * FileDescriptor是一个文件描述符号
       * 有in,out,err三种类型
       * in:标准输入描述符,out:标准输出的描述符,err:标准错误输出的描述号
       */
    public FileInputStream(FileDescriptor fdObj){}
    //打开指定的文件进行读取 ,是java和c之间进行操作的api 我们并不会用到
    private native void open0(String name){}
    //打开指定的文件进行读取,我们并不会用到 因为在构造方法里面帮我们打开了这个文件
    private void open(String name){}
    //从输入流中读取一个字节的数据,如果到达文件的末尾则返回-1
    public int read() throws IOException{}
    //读取一个字节数组
    private native int readBytes(byte b[], int off, int len) throws IOException;
    private native int read0() throws IOException;
    //从输入流中读取b.length的数据到b中
    public int read(byte b[]) throws IOException{}
    //从输入流中读取off到len之间的数据到b中
    public int read(byte b[], int off, int len) throws IOException{}
    //跳过并丢弃输入流中的n个数据
    public long skip(long n) throws IOException{}
    private native long skip0(long n) throws IOException;
    //可以从此输入流中读取的剩余字节数
    public int available() throws IOException {}
    private native int available0() throws IOException;
    //关闭此文件输入流并释放与该流关联的所有系统资源
    public void close() throws IOException {}
    //返回FileDescriptor对象 
    public final FileDescriptor getFD() throws IOException{}
    //该方法返回与此文件输入流关联的通道 NIO中会用到 本文不会提及
    public FileChannel getChannel(){}
    private static native void initIDs();
    private native void close0() throws IOException;
    //没有更多引用时,调用此方法来关闭输入流 一般不使用
    protected void finalize() throws IOException {}

    以上就是java中File类的使用方法,在掌握基础的File类概念后,可以就File类进一步拓展知识点,对其中常见的子类进行学习与掌握。

    专题推荐:java file类的使用
    品易云
    上一篇:java字符流和字节流替换方法 下一篇:java IO流效率对比

    相关文章推荐

    • java中IO的分类• java IO流有几种• IO字节流在java中的使用• java IO字符流的用法• java字符流和字节流替换方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网