Java_io Flow--file Class (2)

Source: Internet
Author: User

The file class uses Exercise 1

1. Output the file name specifying the suffix below the specified directory
Determine if there is a suffix named. txt file under the E-disk directory, and if so, output this file name.

2. Analysis:
(1). Package e-Tray Directory
(2). Gets the file array of all files or folders under this directory to determine if the file
(3). Iterate over the file array, get each file object, and then determine
(4). Whether it is a file
Yes: continue to determine whether to end with a. txt file
Yes: The file name is output
No: It doesn't matter
No: It doesn't matter

3. Improvement:
Using file name filters to improve
Method:
Public string[] List (flienamefilter filter)
Public file[] ListFile (flienamefilter filter)
Implementation of the need to implement an interface and rewrite the inside of the Accept () method

Analysis:
(1). Determine if there is a suffix named. txt file under the E-disk directory, and if so, direct output
(2). First obtain all, and then traverse the time, then judge, if the condition is satisfied with the output
(3). When the acquisition is already satisfied with the condition, then the output can be.


4. Code implementation:
public class Test {
public static void Main (string[] args) {
Package E to determine the directory
File file =new file ("e:\\");
Get all the files below the directory or the file array for the folder
String[] Filearray=file.list (new FilenameFilter () {
@Override
Public Boolean Accept (File dir, String name) {
Directly returns the name of the file that satisfies the condition
return new File (Dir,name). Isfile () &&name.endswith (". txt");
}
});
Traversing output results
for (String S:filearray) {
System.out.println (s);
}
}
}

-----------------------------------------------------

The file class uses Exercise 2

1. Batch modification of file names
Put the demo folder below the data test _01.txt, data test _02.txt, data test _03.txt, data test _04.txt modified to become
Data test results _01.txt, data test results _02.txt, data test results _03.txt, data test results _04.txt


2. Ideas
(1). package Catalog
(2). Gets the file array of all the files under the directory
(3). Traverse the file array to get each file object
(4). Stitch a new name, then rename it

3. Code implementation
public class Test {
public static void Main (string[] args) {
Packaging
File File=new file ("e:\\demo\\");
File[] Filearray =file.listfiles ();
Traverse
for (File Fl:filearray) {
String Name=fl.getname ();
System.out.println ("Origin name is:" +name);
Get an index for character interception
int Index=name.indexof ("_");
String numberstring=name.substring (0, index);
String endstring=name.substring (Index,name.length ());
System.out.println (numberstring);
String newname=numberstring.concat ("result") +endstring;
System.out.println ("NewName:" +newname);
File Newfile=new file (file,newname);
Renaming
Fl.renameto (NewFile);
}
}
}

Java_io Flow--file Class (2)

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.