First, File class
I. overview
File class: abstract representation of file and directory pathnames
2. Characteristics:
1) Used to encapsulate files or folders as objects
2) Easy to manipulate attribute information of files and folders
3) Instances of File classes are immutable; that is, once created, the abstract path names represented by File objects will never change.
4) File objects can be used as constructors of parameters passed to streams
II. File Object Creation
Way 1:
File f =new File("a.txt");
Encapsulate a.txt as a File object. Existing and non-existent files or folders can be encapsulated as objects.
Mode 2:
File f2=newFile("c:\\abc","b.txt");
Pass in the directory path where the file is located along with the file and specify the file path.
Mode 3:
File d=new File("c:\\abc");
File f3=new File(d,"c.txt");
Encapsulate file directory paths as objects. Create the file object again. Reduces the association of files with the parent directory.
Little knowledge:
File.separator represents a directory separator that can be used across platforms. It's equivalent to "\" (parallel slash \\\\\ in windows represents the escaped separator, but not in linux system).
3. Common methods of File class
1. Create
booleancreateNewFile();
Create a file at the specified location, and if the file already exists, do not create it, return false. Unlike the output stream, the output stream object creates a file as soon as it is created. And the file already exists and will be overwritten.
boolean mkdir(); // Create folders, only one folder can be created
Example:
File dir=new File("abc");
dir.mkdir(); //Create the abc folder
boolean mkdirs(); // Create multilevel folders
2, delete
boolean delete();
// Delete files or directories. The file exists, returns true; the file does not exist or is being executed, returns false.
void deleteOnExit(); // Delete the specified file when the program exits
3, judgement
boolean canExecute(); // Is it an executable?
boolean exists(); // Does the file exist?
boolean isFile(); // Is it a file?
Boolean is Directory (); // Is it a folder?
Boolean is Hidden (); // Is it a hidden file?
Boolean is Absolute (); // Is the file an absolute path?
Remember: When deciding whether a file object is a file or a directory, you must determine whether the encapsulated content of the file object exists. Judge by exists.
4. Access to Information
String getName(); // Get the filename
String getPath();
// Get the relative path of the file (that is, get whatever the parameters of the created object are passed in)
String getParent();
Get the file parent directory. Returns the parent directory in the absolute path. If the relative path is obtained, return null. If there is an upper directory in the relative path, the directory returns the result.
String getAbsolutePath(); // Absolute path to get files
long lastModified(); // Return the last time the file was modified
long length(); //Return file length
5. Listing Files and File Filtering
static File[] listRoots(); // List the available file system root directoriesThat is, the system disc character
String[] list();
List all files in the current directory, including hiding. The file object that calls the list method must encapsulate a directory. The directory must also exist.
String[]list(FilenameFilter filter);
// Returns an array of strings to retrieve files or directories in the directory that satisfy the specified filter.
Filename Filter: File name filter is an interface that contains a method, accept(Filedir,String name), which returns boolean type and filters out unqualified files.
File[] listFiles(); // Returns an abstract path name array to retrieve all files and folders under the current folder
File [] ListFiles (Filename Filter filter); // Returns an array of abstract path names to obtain the files or directories in the directory that satisfy the specified filter.
Four, recursion
1, definition
When each loop in a function can also call this function to achieve, that is, the function itself calls itself. This form of expression, or programming technique, is called recursion.
2. Recursive considerations
(a) Limited conditions. To end the loop call, otherwise it's a dead loop.
b. Pay attention to the number of recursions and try to avoid memory overflow. Because each call itself will first execute the next call to its own method, so it will continue to open up new space in stack memory, too many times, will lead to memory overflow.
Example 1
Example two
Example three
Lecture 2: Properties class
I. overview
1. Properties is a subclass of Hashtable, which has the characteristics of Map set. It also has stored key-value pairs, which are strings without generic definitions. It is the collection container that the collection neutralization and IO technology want to combine.
2. Characteristics:
Configuration files that can be used in the form of key-value pairs
2) When loading, data needs to be in a fixed format, commonly used as key = value
2. Special methods
1, setting up
Object setProperty(String key,String value);
// Set keys and values, call the Hashtable method put
2. Get
String getProperty(String key);
//Specify key search value
Set<String> stringPropertyName();
// Returns the key set of the list of attributes and stores it in the Set set
3. Loading flow and storage inflow
void load(InputStream ism);
Read the list of attributes (key and element pairs) from the input byte stream. It is also called loading data from streams into collections.
void load(Readerreader);
Read the list of attributes (key and element pairs) from the input character stream. It is also called loading data from streams into collections.
voidlist(PrintStream out); // Output the list of attributes to the specified output stream
void store(OutputStreamout,String comments);
The // corresponding load(InputStream) writes the list of attributes (key-value pairs) to the output stream. Description of the comments attribute list.
void store(Writerwriter, String comments);
The // corresponding load(Reader) writes the list of attributes (key-value pairs) to the output stream. Description of the comments attribute list.
Example
Practice:
Lecture 3 Print Stream
I. overview
Print Stream and Print Writer
2. The stream provides a printing method that prints all types of data as they are.
2. Byte Print Stream
The type of parameter that can be received in the construction method:
File objects. File
String Path: String
3. Character Output Stream
3. String Print Stream: PrintWriter
Acceptable parameter types in construction methods
File object: File
String Path: String
3. Byte Output Stream
4. Character Output Stream: Writer
Example
Sequence Flow
I. overview
1. SequenceInputStream merges multiple streams. Also known as merge flow.
2. Common constructors
SequenceInputStream(Enumeration<?extends FileInputStream> e)
2. Common steps for merging multiple stream files
Create collections and add stream objects to collections
2. Create Enumeration objects and add collection elements.
3. Create SequenceInputStream objects and merge stream objects
4. Create Write Stream Objects and FileOutputStream Associates Write Files
5. Read and write data repeatedly using SequenceInputStream object and FileOutputStream object.
Example:
Practice: