Summary of IO (input and output) operations in Java (ii) _java

Source: Internet
Author: User
Tags parent directory readable
operation of the file
In this section, we'll talk about the operation of the file itself.
Don't waste saliva, speak in code ...
Example 1: Creating a File Object
Copy Code code as follows:

Import Java.io.File;
public class Demo {
public static void Main (string[] args) {
Create a file path and name to manipulate
where File.separator represents system-related delimiters, Linux is:/Windows below: \
Path represents the parent directory in this program and does not contain child files
String Path = file.separator + "Home" + File.separator + "SIU" +
File.separator + "work" + file.separator;
Childpath represents subdirectories in this program, including child files
String Childpath = file.separator + "Home" + File.separator + "SIU" +
File.separator + "Work" + File.separator + "Demo.txt";
Construct a file object in a way that is separated by a parent directory and a child file
Can also be written as New File ("/home/siu/work", "test.txt");
File F1 = new file (path, "test.txt");
Using absolute paths to construct a file object
Can also be written as New File ("/home/siu/work/demo.txt");
File F2 = new file (Childpath);
Create a file object for the parent directory
File D = new file (path);
Building a new file object with existing parent directory objects and child files
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 compiling, displays the absolute path that each file object points to


Example 2: Creating and deleting Files

Copy Code code 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 {
/* Because creating and deleting files involves the underlying operation, it may cause an exception.
Returns true if the creation succeeds
If the file already exists, the creation is unsuccessful, return to Flase, and do not assume that it will overwrite
System.out.println ("Create file:" + f.createnewfile ());
Deletes a file, returns true successfully, otherwise returns flase
System.out.println ("Delete file:" + f.delete ());
This method indicates that the file is deleted when the virtual machine exits
The reason is that when the program is running, it may cause an exception to exit directly.
It's necessary to clean up the remnants ~!
F.deleteonexit ();
catch (IOException e) {
E.printstacktrace ();
}
}
}

You see, the creation succeeds, so it returns true because it has been created, so the deletion can be successful as well.


Example 3: Judgment and testing of files

Copy code code 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);
To determine whether a file is executable
System.out.println ("F executable:" + f.canexecute ());
To determine whether a file exists
System.out.println ("F exists:" + f.exists ());
To determine whether a file is readable
System.out.println ("F Readable:" + f.canread ());
To determine whether a file is writable
System.out.println ("F can be written:" + f.canwrite ());
To determine whether a file is an absolute path name
System.out.println ("F is absolute path:" + F.isabsolute ());
Determine if a file is a standard file
System.out.println ("F is standard document:" + f.isfile ());
Determine if a file is a directory
System.out.println ("F is directory:" + f.isdirectory ());
To determine if a file is hidden
System.out.println ("F is hidden:" + F.ishidden ());
}
}

Here you can use a different file to do the test, set the file properties or something is simple
Note that if you use Isflie () and isdirectory () to test, you first determine whether the file object has been created


Example 4: Creating a directory

Copy Code code 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 here 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 ());
}
}

Watch the path.


Example 5: Getting file information

Copy Code code 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 a file
The return value here is string
SYSTEM.OUT.PRINTLN ("Absolute path name of F:" + F.getabsolutepath ());
Returns the absolute path of a file
The return value here is file
System.out.println ("F's Absolute Path object:" + f.getabsolutefile ());
Returns the name of a file or directory
System.out.println ("F's name:" + f.getname ());
Returns the relative path of a file
What path is encapsulated in the constructor, and what path is returned
SYSTEM.OUT.PRINTLN ("Path of F:" + F.getpath ());
Returns the path to the parent directory
If the path in the constructor is not an absolute path, then NULL is returned here
System.out.println ("F's parent directory:" + f.getparent ());
}
}

These are more commonly used and functional similar methods, as for the less commonly used information access to the reference API can be


Example 6: Listing the root directory of the file system

Copy Code code 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 files
file[] files = file.listroots ();
foreach loops print a File object
for (File x:files) {
SYSTEM.OUT.PRINTLN (x);
}
}
}

Because the local environment is Linux, so the root directory is only one/, if it is Windows can list all your letter


Example 7: Listing all files under the directory

Copy Code code 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);
Mode one: List ()
Returns an array of strings containing all the file names in the specified directory
Returns null if it is not a directory
string[] files = f.list ();
for (String x:files) {
SYSTEM.OUT.PRINTLN (x);
}
Mode two: Listfiles ()
Returns a file array
/*
file[] files = f.listfiles ();
for (File x:files) {
If you need to include a path, print x directly
System.out.println (X.getname ());
}
*/
}
}

Pic
Both are returned to the directory of all the file names, but the second way is more practical, for recursively listing files to pave


Instance 8: Recursively list all files under the directory

Copy Code code 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 a list of directories in a recursive way
public static void print (File f) {
if (F.isdirectory ()) {
file[] files = f.listfiles ();
for (File x:files) {
print (x);
}
} else {
System.out.println (f);
}
}
}

Well, there's too much to print, so that's what it means.


About the basic operation of the file on this, thinking of continuing to add ...
The next section will explain the other operations of Java IO

Related Article

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.