Io (input and output) operations in java (2)

Source: Internet
Author: User

File Operations
In this section, we will discuss the operations on the file itself.
Don't waste your time talking with code ......
Instance 1: create a file object Copy codeThe Code is as follows: import java. io. File;
Public class Demo {
Public static void main (String [] args ){
// Create the path and name of the file to be operated
// File. separator indicates the system-related separator. in Linux, it is:/in Windows :\\
// Path indicates the parent directory in this program and does not contain sub-files.
String path = File. separator + "home" + File. separator + "siu" +
File. separator + "work" + File. separator;
// ChildPath represents sub-directories in this program, including sub-Files
String childPath = File. separator + "home" + File. separator + "siu" +
File. separator + "work" + File. separator + "demo.txt ";
// Create a File object by separating the parent directory and sub-Files
// You can also write it as new File ("/home/siu/work", "test.txt ");
File f1 = new File (path, "test.txt ");
// Use an absolute path to construct a File object
// You can also write it as new File ("/home/siu/work/demo.txt ");
File f2 = new File (childPath );
// Create a file object in the parent directory
File d = new File (path );
// Create a new File object using an existing parent directory object and a sub-File
File f3 = new File (d, "hello.txt ");
System. out. println ("f1 path" + f1 );
System. out. println ("f2 path" + f2 );
System. out. println ("f3 path" + f3 );
}
}

After compilation, the absolute path pointed to by each File object is displayed.


Instance 2: Create and delete files

Copy codeThe Code is as follows: import java. io. File;
Import java. io. IOException;
Public class Demo {
Public static void main (String [] args ){
String Path = File. separator + "home" + File. separator + "siu" +
File. separator + "work" + File. separator + "demo.txt ";
File f = new File (Path );
Try {
/* The underlying operations involved in file creation and deletion may cause exceptions */
// If the creation is successful, true is returned.
// If the file already exists, the creation is unsuccessful and flase is returned. Do not overwrite it
System. out. println ("Create File:" + f. createNewFile ());
// If the object is deleted successfully, true is returned; otherwise, flase is returned.
System. out. println ("delete file:" + f. delete ());
// This method indicates that the file is deleted when the VM exits.
// Cause: An exception may occur during the running of the program, causing direct exit.
// It is necessary to clear the residue ~!
F. deleteOnExit ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

As you can see, the creation is successful, so true is returned. Because the creation is successful, the deletion can also be successful.


Instance 3: file judgment and Testing

Copy codeThe Code is as follows: import java. io. File;
Public class Demo {
Public static void main (String [] args ){
String Path = File. separator + "home" + File. separator + "siu" +
File. separator + "work" + File. separator + "Demo.txt ";
File f = new File (Path );
// Determine whether the file is executable
System. out. println ("f executable:" + f. canExecute ());
// Determine whether a file exists
System. out. println ("f exists:" + f. exists ());
// Determine whether the object is readable
System. out. println ("f readable:" + f. canRead ());
// Determine whether a file can be written
System. out. println ("f writable:" + f. canWrite ());
// Determine whether the file is an absolute path name
System. out. println ("f absolute path:" + f. isAbsolute ());
// Determine whether the file is a standard file
System. out. println ("f is a standard file:" + f. isFile ());
// Determine whether the file is a directory
System. out. println ("f is a directory:" + f. isDirectory ());
// Determine whether the object is hidden
System. out. println ("f hidden:" + f. isHidden ());
}
}

Different files can be used for testing. Setting file attributes is also very simple.
Note that if you use isFlie () and isDirectory () for testing, you must first determine whether the file object has been created.


Instance 4: create a directory

Copy codeThe Code is as follows: import java. io. File;
Public class Demo {
Public static void main (String [] args ){
String path = File. separator + "home" + File. separator + "siu" +
File. separator + "work" + File. separator;
// Path exists as the parent directory.
File f1 = new File (path, "/abc ");
File f2 = new File (path, "/d/e/f/g ");
// Create a directory
System. out. println (f1.mkdir ());
// Recursively create a directory
System. out. println (f2.mkdirs ());
}
}

Check the path


Instance 5: Get File Information

Copy codeThe Code is as follows: import java. io. File;
Public class Demo {
Public static void main (String [] args ){
String path = File. separator + "home" + File. separator + "siu" +
File. separator + "work" + File. separator + "demo.txt ";
File f = new File (path );
// Returns the absolute path of the object.
// The returned value is String.
System. out. println ("f's absolute path name:" + f. getAbsolutePath ());
// Returns the absolute path of the object.
// The returned value is File.
System. out. println ("f absolute path object:" + f. getAbsoluteFile ());
// Return the name of the file or directory
System. out. println ("f name:" + f. getName ());
// The relative path of the returned object
// The path encapsulated in the constructor returns the path.
System. out. println ("f path:" + f. getPath ());
// Return the path of the parent directory
// If the path in the constructor is not an absolute path, null is returned here.
System. out. println ("f parent directory:" + f. getParent ());
}
}

These are common and function-like methods. For information that is not commonly used, refer to the API.


Instance 6: list the root directories of the file system

Copy codeThe Code is as follows: import java. io. File;
Public class Demo {
Public static void main (String [] args ){
// ListRoots () is a static method that returns an array of objects.
File [] files = File. listRoots ();
// Foreach print the File object cyclically
For (File x: files ){
System. out. println (x );
}
}
}

Because the local environment is Linux, there is only one/in the root directory. If it is Windows, all your drive letters will be listed.


Instance 7: list all files in the directory

Copy codeThe Code is as follows: import java. io. File;
Public class Demo {
Public static void main (String [] args ){
String path = File. separator + "opt" + File. separator;
File f = new File (path );
// Method 1: list ()
// Returns an array of strings containing all file names in the specified directory.
// If it is not a directory, null is returned.
String [] files = f. list ();
For (String x: files ){
System. out. println (x );
}
// Method 2: listFiles ()
// Returns the File array.
/*
File [] files = f. listFiles ();
For (File x: files ){
// Print x directly if the path needs to be included
System. out. println (x. getName ());
}
*/
}
}

Pic
Both return all file names in the directory, but the second method is more practical, paving the way for Recursive listing of Files


Example 8: recursively list all files in a directory

Copy codeThe Code is as follows: import java. io. File;
Public class Demo {
Public static void main (String [] args ){
String path = File. separator + "opt" + File. separator;
File f = new File (path );
// Call the following recursive method
Print (f );
}
// Print the directory list recursively
Public static void print (File f ){
If (f. isDirectory ()){
File [] files = f. listFiles ();
For (File x: files ){
Print (x );
}
} Else {
System. out. println (f );
}
}
}

Okay, there are too many printed content. That's just what it means.


This is the basic operation on the file, and I want to continue adding ......
The subsequent sections will explain other java io operations.

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.