博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
File类常见方法
阅读量:2339 次
发布时间:2019-05-10

本文共 2445 字,大约阅读时间需要 8 分钟。

import java.io.*;/*File类常见方法:1,创建。    boolean createNewFile();在指定位置创建文件,如果该文件已经存在,则不创建,返回false。    和输出流不一样,输出流对象一建立创建文件。而且文件已经存在,会覆盖。    boolean mkdir():创建文件夹    boolean mkdirs():创建多級文件夹2,删除。    boolean delete();删除失败返回false。    void deleteOnExit():在程序退出时删除指定文件3,判断。    boolean exists()  :文件是否存在    isDirectory()    isFile()    isHidden();    isAbsolute()  是否绝对路径4.获取信息    getName():    getPath():    getParent():    getAbsolutePath()     lastModified()  最后修改时间    length() 文件大小*/class  FileDemo{    public static void main(String[] args) throws IOException    {        //consMethod();        method_5();    }    public static void method_5()    {        File f1 = new File();        File f2 = new File();        sop("raname:"+f1.renameTo(f2));    }    public static void method_4()    {        File f = new File("c:\\a.txt");        sop("path:"+f.getPath());        sop("abspath:"+f.getAbsolutePath());        sop("parent:"+f.getParent());//该方法返回的是绝对路径中的父目录。如果获取的是相对路径,返回null.            //如果相对路径中有上一层目录那么该目录就是返回结果。    }    public static void method_3() throws IOException    {        File f = new File("file.txt");    //記住在判断文件对象是否是文件或者目录时,必须要先判断该文件对象封装的内容是否存在    //通过exists判断        sop("dir:"+f.isDirectory());        sop("file:"+f.isFile());        sop("Absolute:"+f.isAbsolute());//  是否绝对路径    }    public static void method_2()    {        File f = new File("file.txt");        //sop("execute:"+f.canExecute());        File dir = new File("abc");        sop("mkdir:"+dir.mkdir());    }    public static void method_1() throws IOException    {        File f = new File("file.txt");        f.deleteOnExit();//在程序退出时删除        //sop("create:"+f.createNewFile());        sop("delete:"+f.delete());    }    public static void consMethod()    {        System.out.println("Hello World!");        //创建File对象        //将a.txt封装成file对象。可以将已有的和未出现的文件或者文件夹封装成对象。        File f1 = new File("C:\\abc\\a.txt");//File(String pathname)         File f2 = new File("c:\\abc","b.txt");//File(String parent, String child) ,根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。        File d = new File("C:\\abc");        File f3 = new File(d,"c.txt");//File(File parent, String child)         sop("f1:"+f1);//打印出来的是路径        sop("f2:"+f2);        sop("f3:"+f3);        File f4 = new File("c"+File.separator+"abc"+File.separator+"zzz"+File.separator+"a.txt");//File.separator分隔符,可以实现跨平台    }    public static void sop(Object obj)    {        System.out.println(obj);    }}

转载地址:http://twzvb.baihongyu.com/

你可能感兴趣的文章
Maplab开源VI-SLAM框架介绍
查看>>
maplab(1):安装
查看>>
陀螺仪随机误差的Allan方差分析
查看>>
Ubuntu 64位安装Adobe Reader 9.5.5
查看>>
Ubuntu 下如何查看已安装的软件
查看>>
Linux 系统下可以注释标注的pdf阅读器安装、比较和推荐
查看>>
福昕阅读器foxit reader Linux版
查看>>
Ubuntu 安装百度云客户端
查看>>
每天一个linux命令:locate
查看>>
Linux 环境下载百度云资源,Firefox插件(百度网盘助手)
查看>>
ubuntu Firefox/chrome adobe flash 插件安装
查看>>
OpenCV图像变换(仿射变换与透视变换)
查看>>
仿射变换与透视变换
查看>>
Ubuntu 16.04 上安装 CUDA 9.0 详细教程
查看>>
Verify You Have a CUDA-Capable GPU
查看>>
ROS中OpenCV的使用——人脸检测
查看>>
ROS学习笔记(1):在ROS中使用OpenCV进行简单的图象处理--原理篇
查看>>
ROS学习笔记(2):在ROS中使用OpenCV进行简单的图像处理---代码实现篇
查看>>
C语言中声明和定义详解
查看>>
ros代码中添加使用opencv库,cv::Mat和ros image之间的相互转换
查看>>