Java File Bulk Rename batch fetch specific type file _java

Source: Internet
Author: User
Tags event listener file copy gettext set background

Reason:

Because in the online download video tutorials, some names are particularly long, is generally the organization or website propaganda, not convenient to see the video of the introduction, so did the following first function.

Because the teacher sends the courseware, the folder is too many, wants to put the docx all to put in the same folder below, one one finds to be too troublesome, therefore has done the second function.

Recently learned the Java file and flow of knowledge, so just practicing practicing, this is their first EXE program, share AH.

(Export jar file, and use tool exe4j to generate EXE file, this part omitted ha)

The knowledge used:

Use Java files, streaming knowledge, and simple GUI knowledge.

Function:

Function One: Remove the name of some of the keywords, can also be set to replace the word.

Function Two: Extract a path under all the specific types of files, and then put it under a new folder, if there are duplicate files, then automatically sorted by adding numbers to differentiate.

First look at the start of the interface and the resulting EXE file:

First Feature Demo:

Not before the operation:

After the operation: the previous part of the same keyword all removed

Also has the recall function:

A second feature demo:

No action before:

After Operation:

Of course, there is also a recall function

Source Code Analysis:

Start class:

Package Guuze;
public class Test {public
static void Main (string[] args) {
//start GUI, that is, user interface
new Showgui ();
}
}

Show GUI classes:

Package Guuze;
Import java.awt.Dimension;
Import Java.awt.FlowLayout;
Import Java.awt.Font;
Import Java.awt.Image;
Import Java.awt.Toolkit;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Java.io.File;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JTextField; public class Showgui {private JFrame F; private JButton B1; private JButton B2; private JButton B3; private JTextField TF
1;
Private JTextField TF2;
Private JTextField tf3;
Private JButton b4;
Private JButton B5;
Private JButton b6;
Private JTextField Tf4;
Private JTextField Tf5;
Private JTextField Tf6;
private static String S1; Public Showgui () {//Direct call to Startgui ();} public void Startgui () {f = new bgset ();//To set background picture f.setlayout (New FlowLayout ()
);
Image icon = Toolkit.getdefaulttoolkit (). GetImage ("image/4.jpg");/Set logo icon F.seticonimage (icon) in the upper left corner;
6 button B1 = new JButton ("Start"); b2 = new JButton ("recall");
B3 = new JButton ("Exit");
B4 = new JButton ("One-click extraction");
B5 = new JButton ("withdrawn");
B6 = new JButton ("Exit");
The size of the 6 buttons b1.setpreferredsize (new Dimension (89, 39));
B2.setpreferredsize (New Dimension (89, 39));
B3.setpreferredsize (New Dimension (89, 39));
B4.setpreferredsize (New Dimension (89, 39));
B5.setpreferredsize (New Dimension (89, 39));
B6.setpreferredsize (New Dimension (89, 39));
The size of the 6 text boxes and the properties of the input font TF1 = new JTextField ("Please input Absolute_path", 40);
Tf1.setfont (New Font ("Song Body", Font.plain, 25));
Tf1.setbounds (200, 15, 550, 126);
TF2 = new JTextField ("Please input keyWords", 40);
Tf2.setfont (New Font ("Song Body", Font.plain, 25));
Tf2.setbounds (200, 15, 550, 126);
TF3 = new JTextField ("Please input replacewords", 40);
Tf3.setfont (New Font ("Song Body", Font.plain, 25));
Tf3.setbounds (200, 15, 550, 126);
TF4 = new JTextField ("Please input Absolute_path", 40);
Tf4.setfont (New Font ("Song Body", Font.plain, 25));
Tf4.setbounds (200, 15, 550, 126);
Tf5 = new JTextField ("Please input Target_path", 40); Tf5.setfont (nEW Font ("Song Body", Font.plain, 25));
Tf5.setbounds (200, 15, 550, 126);
TF6 = new JTextField ("Please input filetype", 40);
Tf6.setfont (New Font ("Song Body", Font.plain, 25));
Tf6.setbounds (200, 15, 550, 126);
Add the button and text box to the F.add (TF1);
F.add (TF2);
F.add (TF3);
F.add (B1);
F.add (B2);
F.add (B3);
F.add (TF4);
F.add (TF5);
F.add (TF6);
F.add (B4);
F.add (B5);
F.add (B6);
Invoke Event Listener function MyEvent ();
F.setvisible (TRUE); private void MyEvent () {//click on the upper right corner x exit F.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e)
{system.exit (0);}
}); Click the response event B1.addmouselistener for the first button (new Mouseadapter () {public void mouseclicked (MouseEvent e) {S1 = Tf1.gettext ();
The value in the text box is judged, if nothing is written, as null processing, the following similar if (s1.equals ("Please input path")) {S1 = "";}
File File = new file (S1);
String test[];
Test = File.list ();
Renamefunction.test1 = test;
String s2 = Tf2.gettext ();
if (s2.equals ("Please input replacewords")) {s2 = "";}
String s3 = Tf3.gettext ();
if (s3.equals ("Please input replacewords")) {s3 = "";} Try{//Start the Rename function renamefunction.sure (S1, S2, S3);}
catch (Exception E1) {}}}); Click the response event B2.addmouselistener for the second button (new Mouseadapter () {public void mouseclicked (MouseEvent e) {The try {if (!s1.equa LS ("Please input path")) {//Initiate recall Revokerename.revoke (S1);}
catch (Exception E2) (Exception E1) {}}});
Click on the third button's response event B3.addmouselistener (new Mouseadapter () {public void mouseclicked (MouseEvent e) {system.exit (0);//exit}
}); Click on the Fourth button's response event B4.addmouselistener (new Mouseadapter () {public void mouseclicked (MouseEvent e) {String s2 = tf4.gettext (
);
String s3 = Tf5.gettext ();
String S4 = Tf6.gettext ();
if (s2.equals ("Please input Absolute_path")) {s2 = "";}
if (s3.equals ("Please input Target_path")) {s3 = "";}
if (s4.equals ("Please input filetype")) {S4 = "";}
Startup file search function searchfilefunction.startcopy (S2, S3, S4);
}
}); Click on the Fifth button's response event B5.addmouselistener (new Mouseadapter () {public void mouseclicked (MouseEvent e) {String s2 = tf5.gettext (
); Start Recall function RemovetargEtfile.startdelete (S2);
}
});
Click on the Sixth button's response event B6.addmouselistener (new Mouseadapter () {public void mouseclicked (MouseEvent e) {system.exit (0);//exit}
}); }
}

GUI background picture settings class:

Package Guuze;
Import Javax.swing.ImageIcon;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JPanel;
public class Bgset extends JFrame {
private static final long serialversionuid = 1L;
Public Bgset () {
//Set title
super ("Greatfish");
SetBounds (M, M, N);
The path of the background picture.
String Path = "Image/3.jpg";
ImageIcon background = new ImageIcon (path);
JLabel label = new JLabel (background);
Label.setbounds (0, 0, this.getwidth (), This.getheight ());
JPanel Imagepanel = (JPanel) this.getcontentpane ();
Imagepanel.setopaque (false);
This.getlayeredpane (). Add (Label, new Integer (Integer.min_value));
}

File Rename class:

Package Guuze;
Import Java.io.File;
Import Java.util.Scanner;
public class Renamefunction {
static Scanner input = new Scanner (system.in);
public static String test1[];
public static void sure (string s1, String s2, string s3) throws Exception {
file File = new file (s1);
String test[];
Test = File.list ();
Iterate over the name of the file for
(int i = 0; i < test.length i++) {
//judge if there is a keyword you would like to remove
(test[i].indexof (S2)!=-1) {
   //save renamed filename
test[i] = test[i].replace (s2, S3);
}
file[] files = file.listfiles ();
for (int i = 0; i < test.length;) {
for (File f:files) {
if (F.isfile ()) {
///Loop after renaming name
F.renameto (new File (S1 + "/" + test[i++)); 
   }}}}

File Rename recall function class:

Package Guuze;
Import Java.io.File;
public class Revokerename {public
static void Revoke (String s1) throws Exception {
//re-assign back the original name
file File = N EW File (S1);
file[] files = file.listfiles ();
for (int i = 0; i < RenameFunction.test1.length;) {for
(File f:files) {
if (F.isfile ()) {
//note is test1
f.renameto (new file (S1 + "/" + renamefunction.tes t1[i++]));}}}

File Copy class:

Package Guuze;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException; public class Searchfilefunction {static int count = 1; public static void Startcopy (String source_path, String Target_pat H, string file_type) {//Start loop function Xunhuan (Source_path, Target_path, File_type);} public static void Xunhuan (String source_
Path, String Target_path, String file_type) {File File = new file (Source_path);
String names[] = File.list (); Determine if the file and whether it ends with the file type you want if (File.isfile () && File.getabsolutepath (). EndsWith (File_type)) {String New_path =
Target_path + "/" + file.getname ();
File File1 = new file (New_path); if (!file1.exists ()) {try {file1.createnewfile ();} catch (IOException e) {}} else {//If the file name is the same, precede the point with a number to differentiate//note the use of \.
To separate, instead of.
string[] arr = new_path.split ("\;");
String new_path1 = arr[0] + count + "." + arr[1]; File1.renameto (New File (NEW_PATH1));
//is a file, so start copying Files Filecopybybufferstreamarray (File.getabsolutepath (), new_path); else if (File.isfile () &&!file.getabsolutepath (). EndsWith (File_type)) {//Note that this method body does not write anything, it does not do processing} else {for (i NT i = 0; i < names.length;
i++) {//is not a file, iterative Xunhuan (File.getabsolutepath () + "/" + names[i], Target_path, file_type);} public static void Filecopybybufferstreamarray (String srcfile, String targetfile) {//write with knowledge of the stream file = = new File (s
Rcfile);
File File1 = new file (targetfile);
FileInputStream FIS = null;
FileOutputStream fos = null;
Bufferedinputstream bis = null;
Bufferedoutputstream BOS = NULL; try {fis = new FileInputStream (file), Fos = new FileOutputStream (file1), bis = new Bufferedinputstream (FIS); bos = new Bu
Fferedoutputstream (FOS);
int len = 0;
Byte[] B = new BYTE[10];
while (len = Bis.read (b))!=-1) {bos.write (b, 0, Len);} bos.flush (); catch (IOException e) {} finally {try {fis.close (); Fos.close (); Bis.close (); Bos.close ();} catch (IOException e) {
}
}
}
} 

File replication Recall class:

Package Guuze;
Import Java.io.File;
public class Removetargetfile {public
static void Startdelete (String path) {
file File = new file (path);
DeleteFile (file);
private static void DeleteFile (file file) {
//remember not to delete the folder for the path
if (file.exists ()) {
if (File.isfile ()) { c11/>//is a file that deletes
File.delete directly (), or
else if (file.isdirectory ()) {
file[] files = file.listfiles ();
for (int i = 0; i < files.length i++) {
//If not a file, iterate
DeleteFile (Files[i]);}
}}
}

The above is a small series to introduce the Java File Batch rename batch to extract a specific type of file, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.