Description of the File

Let’s first look at the definition of the File class

File: Abstract representation of the path names of files and directories.

Well, File can be used to represent information about a File and its path. A folder is a special file.

All right, enough nonsense, you know File is a File, but let’s look at the API

The method name Methods described
canExecute() Tests whether the application can execute the file represented by this abstract pathname
canRead() Tests whether the file represented by this abstract pathname can be read by the application
canWrite() Tests whether the application can modify the file represented by this abstract pathname
createNewFile() A new empty file is created if and only if the file does not already exist
delete() Deletes the file or directory represented by this abstract pathname
deleteOnExit() When the virtual machine terminates, the file or directory represented by this abstract pathname is requested to be deleted
exists() Tests whether the file or directory represented by this abstract pathname exists
getAbsoluteFile() Returns the absolute pathname form of this abstract pathname
getAbsolutePath() Returns an absolute pathname string for this abstract pathname
getCanonicalFile() Returns the canonical form of this abstract pathname
getCanonicalPath() Returns a canonical pathname string for this abstract pathname
getFreeSpace() Returns the number of unallocated bytes in the partition specified by this abstract pathname
getName() Returns the name of the file or directory represented by this abstract pathname
getParent() Returns the pathname string of the parent directory of this abstract pathname; If the pathname does not specify a parent directory, null is returned
getParentFile() Returns the abstract pathname of the parent directory of this abstract pathname; If the pathname does not specify a parent directory, null is returned
getPath() Convert this abstract pathname to a pathname string
getTotalSpace() Returns the partition size specified by this abstract pathname
getUsableSpace() Returns the number of bytes available for this virtual machine on the partition specified by this abstract pathname
isAbsolute() Tests whether this abstract pathname is an absolute pathname
isDirectory() Tests whether the file represented by this abstract pathname is a directory
isFile() Tests whether the file represented by this abstract pathname is a standard file
isHidden() Tests whether the file specified by this abstract pathname is a hidden file
lastModified() Returns the last time the file represented by this abstract pathname was modified
length() Returns the length of the file represented by this abstract pathname
list() Returns an array of strings specifying the files and directories in the directory represented by this abstract pathname
list(FilenameFilter filter) Returns an array of strings specifying the files and directories in the directory represented by this abstract pathname that satisfy the specified filter
listFiles() Returns an array of abstract pathnames representing files in the directory represented by this abstract pathname
listFiles(FilenameFilter filter) Returns an array of abstract pathnames representing files and directories in the directory represented by this abstract pathname that satisfy the specified filter
listRoots() Lists the available file system roots
mkdir() Creates the directory specified by this abstract pathname
mkdirs() Creates the directories specified by this abstract pathname, including all required but nonexistent parent directories
renameTo() Renames the file represented by this abstract pathname
setExecutable(boolean executable) A convenient way to set execution permissions for this abstract pathname owner
setLastModified(long time) Sets the time when the file or directory specified by this abstract pathname was last modified
setReadable(boolean readable) A convenient way to set read permissions for this abstract pathname owner
setReadOnly() Marks the file or directory specified by this abstract pathname so that it can only be read
setWritable(boolean writable) A convenient way to set write permissions for this abstract pathname owner
toURI() Construct a file: URI that represents this abstract pathname
toURL() Obsolete method, file.touri ().torul () is recommended

The File exercise: List all File names in a folder

Need, print all file names in the file thinking: all files in the folder, judge if it is a file to print, if it is a folder repeat the above operation

So this is the kind of problem solving that we’ve been thinking about that can be done with a recursive algorithm, so let’s just look at it a little bit and call it a recursive algorithm.

recursive

The programming technique by which a program calls itself is called recursion. Recursion as an algorithm is widely used in programming languages. A procedure or function in the introduction to the definition or call their own, directly or indirectly, with a kind of method, it is usually the problem of a large complex layers into a similar to the original problem of smaller problems to solve, the recursion strategy only a small number of procedures can be required to describe the problem solving process of repeatedly calculation, greatly reduces the amount of program code. Recursion is the ability to define an infinite collection of objects in finite statements. In general, recursion requires boundary conditions, recursive forward sections, and recursive return sections. When the boundary condition is not satisfied, recursion advances. The recursion returns when the boundary condition is satisfied.

define

Recursion is calling yourself at run time

The conditions required to form recursion

  • The subproblem must be the same thing as the original problem and simpler;
  • You can’t call itself indefinitely, you have to have an exit

Recursive algorithms are generally used to solve the following three types of problems:

  • Data is defined recursively
  • The problem solution is realized by recursive algorithm
  • The structure of the data is defined recursively (e.g. linked lists, binary trees, generalized tables, etc.)

Disadvantages of recursion:

Recursive algorithm is less efficient than common algorithms such as common loop. Therefore, avoid recursion unless there is no better algorithm or a specific situation where recursion is more appropriate. In the process of recursive call, the system opens up a stack to store the return points and local quantities of each layer. Too many recursive times will easily cause stack overflow.

Recursive code implementation

private static void showDir(File dir) {
    System.out.println("Contents:" + dir);
    File[] files = dir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            showDir(file);
        } else{// List the root directory system.out.println ("files"+ file); }}}Copy the code

Properties

Well, this is a class I haven’t touched before that inherits from HashTable, which means it has the characteristics of a map collection, and the key-value pairs are strings.

What are its features? Why are we talking about sets here all of a sudden…

Let’s look at the JDK description ~

The Properties class represents a persistent set of Properties that can be stored in or loaded from a stream. Because Properties inherits from Hashtable, you can apply the PUT and putAll methods to Properties objects. These two methods are not recommended because they allow the caller to insert items whose key or value is not a String.

There are several important things we can read from this. Map collection, string key-value pairs, persistent attribute set. Well, I won’t keep you in suspense, but this thing can be used to set up configuration files.

Similar to SharedPreferences, you can use Properties to implement MySharedPreferences.

Here I hand lifted a MySharedPreference, has not been tested, we make do with a look, ha ha 😀

public class MySharedPreferences {
    private static MySharedPreferences mInstance;
    private final String mPath;

    public static MySharedPreferences getInstance() {
        if (mInstance == null) {
            synchronized (MySharedPreferences.class) {
                if(mInstance == null) { mInstance = new MySharedPreferences(); }}}return mInstance;
    }

    private MySharedPreferences() {
        mPath = Environment.getExternalStorageDirectory().getPath() + "SP.txt";
        File file = new File(mPath);
        if(! file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } public void putValue(String key, String value) { try { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(mPath,true));
            BufferedReader bufferedReader = new BufferedReader(new FileReader(mPath));
            Properties properties = new Properties();
            properties.load(bufferedReader);
            properties.setProperty(key, value);
            properties.store(bufferedWriter, "");
            bufferedWriter.flush();
            bufferedReader.close();
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public String getValue(String key) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(mPath));
            Properties properties = new Properties();
            properties.load(bufferedReader);

            bufferedReader.close();
            return properties.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }
        returnnull; }}Copy the code

MySharedPreference = MySharedPreference = MySharedPreference = MySharedPreference = MySharedPreference = MySharedPreference I don’t think it is necessary to apply for permission, so I don’t want to test it. You can tell me whether it requires permission according to your understanding of Android. As for performance issues, reading and writing SPS should be a low-frequency affair, so read and write streams are created when needed and closed when not.

IO stream and file operation learning here ~~ NIO concurrency class operation and so on later learn concurrency time again one-time literacy.