An overview
Problems that have been learned by common objects for APIs:
String string--manipulate text data.
The string buffer---container, which can store many arbitrary types of data--strings.
The basic data type wrapper class---resolves the conversion between the string and the base data type "123"--parseint--123. It also provides more operations to the basic data. --tobinarystring ()
Set frame--more objects, easy to store after operation. Add put Collection--iterator Map-set-iterator
Math: Solving math operations. Random number.
System: Gets the information about the systems properties. You can get the system delimiter.
Date,dateformat, Calendar: Resolves a date and time issue.
You want to persist the data for such operations. Requires some technology to complete the operation of data on other devices.
IO technology is needed at this point. The operation of IO technology is provided in Java (which ultimately relies on the operating system).
The data is eventually persisted to the hard disk, which is the file.
Understand the file.
Two Hierarchy Chart
Three-byte read-write and anomaly graphs
The constructor of the four File4.1 file
Package Cn.itcast.io.a.file;import Java.io.File; Public classFiledemo {//private static final String File_separator = System.getproperty ("File.separator"); /** * @param args*/ Public Static voidMain (string[] args) {/** constructor of the file class. * How to create a file object. */String PathName="E:\\java_code\\day22e\\hello.java"; File F1=NewFile (PathName);//encapsulates the Test22 file as a Files object. Note that there is no file or folder that can be encapsulated and become an object. System. out. println (F1); File F2=NewFile ("e:\\java_code\\day22e","Hello.java"); System. out. println (F2); //encapsulates the parent as a file object. File dir =NewFile ("e:\\java_code\\day22e"); File f3=NewFile (dir,"Hello.java"); System. out. println (F3); //file F4 = new file ("E:" +file_separator+ "Java_code" +file_separator+ "day22e" +file_separator+ "Hello.java"); File f5 =NewFile ("e:"+file.separator+"Java_code"+file.separator+"day22e"+file.separator+"Hello.java"); }}
4.2 File Method
Package Cn.itcast.io.a.file;import java.io.file;import java.text.dateformat;import java.util.Date; Public classFilemethoddemo {/** * @param args*/ Public Static voidMain (string[] args) {/** Method Demo for file class. * Get information about the file. Name, size, time. * */File File=NewFile ("Test22.java"); String Abspath=File.getabsolutepath (); String Path= File.getpath ();//The path that is encapsulated in file is what gets to it. String filename =File.getname (); LongSize =file.length (); LongTime =file.lastmodified (); System. out. println ("abspath="+Abspath); System. out. println ("path="+path); System. out. println ("filename="+filename); System. out. println ("size="+size); System. out. println ("time="+Time ); //millisecond value--date--formatting--string literalsString str_date = dateformat.getdatetimeinstance (dateformat.long,dateformat.long). Format (NewDate (time)); System. out. println (str_date); }}
File or file plus operation
Package cn.itcast.io.a.file;import Java.io.file;import java.io.IOException; Public classFileMethodDemo2 {/** * @param args * @throws ioexception*/ Public Static voidMain (string[] args) throws IOException {/** Method 2 in the file class. * * file or file plus operation. */File File=NewFile ("E:\\file.txt"); /** Create a file if the file does not exist, create true if the file exists, then do not create false. If the path is wrong, IOException. */Boolean B1=File.createnewfile (); System. out. println ("b1="+B1); //deleted. //Boolean b2 = File.delete ();//Note: Do not go to the Recycle Bin. Use with caution. //System.out.println ("b2=" +b2); //you need to determine if the file exists. //Boolean b3 = File.exists (); //System.out.println ("b3=" +b3); //Create, delete, and Judge directory operations. File dir =NewFile ("E:\\ABC"); //Boolean b4 = Dir.mkdirs ();//Create a multilevel catalog. //Dir.mkdirs (); //System.out.println ("b4=" +b4);boolean B5= Dir.delete ();// When you delete a directory, you cannot delete it directly if there is content in the directory . // Ensure that the directory is empty only after you delete the contents of the directory. This directory can then be deleted . System. out. println ("b5="+b5); System. out. println ("----------- judgment file, directory ------------"); File F=NewFile ("E:\\javahaha");//to determine whether it is a file or a directory, you must first determine the existence. //F.mkdir ();F.createnewfile (); System. out. println (F.isfile ()); System. out. println (F.isdirectory ()); }}
Listfiles () method @ Get File Order
Package Cn.itcast.io.a.file;import Java.io.File; Public classFileMethodDemo3 {/** * @param args*/ Public Static voidMain (string[] args) {//requirement: Get internal content for a given directory. File dir =NewFile ("E:\\java_code"); //robustness of judgment. 1, must be present, 2, must be a directory. Otherwise, it is easy to raise the return array to null, which appears nullpointerexceptionstring[] names = Dir.list ();//gets the name of the current file and folder under the directory. //System.out.println (names); for(String name:names) {//System.out.println (name);} file[] Files= Dir.listfiles ();//gets the current file and the file object under the directory. for(File file:files) {System. out. println (File.lastmodified ()); } }}
Filter (filter files or folders)
Package cn.itcast.io.a.file;import Java.io.file;import cn.itcast.io.b.filter.filenamefilterbysuffix; Public classFileMethodDemo4 {/** * @param args*/ Public Static voidMain (string[] args) {/** Requirement 2: Gets the contents of the directory, but as long as the. java file. */File dir=NewFile ("e:\\java_code\\day06"); file[] Files= Dir.listfiles (NewFilenamefilterbysuffix (". JPG"));//a file name filter FilenameFilter object needs to be passed. /*listfiles Source code parsing. Public file[] Listfiles (filenamefilter filter) {//filenamefilter filter = new Filenamefilterbyjava () String ss[ ] = list ();//Call the list () in the file class to get all the names of the array ss. if (ss = = null) return null;//robustness judgment, returns if the array is null. arraylist<file> files = new arraylist<> ();//Create a collection. The element is a file type. for (String s:ss)//Iterate over the name array. if ((filter = = null) | | Filter.accept (this, s))//The filter is judged by the name of the traversal. The current directory, this, is traversed to the name S passed to the Accept method. Once the condition satisfies the filter filter condition. Files.add (The new File (S, this)), or//To add the filter criteria to the collection. Add is to encapsulate the file name and the current directory into a Files object. New File (Dir,name); Return Files.toarray (New File[files.size ()));//Returns the set to the array, why? No additions or deletions are required. } */ for(File file:files) {System. out. println (File.getname ()); } }}
Import Java.io.File;
Import Java.io.FilenameFilter;
/**
* Used to filter files with the specified extension.
* @author Teaching
*
*/
public class Filenamefilterbysuffix implements FilenameFilter {
private String suffix;
Public filenamefilterbysuffix (String suffix) {
Super ();
this.suffix = suffix;
}
@Override
Public Boolean Accept (File dir, String name) {
return name.endswith (suffix);
}
}
Java-io Inflow Door