代码:
import java.io.*;
import java.io.File.*;
import java.awt.*;
import java.awt.event.*;
public class OK extends Frame implements ActionListener
{
public Label lAddress;
public Label lFormer;
public Label lLatter;
public TextField tfOK;
public Button bOK;
public TextField tfFormer;
public TextField tfLatter;
public TextField tfAddress;
public OK(String s)
{
super(s);
lAddress=new Label( "文件夹: ");
tfAddress=new TextField(18);
lFormer=new Label( "原来的扩展名: ");
tfFormer=new TextField(18);
lLatter=new Label( "改成的扩展名: ");
tfLatter=new TextField(18);
bOK=new Button( "确定 ");
setLayout(new GridLayout(8,1));
add(lAddress);
add(tfAddress);
add(lFormer);
add(tfFormer);
add(lLatter);
add(tfLatter);
add(bOK);
add(tfOK);
bOK.addActionListener(this);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
}
public void actionPerformed(ActionEvent e)
{
if(tfAddress.getText()==null)
{
tfOK.setText( "无有效文件夹。 ");
System.exit(0);
}
else
{
File n=new File(tfAddress.getText());
/*if(!n.exist())
{
tfOK.setText( "不是有效的文件夹。 ");
System.exit(0);
}*/
if(tfFormer.getText()==null||tfLatter.getText()==null)
//||(!(tfFormer.getText().startWith( ". ")))||(!(tfLatter.getText().startWith( ". "))))
{
tfOK.setText( "文件格式不明确。 ");
System.exit(0);
}
File flDir=new File(tfAddress.getText());
String [] files =flDir.list();
for(int i=0;i <files.length;i++)
{
File flTemp=new File(files[i]);
String s=new String(files[i]);
if(flTemp.isDirectory())
continue;
String str=s.subSting(1,9);
String name=str.concat(tfLatter.getText());
File newName=new File (name);
flTemp.renameTo(newName);
}
tfOK.setText( "完成! ");
}
public static void main(String [] args)
{
OK o=new OK( "批量改扩展名 ");
o.setVisible(true);
};
}
}
提示错误:
C:\JBuilder6\jdk1.3.1\bin> javac OK.java