"Javase" Day07_file

Source: Internet
Author: User
Tags create directory

"Javase" Day07_file


1.File

1) java.io.File

* This class is used to describe a file or directory in a file system.

* File can access the property information of the files or directories. File can manipulate files or directories. File can see which subkeys are in a directory.

* File cannot access the contents of files!

2) String GetName ()

---get file names

3) long Length ()

---Get the file size (the amount of bytes that the file occupies)

4) Boolean exists ()

---to determine whether the file or directory currently represented by files already exists on the hard disk.

5) Boolean lastmodified ()

---Get the last Modification time 1970 New Year's Day to this moment of millisecond difference. The last modified time for files that do not yet exist is 0.

See the code for details:


Package Day01;import java.io.file;import Java.text.simpledateformat;import java.util.date;/** * java.io.File * This class is used to describe a file or directory in a file system. * File can access the property information of the files or directories. * file can manipulate files or directories. * file to see which subkeys are in a directory. * * File cannot access the contents of files! */public class FileDemo1 {public static void main (string[] args) {/* * Description directory should be aware of two points: * 1: Try not to use absolute path, because different operating systems differ. * 2: Hierarchy of directories do not write "/" or "\" directly, because * different systems also have differences, so use the constant of File: * File.separator is OK. *///"/home/soft01/myworkspace/mycode003_se02/demo.txt" File File = new file (".") +file.separator+ "Demo.txt"); Indicates the current directory, the directory in which the item is located */* Gets the file name */string name = File.getname (); System.out.println ("File name:" +name);/* * Gets the file size (the amount of bytes consumed by the file) */long length = File.length (); SYSTEM.OUT.PRINTLN ("File size:" +length),/* * To determine whether the file or directory currently represented by files already exists on the hard disk. */boolean exits = File.exists (); System.out.println ("already exists:" +exits); */* Gets the last modification time 1970 New Year's Day to this moment of millisecond difference. * The last modified time for files that do not exist is 0. */long dateTime = file.lastmodified (); SYSTEM.OUT.PRINTLN (datetime);D ate date = new Date (datetime); SYSTEM.OUT.PRINTLN (date); SimpleDateFormat SDF = new SimpledateformaT ("yyyy years M D Day HH:mm:ss"); String datestr = Sdf.format (date); System.out.println (DATESTR);//Whether it is a hidden file Boolean Ishidden = File.ishidden ();//Is readable Boolean canRead = File.canread ();// Whether to write Boolean canWrite = File.canwrite (),//whether to run Boolean canexecute = File.canexecute (), or whether to file a Boolean isfile = File.isfile ();//Is the directory Boolean isdir = File.isdirectory (); System.out.println (ishidden+ "," +canread+ "," +canwrite+ "," +canexecute+ "," +isfile+ "," +isdir);}}

6) Boolean createnewfile ()

---Create a new file

Code:

Package Day01;import Java.io.file;import java.io.ioexception;/** * Create a new file using file */public class FileDemo2 {public static void Main (string[] args) throws IOException {/* * Creates a file under the current directory root: Test.txt * If you write the file name directly, the default is in the current directory, so "./" can be ignored. */file file = new file ("Test.txt"), if (!file.exists ()) {//creates the current file (no longer created if the file already exists) File.createnewfile (); System.out.println ("File creation succeeded");} File.createnewfile ();//system.out.println ("111");}}

7) Boolean Delete ()

---Delete an existing file

Code:


Package Day01;import java.io.file;/** * Deleting an existing file */public class FileDemo3 {public static void main (string[] args) {/* * Delete In addition to the Test.txt file in the current directory, */file files = new file ("Test.txt"), if (File.exists ()) {Boolean b1 = File.delete (); System.out.println (b1+ "Delete Complete");}}}

8) Boolean mkdir ()

---Create a directory

Code:

Package Day01;import Java.io.file;import java.io.ioexception;/** * Create a directory */public class FileDemo4 {public static void Mai N (string[] args) throws IOException {/* * Create directory in current directory demo */file dir = new File ("demo"); if (!dir.exists ()) {Dir.mkdir (); SYSTEM.OUT.PRINTLN ("Create directory successfully! ");} File File1 = new file ("."); System.out.println (File1.length ());//delete directory//dir.delete ();}}

9)boolean mkdirs ()

---Create a multilevel directory

Package Day01;import java.io.file;/** * Create a multilevel directory * */public class FileDemo5 {public static void main (string[] args) {/* * Create a directory under the current project root directory: * a/b/c */file dirs = new File ("a/b/c/d/e/f"), if (!dirs.exists ()) {dirs.mkdirs (); SYSTEM.OUT.PRINTLN ("Creation complete! ");} Dirs.delete ();}}

10) Recursive deletion of multilevel directories

Code:


Package Day01;import java.io.file;/** * Delete a multilevel directory * When you delete a directory that contains subkeys, the Delete method is unsuccessful, and the directory is deleted after all subkeys in the directory have been deleted. * */public class FileDemo6 {public static void main (string[] args) {/* * Delete the A directory in the current directory */file dir = new File ("a");d Eletefile (dir);} public static void DeleteFile1 (file file) {if (!file.exists ()) {return;} if (File.isfile ()) {file.delete (); return;} File[] Subs = File.listfiles (); for (file f:subs) {DeleteFile (f);} File.delete ();//return;} public static void DeleteFile (file file) {if (File.isdirectory ()) {file[] sub = File.listfiles (), for (file f:sub) { DeleteFile (f);}} File.delete ();}}

One) file[] Listfiles ()

---This method gets all the subkeys under the current directory, it can be represented by a file object because it contains nothing but files or directories. So getting a directory with all the subkeys equals fetching several file objects, each representing one of the subkeys.

Code:


Package Day01;import java.io.file;/** * Gets all subkeys under a directory * */public class FileDemo7 {public static void main (string[] args) {/ * * Gets all subkeys under the current project root directory */file file = new file ("."); * * file[] listfiles () * This method gets all the subkeys in the current directory, which can be represented by a file * object because it contains nothing but files or directories under a directory. So getting a directory with all the subkeys equals getting * A number of file objects, each representing one of the subkeys. */file[] files = file.listfiles (); for (File f:files) {if (F.isdirectory ()) {///To determine whether the current file represents a document System.out.print ("directory:" );} else if (F.isfile ()) {//Determines whether the current file represents a directory System.out.print ("file:");} System.out.println (F.getname ());}}}

FileFilter file Filter

Code:

Package Day01;import java.io.file;import java.io.filefilter;/** * filefilter file filter * files  have an overloaded Listfiles method, Allows us to pass in a * file filter and return the subkeys that meet the requirements of the filter. Not satisfied is ignored. * */public class FileDemo8 {public static void main (string[] args) {/* * Gets the current directory under the name "." The beginning of the file or directory. */filefilter filter = new FileFilter () {/** * Filter Filters: * Returns True if the given file object satisfies the requirements. */public Boolean Accept (file file) {return File.getname (). StartsWith (".");}; File File = new file ("."); * * Gets all the subkeys that meet the given filter requirements. */file[] Sub = file.listfiles (filter); for (File f:sub) {System.out.println (F.getname ());}}}




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

"Javase" Day07_file

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.