Dark Horse Programmer--file Class

Source: Internet
Author: User

------- Android Training , Java Training , look forward to communicating with you! ----------

File class

Used to encapsulate a file or folder as an object

Easy to manipulate files and files

The File object can be passed as a parameter to the constructor of the stream

The file class provides a number of useful operations for manipulating files, which provides a way to decompose the pathname to query the file system associated with the file that the path name refers to

A file object actually represents the path of a document, not the file itself

Common methods for File classes:

1, create

boolean createnewfile (); create a file in the development location, if the file already exists, is not created, returns false and the output stream is not the same, the output stream object is created and the file already exists and will overwrite.

Create folder:mkdir (); This method can only create a first level directory

mkdirs (); Create a Multilevel Catalog

2, delete

Boolean Delete (); Delete failed, return false

void Deleteonexit (); Delete the specified file when the program exits

3, judging

Whether CanExecute () can execute, test whether the application can execute the file represented by this abstract path name

Boolean exists (); Whether the file exists

boolean isdirectory (); Determine if it is a directory

boolean isfile (); determine if it is a file

Ishidden (); is a hidden file

boolean isabsolute (); is the absolute path

When you judge a file object for a file's directory and file, you must determine whether the file exists

4. Access to Information

The return type is a string

getName (); Get Name

GetPath (); Get Path

getParent (); Get Parent Directory This method returns the parent directory in the absolute path

If you get a relative path, the return is null

If there is a previous level of directory in the relative path, then the directory is the return result

GetAbsolutePath (); Get Absolute Parent directory

long LastModified (); Gets the time the file was last modified

long Length (); Get the size of a file

5, directory List

String[] List (): Returns an array of strings consisting of the names of all files or subdirectories in the directory that the file instance refers to. Returns null if the current file instance represents a normal document instead of a directory

File[] Listfiles (): If the file instance is not referring to a directory, this method returns NULL. Otherwise, an array of file objects is returned, each of which corresponds to each file or directory in the directory. If the directory is empty, then the array will also be empty

Example: List the files or folders in the specified directory, including the contents of subdirectories, that is, listing all content in the specified directory

because there are directories in the directory, just use the same function that lists the directory functions to complete

You can also call this function again if the directory appears in the listing process

which means that the function itself calls itself

This form of representation, or programming technique, is called recursion.

Recursive Note condition:

1. Qualifying Conditions

2. be aware of the number of recursion times and try to avoid memory overflow

To view all the directories in a recursive form:

import java.io.*;

class Dirdemo

{

public static void Main (string[] args)

{

file Dir=new file ("F:\\aa");

Showdir (dir);

}

public static void Showdir (File dir)

{

System.out.println (dir);

File[] Files=dir.listfiles ();

for (int x=0;x<files.length;x++)

   {

if (Files[x].isdirectory ())

Showdir (files[x]);

Else

System.out.println (files[x]);

}

}

}

Practice:

Stores the absolute path of a Java file in a specified directory into a text file

Create a Java file list file

Ideas:

1. recursion for the specified directory

2. get the path of all Java files in the recursive process

3. Store These paths in the collection

4. writing data from a collection to a file

Properties Overview :

Properties is a subclass of Hashtable

That is, it has the feature of the map set, and the key-value pairs stored in it are strings.

is a collection container in combination with IO technology

characteristics of the object: a configuration file that can be used in the form of a key-value pair then when the data is loaded, the data needs to have a fixed format key = value

SetProperty (); Set element

GetProperty (); get Element

Exercise: Used to record the number of application runs

If the number of uses has been reached, then give the registration prompt

It is easy to think of: counter,

However, the counter is defined in the program town of the southern gate, with the operation of the program exists, and the self-increment

But as the application exits, the counter disappears in memory.

The next time you start the program, start counting again from 0

This is not what we want, want to, program both to end, the value of this counter also exists

The next time the program starts, it loads the value of the counter and stores it again after 1 .

So create a configuration file to record the number of times the software is used

This profile uses key-value pairs, which makes it easy to read books and manipulate data

Key-value pair data is a map collection

Data is stored as a file, using IO technology

Then Map+io is the Properties .

------- Android Training , Java Training , look forward to communicating with you! ----------

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse Programmer--file Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.