Io
Most applications have data exchange with external devices, and the most common external devices include disks and networks. IO refers to the data input and output of these devices by the application, and the Java language defines many classes that are specifically responsible for the input and output of various methods, which are placed in the java.io package.
File class
The file class is the only object in the IO package that represents the disk file itself , and the file class defines some platform-independent methods to manipulate the files. By invoking the various methods provided by the file class, it is possible to create, delete, rename, and determine whether a file's read-write permission exists, set and query the last modified time of the file, and so on.
The file class does not have a parameterless constructor, and most commonly uses the following construction method to generate a file object (note that delimiters can use "/" and "\", but "\" must be written "\ \" because it involves escaping):
File (String pathName)
Pathname refers to the path name of the file
Methods in the File class
File defines a lot of ways to get the standard properties of a file object, and write a piece of code to look at:
1PublicStaticvoidMain (string[] args)2{3 String fileName = "D:" + file.separator + "Files";4 File File =NewFile (FileName);56if (file.exists () && file.isdirectory ())//Determines whether the file/folder that the path points to exists, whether the directory7{8 System.out.println ("file is a folder \ n");9Ten file[] files = file.listfiles ();//Get all files/folders under directory (only under this layer path)System.out.print ("File under Path:");12For(File f:files)13{System.out.print (f + "\ T"));15}16System.out.println ();17System.out.println ("files[0" FileName: "+ files[0].getname ());//Get file name, folder nameSystem.out.println ("files[0" file path: "+ Files[0].getpath ());//Get file, Folder pathThe absolute path of System.out.println ("Files[0"): "+ Files[0].getabsolutepath ());//Get file, folder absolute pathSystem.out.println ("files[0" parent folder name: "+ files[0].getparent ());//Get File Parent directory pathSystem.out.println (Files[0].exists ()? The existence of "files[0": "Files[0" does not exist ");//Determine if a file or folder existsSystem.out.println (Files[0].canwrite ()? "Files[0": "Files[0" is not writable ");//Determine if the file is writableSystem.out.println (Files[0].canread ()? "Files[0": "Files[0" is unreadable ");//Determine if a file is readableSystem.out.println (Files[0].canexecute ()? "File[0] executable": "file[0] not enforceable");//Determine if a file is executableSystem.out.println (files[0].isdirectory)? "Files[0" is the Directory ":" Files[0] is not the directory ");//Determine if a file or folder is a directorySystem.out.println (Files[0].isfile ()? "Files[0" is the document ":" Files[0] is not a document ");//Determine whether to take files, folders is not a standard fileSystem.out.println (Files[0].isabsolute ()? The pathname of "files[0" is the absolute path ":" The path name of the files[0] is not an absolute path ");//Determine if the path name is not an absolute pathSystem.out.println ("Files[0] Last modified:" + files[0].lastmodified ());//Get file, folder last modified timeThe size of System.out.println ("Files[0"): "+ files[0].length () +" Bytes ");// Gets the number of bytes of the file, and if it is a folder, this value is 031 System.out.println ("Files [0] The path is converted to a URI: "+ Files[0].touri ()); // path name after getting the file path uri 32 Span style= "color: #008080;" >33 if (Files[0].exists ()) 34 files[0].delete (); // Delete the specified file, folder 35 if (Files[1].exists ()) 36 files[1].deleteonexit (); Span style= "color: #008000;" >//37 }38}
The result of the operation is:
file is a folder path with files: D:\Files\buffered.txt D:\Files\example.txt D:\Files\Inner d:\ Files\serializable.txt D:\Files\stream.txt D:\Files\test.txt files[0 "FileName: buffered. Txtfiles[0 "the parent folder name: D:\filesfiles[0 "existence Files[0" writable Files[0 "is the file files[0 "path name is the last modified time for absolute path files[0]: 1440313521120files[0] Size: 21 Bytesfiles[0] The path is converted to Uri:file:/d:/files/buffered.txt
The sample code should be a more comprehensive demonstration of the use of file, with two points worth noting:
1, the third line of the program code, write "D:/files" and "D:\\files" is OK, but like the example code is a better way to write, because it makes the code cross-platform, robust and better. The "File.separator" will take the separators under different operating systems depending on the operating system. In fact, file also has another delimiter pathseparator, which means ":", but because Java is mainly used in Windows and Linux, there is no drive letter problem, so generally directly hit ":" Just fine
2, the code of the 34th, the 36th line, note, Delete if it is a folder, folder also has files/folders, is unable to delete the successful
Finally, again, file is the basis of the IO stream.
Java Io1:io and file