Java文件批量重命名批量提取特定类型文件

      2020-07-02 04:43      JAVA编程
这篇文章主要介绍了Java文件批量重命名批量提取特定类型文件的相关资料

原因:

  因为在网上下载视频教程,有的名字特别长,一般都是机构或者网站的宣传,不方便直接看到视频的简介,所以做了下面的第一个功能。

  因为老师发的课件中,文件夹太多,想把docx都放在同一个文件夹下面,一个一个找出来太麻烦,所以做了第二个功能。

最近刚刚学了Java文件和流的知识,所以正好练练手,这也是自己的第一个exe程序,分享一下哈。

  (导出jar文件,以及用工具exe4j生成exe文件,这部分省略了哈)

用到的知识:

  用到Java中文件,流的知识,以及简单的GUI知识。

功能:

   功能一:去除文件名字的某些关键字,也可以设置代替字。

   功能二:提取一个路径下面所有特定类型的文件,然后放在一个新的文件夹下面,如果有重复的文件,则自动排序在后面加数字来区分。

先看下启动后的界面和生成的exe文件:

第一个功能演示:

  没有操作前的:

  操作后:把前面部分相同关键字全部去掉了

  还有撤回功能:

第二个功能演示:

  没有操作前:

  操作后:

  当然,也有撤回功能

源代码分析:

启动类:

package guuze;public class Test {public static void main(String[] args) {//启动GUI,即用户界面new ShowGui();}}

显示GUI类:

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 tf1;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() {// 直接调用startGui();}public void startGui() {f = new BgSet();// 用来设置背景图片f.setLayout(new FlowLayout());Image icon = Toolkit.getDefaultToolkit().getImage("image/4.jpg");// 设置左上角logo图标f.setIconImage(icon);// 6个按钮b1 = new JButton("开始");b2 = new JButton("撤回");b3 = new JButton("退出");b4 = new JButton("一键提取");b5 = new JButton("撤回");b6 = new JButton("退出");// 6个按钮的大小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));// 6个 文本框的大小以及输入字体的属性tf1 = new JTextField("Please input absolute_path", 40);tf1.setFont(new Font("宋体", Font.PLAIN, 25));tf1.setBounds(200, 15, 550, 126);tf2 = new JTextField("Please input keyWords", 40);tf2.setFont(new Font("宋体", Font.PLAIN, 25));tf2.setBounds(200, 15, 550, 126);tf3 = new JTextField("Please input replaceWords", 40);tf3.setFont(new Font("宋体", Font.PLAIN, 25));tf3.setBounds(200, 15, 550, 126);tf4 = new JTextField("Please input absolute_path", 40);tf4.setFont(new Font("宋体", Font.PLAIN, 25));tf4.setBounds(200, 15, 550, 126);tf5 = new JTextField("Please input target_path", 40);tf5.setFont(new Font("宋体", Font.PLAIN, 25));tf5.setBounds(200, 15, 550, 126);tf6 = new JTextField("Please input filetype", 40);tf6.setFont(new Font("宋体", Font.PLAIN, 25));tf6.setBounds(200, 15, 550, 126);// 把按钮和文本框添加上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);// 调用事件监听函数myEvent();f.setVisible(true);}private void myEvent() {// 点击右上角×退出f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});// 点击第一个按钮的响应事件b1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {s1 = tf1.getText();// 对文本框内值进行判断,如果什么也没写,当做空处理,以下的类似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 {// 启动重命名函数RenameFunction.sure(s1, s2, s3);} catch (Exception e1) {}}});// 点击第二个按钮的响应事件b2.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {try {try {if (!s1.equals("Please input path")) {// 启动撤回RevokeRename.revoke(s1);}} catch (Exception e2) {}} catch (Exception e1) {}}});// 点击第三个按钮的响应事件b3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {System.exit(0); // 退出}});// 点击第四个按钮的响应事件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 = "";}// 启动文件搜索函数SearchFileFunction.startCopy(s2, s3, s4);}});// 点击第五个按钮的响应事件b5.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {String s2 = tf5.getText();// 启动撤回函数RemoveTargetFile.startDelete(s2);}});// 点击第六个按钮的响应事件b6.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {System.exit(0); // 退出}});}}

GUI背景图片设置类:

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() {// 设置标题super("GreatFish");setBounds(100, 100, 600, 600);// 背景图片的路径。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));}}

文件重命名类:

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();// 遍历文件的名字for (int i = 0; i < test.length; i++) {// 判断是不是有你想去除的关键字if (test[i].indexOf(s2) != -1) {// 保存重命名后的文件名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()) {// 循环赋重命名后的名字f.renameTo(new File(s1 + "/" + test[i++]));}}}}}

文件重命名撤回函数类:

package guuze;import java.io.File;public class RevokeRename {public static void revoke(String s1) throws Exception {// 重新赋回原来的名字File file = new File(s1);File[] files = file.listFiles();for (int i = 0; i < RenameFunction.test1.length;) {for (File f : files) {if (f.isFile()) {// 注意是test1f.renameTo(new File(s1 + "/" + RenameFunction.test1[i++]));}}}}}

文件复制类:

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_path,String file_Type) {// 启动循环函数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();// 判断是不是文件以及是否以你想要的文件类型结尾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 {// 如果文件名字相同,在点前面加数字进行区分// 注意用\\.进行分隔,而不是.String[] arr = new_path.split("\\.");String new_path1 = arr[0] + count + "." + arr[1];file1.renameTo(new File(new_path1));}// 是文件,所以开始复制文件fileCopyByBufferStreamArray(file.getAbsolutePath(), new_path);}else if (file.isFile() && !file.getAbsolutePath().endsWith(file_Type)) {// 注意这个方法体中什么都不写,就是不做处理} else {for (int i = 0; i < names.length; i++) {// 不是文件,进行迭代xunHuan(file.getAbsolutePath() + "/" + names[i], target_path,file_Type);}}}public static void fileCopyByBufferStreamArray(String srcFile,String targetFile) {// 用流的知识进行写文件File file = new File(srcFile);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 BufferedOutputStream(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) {}}}}

文件复制撤回类:

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) {// 记住不要把路径的那个文件夹删掉了if (file.exists()) {if (file.isFile()) {// 是文件,直接删除file.delete();} else if (file.isDirectory()) {File[] files = file.listFiles();for (int i = 0; i < files.length; i++) {// 如果不是文件,进行迭代deleteFile(files[i]);}}}}}

以上所述是小编给大家介绍的Java文件批量重命名批量提取特定类型文件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!