Io system Introduction Starting from File class

Source: Internet
Author: User
No matter which language you want to learn, you will inevitably have to access the file system and always deal with files. Of course, Java is no exception. Some netizens think That I/O design is very cumbersome. Sometimes I feel like this. Actually, I am very familiar with it, but I think it is quite convenient. Before introducing Java Io, we should first introduce a very important class file.

After seeing the name of this class, you must think that it represents a file. In fact, this is not accurate, because the file class in Java can represent a file or a directory. This is described in the API doc, and the file is represented by abstract pathname. But do you think directories are not special files?

File is very simple to use. It has four constructors:
File (string parent, string child)
File (File parent, string child)
File (URI)
File (string pathname)
The first two allow us to create a file or directory under a specific directory, and the other two allow us to create a file or directory through pathname or Uri. Note that although file is a system-independent representation, pathname is related to the system. For example, "/" in UNIX indicates the root directory, in Windows, the drive letter is usually used. For example, if the absolute path is C:/helloworld/mingjava, it does not start with "/". Generally, the relative path is relative to the current directory. When we create a file, we can use the exists () method to determine whether it exists. If it does not exist, we can choose whether to create a file or a directory. For example
File file = new file ("hehe ");
If (! File. exists ())
{
File. mkdir ();
}
For (INT I = 0; I <5; I ++)
{
File listfile = new file (nextfile, "Ming" + I + ". txt ");
If (! Listfile. exists ())
{
Listfile. createnewfile ();
}

}
If we already know a file object and want to create a file under its directory, we can use the first constructor. For example
File nextfile = new file (file, "Ming // hehe ");
If (! Nextfile. exists ())
{
Nextfile. mkdirs ();
}
Note that mkdirs () is used to create multi-level directories (). We can also list the objects in a directory, and use the list () method to obtain an array of the string [] type, which is the name of the objects in the directory.
String [] list = nextfile. List ();
For (Int J = 0; j <list. length; j ++)
{
System. Out. println (list [J]);
}
File usage is not complex. If you have any problems, please refer to the API Doc to solve the problem. The following is a small example I wrote to illustrate how to use file.
Import java. Io .*;
Import java.net .*;

Public class testfile
{
Public static void main (string [] ARGs) throws ioexception
{
File file = new file ("hehe ");
If (! File. exists ())
{
File. mkdir ();
}
File nextfile = new file (file, "Ming // hehe ");
If (! Nextfile. exists ())
{
Nextfile. mkdirs ();
}
System. Out. println (nextfile. getparent ());

If (nextfile. isdirectory ())
{
File newfile = new file (nextfile, "ming.txt ");
If (! Newfile. exists ())
{
Newfile. createnewfile ();
System. Out. println (newfile. getname ());
System. Out. println (newfile. getabsolutefile ());

}
For (INT I = 0; I <5; I ++)
{
File listfile = new file (nextfile, "Ming" + I + ". txt ");
If (! Listfile. exists ())
{
Listfile. createnewfile ();
}

}
String [] list = nextfile. List ();
For (Int J = 0; j <list. length; j ++)
{
System. Out. println (list [J]);
}
}


}
}
Output result:
Hehe/Ming
Ming.txt
Ming0.txt
Ming1.txt
Ming2.txt
Ming3.txt
Ming4.txt
Press any key to continue...

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.