文件

作者: itachi | 来源:发表于2016-10-18 22:46 被阅读59次

    <small>

    文件 目录(信息)

    文件目录信息类

    new File(String pathName)

    此构造绝对不是创建文件,仅仅表示根据抽象路径pathName,创建一个File文件实例
    File实例,并不能说明文件就真实存在,只是用来判断文件目录是否真实存在

    boolean createNewFile()

    创建文件并返回是否创建成功

    boolean delete()

    删除文件

    boolean isFile()

    判断是否为文件

    boolean isDirectory()

    判断是否为目录
    程序实例:

    String pathName = "E:\\ksxx\\所有知识点讲解\\21\\demo.txt";
            //创建一个File实例
            File file = new File(pathName);
            boolean exists()
             */
            boolean isExists = file.exists();
            System.out.println("文件是否存在:"+isExists);
            if(!isExists){
                boolean isCreateFileOk = 
                        file.createNewFile();
                System.out.println(isCreateFileOk?"创建文件成功":"创建文件失败");
            }else{
                boolean isDeleteFileOk = file.delete();
                System.out.println(isDeleteFileOk?"删除文件成功":"删除文件失败");
            }
    pathName = "E:\\";
            file = new File(pathName);
            System.out.println("E:\\存在:"+file.exists());
    boolean isFile = file.isFile();
            System.out.println(isFile?"E:\\是文件":"E:\\不是文件");
    boolean isDirectory = file.isDirectory();
            System.out.println(isDirectory?"E:\\是目录":"E:\\不是目录");
    file = new File("/");
            System.out.println(file.isDirectory()?"/是目录":"/不是目录");
    pathName = "E:"+File.separator+"ksxx"+File.separator+
                        "所有知识点讲解"+File.separator+
                        "21"+File.separator+"demo.txt";
            System.out.println(pathName);
    pathName = "../demo02.txt";
            file = new File(pathName);
            file.createNewFile();//创建当前目录下的demo01.t
    

    1、windows与linux的区别:
    * widows中,目录的表示为
    * 没有根目录,但有盘符,
    * 如:C:\、D:\、E:\、F:\、U盘等
    * linux中,目录的表示为 /
    * 没有盘符概念,但有且只有一个根目录。
    * 所有的路径都是从根目录出发。
    * U盘被视为挂载点。
    *
    * 不管是什么系统的服务器,
    * 可以利用File.separator来实现目录的自动辨析。
    * 当遇到是windows系统服务器,就会自动填充
    * 当遇到是linux系统服务器,就会自动填充 /

    /*
    * 2、/、./与../的区别
    * 如果后面不跟内容,点后面的/可以省略(建议不要省略)
    *
    * / 根路径
    * ./(可以直接省略) 用来表示当前目录下
    * [./]../ 用来表示上级目录
    *
    * 绝对路径:
    * 以 / 开头的路径。
    *
    * 相对路径:
    * 以./货../ 开头的路径。

    long length()

    获得文件的大小(字节量)

    lastModified()

    获得文件最后一次修改时间

    public class FileDemo02 {
        public static void main(String[] args) throws IOException {
            /*
             * File:文件目录信息类
             *   查询文件信息方法
             */
            String pathName = 
                    "E:\\ksxx\\所有知识点讲解\\21\\demo.txt";
            //创建一个File实例
            File file = new File(pathName);
            /*
             * 6、获得文件的大小(字节量)
             *    long length()
             */
            long bytes = file.length();
            System.out.println("demo.txt文件大小:"+bytes);
            
            /*
             * 7、获得文件的最后一次修改时间
             */
            long lastModifiedTime = file.lastModified();
            System.out.println("demo01.txt最后一次修改时间:"+new Date(lastModifiedTime));
            
            System.out.println("是否为可读文件:"+file.canRead());
            System.out.println("是否为可写文件:"+file.canWrite());
            System.out.println("是否为可执行文件:"+file.canExecute());
            System.out.println("是否为隐藏文件:"+file.isHidden());
            
            System.out.println("获得文件名:"+file.getName());
            System.out.println("获得目录名:"+file.getParent());
            System.out.println("获得抽象路径:"+file.getPath());
    
    boolean midir();

    只能创建单级目录,只有上级目录存在的前提下,才能创建单级目录。如果多级目录不存在,则无法创建。

    boolean mkdirs

    可以创建多级目录

    RandomAcessFile

    文件读写操作类:可以通过File实例对文件实现读写操作。
    RandomAccessFile的构造及注意事项
    * RandomAccessFile(String name,String mode)
    * RandomAccessFile(File file,String mode)
    *
    * 构造二,其实是对构造一的封装。
    * RandomAccessFile(new File(name),String mode)
    *
    * 构造参数:
    * 第一个参数:字符串类型的抽象路径
    * 也可以是抽象路径封装好的File实例。
    *
    * 第二个参数:读写模式
    * 读:r (read)
    * 写:w (write)
    *
    * RandomAccessFile对象创建的
    * 注意事项:
    * 1、文件路径(文件所在的目录)非真实存在,
    * 会抛出异常:FileNotFoundException
    * 2、文件路径(文件所在的目录)真实存在,
    * 只不过是文件不存在:
    * ①、只读模式,必须要求文件存在,
    * 否则抛出FileNotFoundException
    * ②、读写模式,文件不存在,
    * 会自动创建文件。

    RandomAccessFile(String name,String mode)
    RandomAccessFile(File file,String mode)
    String pathName = "./demo01.txt";
            RandomAccessFile raf = 
                    new RandomAccessFile(pathName,"rw");
            //或者
            File file = new File(pathName);
            raf = new RandomAccessFile(file,"rw");
    

    文件的读写

    单个字节的读写
    String pathName = "./demo01.txt";
            RandomAccessFile raf = 
                    new RandomAccessFile(pathName,"rw");
    raf.write('a');//a
            raf.write(353);//其实写进去还是a
            raf.write(865);//其实写进去还是a
    int read1 = raf.read();
    //      System.out.println(read1);//-1表示读取到文件末尾
    raf.close();//读写完毕后,需要关闭io通道在一定意义上,就是Ctrl+S保存
    raf = new RandomAccessFile(pathName,"rw");
            int read1 = raf.read();
            System.out.println(read1);//97
            
            int read2 = raf.read();
            System.out.println(read2);//97
            
            int read3 = raf.read();
            System.out.println(read3);//97
            
            int read4 = raf.read();
            System.out.println(read4);//-1
            
            raf.close();
    

    文件的复制

    单字节复制
    RandomAccessFile file = 
                    new RandomAccessFile("16.rar","rw");
            RandomAccessFile copyFile = 
                    new RandomAccessFile("16_copy.rar","rw");
            
            long startTime = System.currentTimeMillis();
            /*
             * 要对文件实现单字节的读写操作,
             *  必须使用循环来完成。
             */
            int read = 0;
            //对file进行单字节读取操作
            while((read = file.read()) != -1){
                //读一个字节
                //对copyFile进行单字节写操作
                copyFile.write(read);//写
            }
            
            //读写完毕,要关流
            file.close();
            copyFile.close();
            long endTime = System.currentTimeMillis();
            //969128ms
            System.out.println("本次拷贝,用时:"+(endTime - startTime)+"ms");
    
    字节数组的复制
    public static void main(String[] args) throws IOException {
            /*
             * 通过RandomAccessFile批量字节的读写操作
             *      来完成对文件16.rar的拷贝。
             *      目标文件为16_copy.rar
             *      
             *      实现步骤:
             *          先对16.rar文件进行批量字节读取。
             *          读取的字节,
             *          向16_copy.rar中写。
             */
            RandomAccessFile file = 
                    new RandomAccessFile("16.rar","rw");
            RandomAccessFile copyFile = 
                    new RandomAccessFile("16_copy2.rar","rw");
            
            long startTime = System.currentTimeMillis();
            /*
             * 要对文件实现单字节的读写操作,
             *  必须使用循环来完成。
             */
            int read = 0;
            byte[] buf = new byte[1024];
            //对file进行字节数组读取操作
            while((read = file.read(buf)) != -1){
                //读字节数组
                //对copyFile进行字节数组写操作
                copyFile.write(buf);//写
            }
            
            //读写完毕,要关流
            file.close();
            copyFile.close();
            long endTime = System.currentTimeMillis();
            //1158ms
            System.out.println("本次拷贝,用时:"+(endTime - startTime)+"ms");
    

    字节数组的复制效率高很多

    总结:
    file只是文件目录的信息类,根据抽象路径,创建file实例,通file实例,可以查询文件,目录的相关信息
    file不代表文件本身,并不能对文件实现读写操作

    相关文章

      网友评论

        本文标题:文件

        本文链接:https://www.haomeiwen.com/subject/kcctuttx.html