Windows 운영 체제는 이름 바꾸기 파일을 구현할 수 있지만 배치 라이닝을 구현할 수는 없습니다. 이 예제는 배치 변경 기능을 구현하고 특정 규칙에 따라 폴더에서 동일한 유형의 파일 이름을 바꿀 수 있습니다. 사용자는 템플릿 이름을 바꿀 수 있으며 프로그램은 템플릿에 따라 해당 파일 이름을 바꿀 수 있습니다. 또한 이름 바꾸기 템플릿에 특수 기호를 추가 할 수 있으며 프로그램은 이러한 특수 기호를 이름이 변경된 파일 번호로 바꿉니다.
아이디어 분석 :
1. 뷰 레이어를 보면 일부 jlabel 컨트롤은 사용자가 선택한 경로를 표시하고 파일 이름 템플릿을 입력하고 확장자를 입력해야합니다. 반복 시작. 이름, jseparator 컨트롤은 분할 라인을 나타내고, JSPInner 컨트롤은 시작 번호를 나타내고, jscrollpane 컨트롤은 컨테이너 역할을하며, jtable 컨트롤이 내부에 배치되어 이전 파일 이름과 새 파일 이름을 나열합니다.
2. 모델 레이어를 다시보십시오. 먼저, 브라우징 버튼의 이벤트 처리 방법을 정의하고,이 메소드에서 jfilechooser 파일 선택기를 만들고, jfilechooser 클래스의 setfileSelectionMode () 메소드를 사용하여 폴더를 선택하고 열린 대화 상자를 JFILECHOOSER 클래스가 버튼을 확인하여 jfilechooser 클래스의 getSelectedFile () 메소드를 사용하여 선택한 폴더를 가져오고 폴더 정보를 표시합니다.
3. 클래스를 정의하여 파일 필터 인터페이스를 구현하고 클래스 생성자에 파일 확장자를 저장 한 다음 문자열 클래스의 endStwith () 메소드가 파일 확장자를 필터링하는 데 사용되는 메소드를 정의합니다.
4. 그런 다음 시작 버튼의 이벤트 처리 방법을 정의하십시오. jtextfield 컨트롤의 getText () 메소드를 사용하여 템플릿 문자열을 사용하십시오. Joptionpane 클래스의 방법을 사용한 다음 jtable 클래스를 사용하여 테이블 데이터 모델을 사용하십시오 () jspinner 클래스의 메소드 시작 번호를 얻고 문자열 클래스의 indexof 메소드를 사용하여 첫 번째 "#"문자열 클래스 substring ()의 인덱스를 가져옵니다. 문자열 클래스의 대체 () 메소드는 템플릿에서 숫자 자리 표시기 문자열을 얻는 데 사용됩니다. 템플릿의 자리 표시자를 지정된 형식으로 바꾸십시오. 그런 다음 파일 클래스 listFiles () 메소드를 사용하여 폴더에서 파일 목록 배열을 가져오고, oreach ()를 사용하여 각 파일을 통해 루프하고 문자열 클래스의 형식 () 메소드를 통해 각 파일 이름을 형식화하고 addrow () 메소드를 사용합니다. DefaultTableModel 클래스의 기존 파일을 회전시키기위한 이름과 새 이름을 테이블의 데이터 모델에 추가하십시오. 새 파일 이름으로 마지막으로 파일 클래스 구현 파일 이름 바꾸기의 Renameto () 메소드를 사용하십시오.
코드는 다음과 같습니다.
코드 사본은 다음과 같습니다.
java.awt.eventqueue import;
import java.awt.event.actionevent;
import java.awt.event.actionListener;
import java.io.file;
import java.io.filefilter;
import javax.swing.jbutton;
import javax.swing.jfilechooser;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jpanel;
import javax.swing.jscrollpane;
import javax.swing.jseparator;
import javax.swing.jspinner;
import javax.swing.jtable;
import javax.swing.jtextfield;
import javax.swing.border.emptyborder;
import javax.swing.table.defaultTableModel;
java.awt.gridbaglayout import;
java.awt.gridbagconstraints import;
java.awt.insets 가져 오기;
/**
* 파일 목록의 필터를 가져옵니다
*
* @Author Li Zhongwei
*/
공개 클래스 renamefiles는 jframe {를 확장합니다.
/**
*
*/
개인 정적 최종 최종 긴 SerialversionUID = 4534371106024773867L;
개인 최종 클래스 ExtNameFileFilter는 파일 필터를 구현합니다.
개인 문자열 ExtName;
public extNameFileFilter (String ExtName) {
this.extname = extName; // 파일 확장자 저장
}
@보수
공개 부울 수락 (파일 pathname) {
// 필터 파일 확장자
if (pathname.getName (). toupperCase ()
.endswith (extname.toupperCase ()))
진실을 반환하십시오.
거짓을 반환합니다.
}
}
개인 jpanel contentpane;
Private Jtextfield Forwardfield;
개인 Jtextfield Templetfield;
개인 파일 dir;
개인 JTable 테이블;
개인 Jtextfield ExtNamefield;
개인 jspinner startspinner;
/**
* 응용 프로그램을 시작하십시오.
*/
public static void main (String [] args) {
eventqueue.invokelater (new Runnable () {
public void run () {
노력하다 {
RenameFiles Frame = New RenameFiles ();
frame.setVisible (true);
} catch (예외 e) {
e.printstacktrace ();
}
}
});
}
/**
* 프레임을 만듭니다.
*/
public renamefiles () {
setResizable (false);
settitle ( "파일 배치 이름 변경");
setDefaultCloseOperation (jframe.exit_on_close);
세트 바운드 (100, 100, 383, 409);
ContentPane = 새로운 jpanel ();
ContentPane.SetBorder (New Emptyborder (5, 5, 5, 5));
SetContentPane (ContentPane);
gridbaglayout gbl_contentpane = new Gridbaglayout ();
gbl_contentpane.columnwidths = new int [] {72, 54, 60, 87, 91, 0};
gbl_contentpane.rowheights = new int [] {25, 25, 10, 25, 24, 25, 2,
216, 0};
gbl_contentpane.columnweights = new Double [] {0.0, 0.0, 0.0, 0.0, 0.0,
double.min_value};
gbl_contentpane.rowweights = new Double [] {0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, double.min_value};
ContentPane.setLayout (gbl_contentpane);
Jlabel 레이블 = New Jlabel ();
label.settext ( "파일 배치 이름 바꾸기 모듈 :");
gridbagconstraints gbc_label = new Gridbagconstraints ();
gbc_label.fill = gridbagconstraints.vertical;
gbc_label.insets = 새로운 삽입 (0, 0, 5, 5);
gbc_label.gridwidth = 3;
gbc_label.gridx = 1;
gbc_label.gridy = 0;
contentpane.add (레이블, gbc_label);
jlabel label_1 = new Jlabel ();
label_1.settext ( "파일 경로 :");
gridbagconstraints gbc_label_1 = new gridbagconstraints ();
gbc_label_1.anchor = gridbagconstraints.east;
gbc_label_1.fill = gridbagconstraints.vertical;
gbc_label_1.insets = 새로운 삽입 (0, 0, 5, 5);
gbc_label_1.gridx = 0;
gbc_label_1.gridy = 1;
contentpane.add (label_1, gbc_label_1);
Forderfield = New Jtextfield ();
Forderfield.settext ( "");
Gridbagconstraints gbc_forderfield = New Gridbagconstraints ();
gbc_forderfield.fill = gridbagconstraints.horizontal;
gbc_forderfield.insets = 새로운 삽입 (0, 0, 5, 5);
gbc_forderfield.gridwidth = 3;
gbc_forderfield.gridx = 1;
gbc_forderfield.gridy = 1;
contentpane.add (Forderfield, GBC_Forderfield);
JBUTTON 버튼 = 새로운 JBUTTON ();
button.addactionListener (new ActionListener () {
public void actionperformed (ActionEvent e) {
do_button_actionperformed (e);
}
});
button.settext ( "찾아보기");
Gridbagconstraints GBC_BUTTON = New GridbagConstraints ();
gbc_button.anchor = gridbagconstraints.northwest;
gbc_button.insets = 새로운 삽입 (0, 0, 5, 0);
gbc_button.gridx = 4;
gbc_button.gridy = 1;
contentpane.add (버튼, gbc_button);
JSEPARATOR SEPLICATOR_1 = NEW JSEPARATOR ();
gridbagconstraints gbc_separator_1 = new Gridbagconstraints ();
gbc_separator_1.fill = gridbagconstraints.both;
gbc_separator_1.insets = 새 삽입 (0, 0, 5, 0);
gbc_separator_1.gridwidth = 5;
gbc_separator_1.gridx = 0;
gbc_separator_1.gridy = 2;
contentpane.add (segrator_1, gbc_separator_1);
jlabel label_5 = new Jlabel ();
label_5.settext ( "숫자 수에 의해 점유 된 위치를 지정하려면 #을 사용하고 *를 사용하여 원본 파일 이름을 삽입합니다 :");
gridbagconstraints gbc_label_5 = 새로운 gridbagconstraints ();
gbc_label_5.fill = gridbagconstraints.vertical;
gbc_label_5.insets = 새로운 삽입 (0, 0, 5, 0);
gbc_label_5.gridwidth = 5;
gbc_label_5.gridx = 0;
gbc_label_5.gridy = 3;
contentpane.add (label_5, gbc_label_5);
jlabel label_3 = new Jlabel ();
label_3.settext ( "템플릿 :");
gridbagconstraints gbc_label_3 = new gridbagconstraints ();
gbc_label_3.anchor = gridbagconstraints.east;
gbc_label_3.fill = gridbagconstraints.vertical;
gbc_label_3.insets = 새 삽입 (0, 0, 5, 5);
gbc_label_3.gridx = 0;
gbc_label_3.gridy = 4;
contentpane.add (label_3, gbc_label_3);
Templetfield = 새로운 jtextfield ();
templetfield.settext ( "Catrestaurant ###");
Gridbagconstraints GBC_TEMPLETFIELD = New GridbagConstraints ();
gbc_templetfield.anchor = gridbagconstraints.south;
gbc_templetfield.fill = gridbagconstraints.horizontal;
gbc_templetfield.insets = 새로운 삽입 (0, 0, 5, 5);
gbc_templetfield.gridwidth = 3;
gbc_templetfield.gridx = 1;
gbc_templetfield.gridy = 4;
contentpane.add (Templetfield, GBC_Templetfield);
jlabel label_4 = new Jlabel ();
label_4.settext ( "시작 :");
gridbagconstraints gbc_label_4 = new gridbagconstraints ();
gbc_label_4.fill = gridbagconstraints.vertical;
gbc_label_4.insets = 새로운 삽입 (0, 0, 5, 5);
gbc_label_4.gridx = 0;
gbc_label_4.gridy = 5;
contentpane.add (label_4, gbc_label_4);
startSpinner = 새로운 jspinner ();
gridbagconstraints gbc_startspinner = new Gridbagconstraints ();
gbc_startspinner.fill = gridbagconstraints.horizontal;
GBC_STARTSPINNER.INSETS = 새로운 삽입 (0, 0, 5, 5);
gbc_startspinner.gridx = 1;
gbc_startspinner.gridy = 5;
contentpane.add (startspinner, gbc_startspinner);
jlabel label_2 = new Jlabel ();
label_2.settext ( "확장 :");
gridbagconstraints gbc_label_2 = new gridbagconstraints ();
gbc_label_2.fill = gridbagconstraints.horizontal;
gbc_label_2.insets = 새로운 삽입 (0, 0, 5, 5);
gbc_label_2.gridx = 2;
gbc_label_2.gridy = 5;
contentpane.add (label_2, gbc_label_2);
JBUTTON STARTBUTTON = NEW JBUTTON ();
startButton.addactionListener (new ActionListener () {
public void actionperformed (ActionEvent e) {
do_startbutton_actionperformed (e);
}
});
extNamefield = 새로운 jtextfield ();
extNamefield.settext ( "JPG");
gridbagconstraints gbc_extnamefield = new gridbagconstraints ();
gbc_extnamefield.fill = gridbagconstraints.horizontal;
gbc_extnamefield.insets = 새로운 삽입 (0, 0, 5, 5);
gbc_extnamefield.gridx = 3;
gbc_extnamefield.gridy = 5;
contentpane.add (extNamefield, gbc_extnamefield);
startButton.settext ( "시작");
gridbagconstraints gbc_startbutton = new Gridbagconstraints ();
gbc_startbutton.anchor = gridbagconstraints.north;
GBC_STARTBUTTON.INSETS = 새로운 삽입 (0, 0, 5, 0);
gbc_startbutton.gridx = 4;
gbc_startbutton.gridy = 5;
contentpane.add (startButton, gbc_startButton);
JSEPARATOR SEPLICATOR_2 = NEW JSEPARATOR ();
gridbagconstraints gbc_separator_2 = new Gridbagconstraints ();
gbc_separator_2.anchor = gridbagconstraints.north;
gbc_separator_2.fill = gridbagconstraints.horizontal;
gbc_separator_2.insets = 새로운 삽입 (0, 0, 5, 0);
gbc_separator_2.gridwidth = 5;
gbc_separator_2.gridx = 0;
gbc_separator_2.gridy = 6;
contentpane.add (segrator_2, gbc_separator_2);
jscrollpane scrollpane = new jscrollpane ();
gridbagconstraints gbc_scrollpane = new gridbagconstraints ();
gbc_scrollpane.fill = gridbagconstraints.both;
gbc_scrollpane.gridwidth = 5;
gbc_scrollpane.gridx = 0;
gbc_scrollpane.gridy = 7;
contentpane.add (scrollpane, gbc_scrollpane);
표 = 새로운 jtable ();
table.setModel (new defaultTableModel (새 개체 [] [] {}, 새 문자열 [] {
"오래된 파일 이름", "새 파일 이름"}));
scrollpane.setViewPortView (표);
}
/**
* 브라우징 버튼 이벤트를 처리하는 방법
*
* @param e
*/
보호 된 void do_button_actionperformed (ActionEvent e) {
JFILECHOOSER ChooSer = new JFILECHOOSER (); // 파일 선택기를 만듭니다
// 폴더 선택 만 설정합니다
chooser.setfileSelectionMode (jfilechooser.directories_only);
int 옵션 = chooser.showopendialog (this); // 열린 대화 상자 표시
if (옵션 == jfileChooser.Approve_Option) {
dir = chooser.getSelectedFile (); // 선택한 폴더를 가져옵니다
} 또 다른 {
dir = null;
}
Forderfield.settext (dir + ""); // 폴더 정보 표시
}
/**
* 시작 버튼 이벤트 처리 방법
*
* @param e
*/
보호 된 void do_startbutton_actionperformed (ActionEvent e) {
문자열 templet = templetfield.getText (); // 템플릿 문자열을 가져옵니다
if (templet.isempty ()) {
joptionpane.showmessagedialog ( "템플릿 이름 바꾸기 확인", "정보 대화 상자",
joptionpane.warning_message);
반품;
}
// 테이블 데이터 모델을 가져옵니다
defaultTableModel model = (defaultTableModel) table.getModel ();
model.setrowcount (0); // 테이블 데이터를 지우십시오
int bi = (Integer) startSpinner.getValue (); // 시작 번호를 얻습니다
int index = templet.indexof ( "#"); // 첫 번째 "#"의 색인을 가져옵니다.
문자열 코드 = 템플릿 .substring (index); // 템플릿에서 숫자 자리 표시기 문자열을 가져옵니다.
// 템플릿의 숫자 자리 표시기 문자열을 지정된 형식으로 바꾸십시오.
templet = templet.replace (코드, "%0" + code.length () + "d");
String extName = extNamefield.getText (). tolowercase ();
if (extname.indexof ( ".") == -1)
extname = "."
// 파일에서 파일 목록 배열을 가져옵니다
file [] files = dir.listfiles (new extNameFileFilter (extName));
for (파일 파일 : 파일) {// 변수 파일 배열
// 각 파일 이름을 형식화합니다
문자열 이름 = string.format (템플릿, bi ++)+extName;
// 파일의 이전 및 새 이름을 테이블의 데이터 모델에 추가
model.addrow (new String [] {file.getName (), name});
파일 parentfile = file.getParentFile (); // 파일이있는 폴더 개체 가져 오기
file newFile = 새 파일 (parentfile, name);
file.renameto (newfile); // 파일 이름 바꾸기
}
}
}
효과는 그림에 나와 있습니다.