(28) and the generic use of the File class

Generics

1, the concept of generics

Generic Java is a new feature introduced J2 SE1.5, which is essentially a parameterized type, that is to say the operation of the data type is specified as a parameter (type parameter) parameter type may be used in this class, interface, and create a method, called generic class, generic interfaces, generic method.

2, the use of generics in the collection, summary:

①, or a set of classes in the set of interfaces jdk5 belt structure is modified to have generic

②, Note: The generic type must be a class, not the basic data types.

③, if instantiated, generic type not specified. The default type is java.lang.Object type

3, the use of generics should be noted :

①, static methods can not be used like generic (generic must first create an object re-use)

②, an exception can not be used in a generic class

4, generic re-used in the method are as follows :

//泛型方法:在方法中出现了泛型的结构,泛型参数与类的泛型参数没有任何关系
public <E> list<E> copyFromArrayToList(E[] arr){
      ArrayList<E> list = new ArrayList<>();
      for(E e : arr){
            list.add(e);
      }
      return list;
}

5, reflected in the succession of generics

  • While class C is the parent class of class D, but G <C> and G <D> do not have both parent child relationships, both are juxtaposed relationship, as follows:
List<Object> list1 = null;
List<String> list2 = null;
//此时的list1和list2的类型不具有子父类关系
list1 = list2;(报错)
反证法:
      假设list1 = list2
      list.add(123);//导致混入非String的数据,出错

NOTE: C is the parent class of class D, C <G> is D <G> parent class, as follows:

AbstarctList<String> list1 = null;
List<String> list2 = null;
ArrayList<String> list3 = null;
list1 = list3;
list2 = list3;

6, using wildcards

  • Wildcards:?
    A class is a superclass of class B, G <C> and G <D> is not related, both common parent class is: G <?>

    Add : <?> The List will not be able to add to its internal data, in addition to adding null

    Acquires (reads) : allowed to read data, the read data type Object

  • There are limitations to the use of wildcards
    the extends C? :
    G <? C the extends> as G <C> and G <D> of the parent class, where D is a subclass of C

    ? Super C :
    G <Super C?> As G <C> and G <D> of the parent class, where D is the parent of C


Use the File class:

1, an object of the File class represents a file or a directory (commonly known as: Folder)

2, File class declaration in the package java.io

①, how to create an instance of the File class

File(String filePath);
File(String parentPath, String childPath);
File(File parentFile, String childPath);

②, the relative path : Compared to a particular path, specify the path to
       absolute path path to the file or directory that contains the letter, including:

③, path separator

windows : \\
unix : /

3, File class acquisition function

public String getAbsolutePath(): 获取绝对路径
public String getPath(): 获取路径
public String getName():获取名称
public String getParent():获取上层文件目录路径。若无,返回null
public long length():获取文件长度(字节数)。不能获取目录的长度
public long lastModified():获取最后一次的修改时间,毫秒值

public String[] list():获取指定目录下的所有文件或者文件目录的名称数组
public File[] listFiles():获取指定目录下的所有文件或者文件目录的File数组
public boolean renameTo(File dest):吧文件重命名为指定的文件路径比如:file1.renameTo(file2)为例:要想保证返回true,需要file1在硬盘中是存在的,且file2不能在硬盘中存在

public boolean isDirectory():判断是否是文件目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判读是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏

pulic boolean createNewFile():创建文件。若文件存在,则不创建,返回false
public boolean mkdir():创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建
public boolean mkdirs():创建文件目录。如果上层文件目录不存在,一并创建
public boolean delete():删除文件或文件夹(注意:Java中的删除不走回收站)

The next chapter, (29) use of the Java IO streams

Published 67 original articles · won praise 19 · views 9872

Guess you like

Origin blog.csdn.net/qq_41530004/article/details/104015597