zoukankan      html  css  js  c++  java
  • 5.6Java File ApI

    5.6Java File ApI

    File

    API说明
    pathSeparator separator 路径|路径分隔符
    File(String parent.Srting child) File(File parent, String child) File(String name) 构造器 没有盘符的话以user.dir作为相对目录
    getName() getPath(构建的时候是什么路径(相对路径or绝对路径),返回时就是什么路径) getAbsolutePath(返回过来的始终是一个绝对路径) getParent(有父路径就返回,没有就返回为空) 文件名、路径名
    exists(判断是否存在路径)  isFile() isDirectory() 判断状态
    length(文件的时候可以返回文件大小,文件夹的话会返回0,不存在也会返回0)---文件夹递归,查看子孙集... 文件长度
    createNewFile(如果存在创建失败)  delete() 创建新文件 删除文件

    测试具体的方法:

    package ioStudy;

    import java.io.File;

    /**
    * 测试File的一些方法
    * getName()
    * getPath()--->构建的时候是什么路径返回的就是什么路径
    * getAbsolutePath()--->始终返回的是绝对路径
    * getParent()
    * @author Lucifer
    */
    public class TestIoNo2 {
       public static void main(String[] args) {
           /*构建File对象*/
           File src = new File("D:/workspace/practice/main/java/ioStudy/Trump.jpg");

           //测试getName方法
           System.out.println("名称" + src.getName());

           //测试getPath方法
           System.out.println("路径" + src.getPath()); //因为创建对象的时候给的是绝对路径,所以返回的是绝对路径

           //测试getAbsolutePath方法
           System.out.println("路径" + src.getAbsolutePath());

           //测试getParent方法
           src = new File("D:/workspace");
           System.out.println("父路径" + src.getParent()); //文件的上一级路径
           /*
           1.如果父路径没有,返回null
           2.如果只有一层目录,返回null--->相当于位于'/'下
            */

           //返回父File对象
           System.out.println(src.getParentFile()); //在这里是返回盘符
      }
    }

    文件状态的方法测试:

    package ioStudy;

    import java.io.File;

    /**
    * 测试文件状态
    * 1.判断是否存在:exists
    * 2.存在
    * 1.判断是文件:isFile
    * 2.判断是文件夹:isDirectory
    */
    public class TestIoNo3 {
       public static void main(String[] args) {

           /*创建File对象---实例化调用方法*/
           File src = new File("ioStudy/Trump.jpg");
           System.out.println(src.getAbsolutePath()); //相对路径返回user.dir提出来

           //在路径不存在的前提下判断是否是文件
           System.out.println("是否是文件:" + src.isFile());

           //在路径不存在的前提下判断是否是文件夹
           System.out.println("是否是文件夹:" + src.isDirectory());

           //分隔符
           System.out.println("----------");

           //判断是否存在
           src = new File("D:/workspace/practice/main/java/ioStudy/Trump.jpg");
           System.out.println("是否存在:" + src.exists());

           //路径存在
           System.out.println("是否是文件:" + src.isFile());

           //判断是否是文件夹
           System.out.println("是否是文件夹:" + src.isDirectory());

           ///分隔符
           System.out.println("----------");

           //构建目录路径
           src = new File("D:/workspace/practice/main/java/ioStudy");
           System.out.println(src.exists());
           System.out.println(src.isFile()); //不是文件---true
           System.out.println(src.isDirectory());

           //分隔符
           System.out.println("----------");

           //后期常用处理方法--->文件状态
           src = new File("D:/workspace/practice/main/java/ioStudy/Trump.jpg");
           //判断--->会有一个空指针的引用
           src = new File("D:/workspace/pra");
           if (src == null || !src.exists()){
               System.out.println("文件不存在---抛出异常");
               throw new RuntimeException("文件不存在异常");
          }else {
               if (src.isFile()){
                   System.out.println("文件操作");
              }else {
                   System.out.println("文件夹操作");
              }
          }
      }
    }

     

    It's a lonely road!!!
  • 相关阅读:
    1014 Waiting in Line (30)(30 point(s))
    1013 Battle Over Cities (25)(25 point(s))
    1012 The Best Rank (25)(25 point(s))
    1011 World Cup Betting (20)(20 point(s))
    1010 Radix (25)(25 point(s))
    1009 Product of Polynomials (25)(25 point(s))
    1008 Elevator (20)(20 point(s))
    1007 Maximum Subsequence Sum (25)(25 point(s))
    1006 Sign In and Sign Out (25)(25 point(s))
    1005 Spell It Right (20)(20 point(s))
  • 原文地址:https://www.cnblogs.com/JunkingBoy/p/14736461.html
Copyright © 2011-2022 走看看