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()) {
- // 注意是test1
- f.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用来进行批量文件重命名,批量提取特定类型文件的更多相关文章
- Alt+Shift+R组合键,用来在一个java文件中批量的重命名变量。
myeclipse和eclipse集成编译软件,都提供了一个快捷键用来批量重命名变量:Alt+Shift+R组合键,用来在一个java文件中批量的重命名变量.扩展知识:如果想要重命名文件名,又不想手动 ...
- 【Java EE 学习 22 上】【文件上传】【目录打散】【文件重命名】
1.文件上传概述 (1)使用<input type="file">的方式来声明一个文件域. (2)表单提交方式一定要是post方式才行 (3)表单属性enctype 默 ...
- asp.net 文件批量移动重命名
最近闲时写了个批量移动重命名文件的工具 点击下载工具
- Java 文件重命名
Java 文件重命名 /** * 重命名文件 * @param fileName * @return */ public static void renameFile(String filePath, ...
- python工具 - 批量文件重命名
日常工作中经常会遇到这样的情况,需要将某个文件夹下的文件按着一定的规则进行重命名,当文件数量及其庞大的时候手工一个一个的去修需要耗费大量的时间,以下python工具可以协助批量修改文件名. 场景:某文 ...
- Java实现文件重命名
最近在做一个Android上面的一个文件管理器的apk,有文件名重命名和剪切的功能. 一般的思路如下: 重命名:先新建一个文件,复制原先的文件,读写文件,最后删除原先文件 剪切:先复制原先的文件,删除 ...
- 利用Python批量重命名一系列文件名杂乱的文件
假设目录下面有这样一系列命令杂乱的文件: OPENFOAM -TRAINING- PART- #1.pdf OPENFOAM - TRAINING- PART- #2.pdf OPENFOAM- TR ...
- python实现批量文件重命名
本文实例为大家分享了python批量文件重命名的具体代码,供大家参考,具体内容如下 问题描述 最近遇到朋友求助,如何将大量文件名前面的某些字符删除. 即将图中文件前的编号删除. Python实现 用到 ...
- Python实例31[批量对目录下文件重命名]
经常会遇到下载的文件或电子书,名字中间都包含了一些网址信息,实际使用中由于名字太长不方便,下面的脚本使用正则表达式来对目录下的所有文件重命名:例如: 修改前:[大家网]Mac OS X for Uni ...
随机推荐
- Base64正反编码
public class Base64 { private static char[] base64EncodeChars = new char[] { 'A', 'B', 'C', 'D', 'E' ...
- js性能优化
使用局部变量(尽量缩短作用域链)JavaScript引擎对变量的解析时间跟作用域链的深度有关.局部变量由于处于链尾,存取速度是最快的,因此,一个好的经验是:任何非局部变量使用超过一次时,请使用局部变量 ...
- Missing separate debuginfos
问题:Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64解决:1.将/etc/yum.rep ...
- SSE and Websocket
http://www.w3school.com.cn/html5/html_5_serversentevents.asp http://javascript.ruanyifeng.com/htmlap ...
- XAF ObjectSpace保存时添加记录
点击保存按钮时自动添加相关记录(用于界面查看),另外还有Audit控件可使用,此处只是一个简单的功能. 新建一个VC并关联到相关的View,代码如下: public partial class Gon ...
- Python生成器的经典程序
import random def get_data(): """返回0到9之间的3个随机数""" return random.sample ...
- centos7.2进入单用户模式
1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 4 - 现在按下 Co ...
- (转)CentOs上配置samba服务
前 言 在我们使用 Windows 作为客户机的时候,通常有文件.打印共享的需求.作为Windows 网络功能之一,通常可以在 Windows 客户机之间通过 Windows Network 固有的功 ...
- libev安装与示例程序编译运行
Linux平台C网络编程,之前总是看各大名著(如UNIX环境高级编程和UNIX网络编程,还有TCP/IP详解 卷1:协议和深入理解计算机系统(原书第2版)),同时写点小程序练习.然而还是拿不出手. 参 ...
- “设计之变”--从iPhone应用到iPad应用
在做APP的iPad版本设计时,我们常常需要考虑:如何在延续iPhone版本设计特色和优点同时,充分利用iPad的特性更好地进行设计.本文从iPad和iPhone的差异性入手,试图总结这一设计过程中需 ...