JAVA I/O operations (i) __java

Source: Internet
Author: User
Tags file separator parent directory
I've always wondered why there was no input from the keyboard in what I had learned before, and what I learned today gave me a good answer, all of which were included under the package. The things mentioned in this article include the file class (how to create and delete files, write to files, and read from files).
The file class has two constructor methods, file (parent directory, file name), association specified directory under the specified name of the files, filename (file name/directory name), associated with a file name or directory, where the/expression means "or".
A better approach is to first associate a directory name with a file object, then create the directory, (MkDir ()), and then construct a file using the constructor method. The following code creates a file named "1.txt" in "My Documents."
File Dir=new file ("C:" +file.separator+ "Documents and Settings" +file.separator+ "Yxy" +file.separator+ "my Documents"); Note the escape character here
Dir.mkdir (); Create a table of contents
File File1=new file (dir, "1.txt");
File1.createnewfile (); Create a new file
String[] Str1=dir.list (); The list () mentioned below
I don't know why the space here can only be so long, the code is I copied from my own code, Khan
One first
You can actually use the escape character "//" to replace File.separator, but this is not a good habit. In order to achieve a Java compile, around the sexy features, we need a common file separator, because the various operating systems are different, such as the Linux file separator is a forward slash "/". The attribute of file separator exactly the file separator under the current operating system. Also, never let "/" exist alone, which is the identity of an escape character in the code that will interpret the next character as an escape character.
In addition to this method you can create a new file, you can also call a static function under the file class
File Createtempfile (String prefix,string suffix,file directory), which is a complete version that creates a temporary file with a prefix prefix name, suffix as the suffix name, in the specified directory. Removed by Deleteonexit (). But there's also a streamlined version,
File Createtempfile (String prefix,string suffix), which does not specify a directory, creates a temporary file with a prefix prefix name, suffix as the suffix name, in the default temporary folder of the current operating system.
The above is how to create a file, and then it's about how to look up files in the directory and find them through file filters.
See the list () method written in the previous code, return an array of type string, get all the file names under the current directory, including the directory name (that is, the folder). However, this is not enough to find the files we need, we need to find a room according to our request. The package class provides the class file filter FilenameFilter, which is an interface with a method Boolean accept (File dir, String name), a method that needs to be overridden, after overriding this method, the list ( Filenamefileter f) method can list the methods that meet our requirements.
Here's the code I practiced.
Class Filenamefilterinstance implements FilenameFilter//file filter
Public Boolean accept (File dir,string name)
Return Name.indexof (". Java")!=-1; Note The return type, indexof () returns the index or-1, and the//accept request returns a Boolean
Class Test1
public static void Main (string[] args) throws Exception//here notice throws an exception
File Dir=new file ("C:" +file.separator+ "Documents and Settings" +file.separator+ "Yxy" +file.separator+ "my Documents"); Note the escape character here
Dir.mkdir ();
File File1=new file (dir, "1.txt");
File1.createnewfile ();
File file2=file.createtempfile ("Hello", ". Java", dir); Create in dir specified directory with a prefix of at least 3 word length
String[] Str1=dir.list ();
for (int i=1;i<str1.length;i++)
System.out.println (Str1[i]);
System.out.println ("--------------------------");
String[] Str2=dir.list (New filenamefilterinstance ());
for (int i=0;i<str2.length;i++)
System.out.println (Str2[i]);
File1.deleteonexit (); Delete files when exiting
File2.deleteonexit ();
Thread.Sleep (5000);
/* Create 1.txt and under My Documents to return all the file and directory names under My Documents, check to based on file filters, and finally delete the created file on exit. CreateNewFile and static methods are used to create files during the createtempfile*/

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: 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.