写的很久了,没有用线程,比较慢
代码如下
//package mp3;
import java.awt.BorderLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.Rectangle;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
/**
* <p>Title:根据文件类型生成相应的文件 </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007.02.08 0:30 </p>
* 05.14 23:40
*
* <p>Company: </p>
*
* @author Goley Yu
* @version 1.1
*/
public class Mp3Seach
extends JFrame {
String strpath = null;
String[] str = {
"mp3,", "mp4,", "3gp,", "avi,", "asf"};
Vector strtmp = null;
String tmp = null;
public Mp3Seach() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
// jTextArea1.setText("jTextArea1");
jTextArea1.setBounds(new Rectangle(43, 124, 286, 77));
jButton1.setBounds(new Rectangle(117, 209, 109, 28));
jButton1.setText("生成歌曲清单");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn();
}
});
jLabel1.setText("输入要搜索的歌曲路径:");
jLabel1.setBounds(new Rectangle(28, 17, 145, 21));
jTextField1.setToolTipText("");
jTextField1.setText("d://mp3cnttec//song");
btn_search.setBounds(new Rectangle(310, 42, 60, 21));
btn_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(JFileChooser.APPROVE_OPTION==jfc.showOpenDialog(null))
jTextField1.setText(jfc.getSelectedFile().getPath());
}
});
jTextField1.setBounds(new Rectangle(49, 42, 250, 21));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(jLabel1);
jLabel2.setText("输入要搜索的文件的后缀名:");
jLabel2.setBounds(new Rectangle(42, 96, 173, 23));
this.getContentPane().add(jTextField1);
this.getContentPane().add(btn_search);
this.getContentPane().add(jButton1);
this.getContentPane().add(jTextArea1);
this.getContentPane().add(jLabel2);
for (int i = 0; i < str.length; i++) {
jTextArea1.append(str[i]);
}
}
//按钮中执行的函数btn()
void btn() {
//获取文本域里数据根据“,”分开
tmp = jTextArea1.getText();
strtmp = new Vector();
System.out.println(tmp);
int k = 0;
for (int i = 0; i < tmp.length(); i++) {
//判断“,”号所在的位置i,第一次k要为0
if (',' == tmp.charAt(i)) {
strtmp.addElement(tmp.substring(k, i));
// System.out.println(strtmp);
k = i;
//要把逗号的位向后移动一位k+1
if (k != 0) {
k = k + 1;
}
}
}
//把获取到的字符送到向量strtmp
strtmp.addElement(tmp.substring(k, tmp.length()));
deleFile(strtmp);
//调用函数madeFile(strtmp),生成文件
madeFile(strtmp);
}
void deleFile(Vector strtmp) {
String tmpok = " ";
for (int i = 0; i < strtmp.size(); i++) {
//将要建立的文件名加入后缀".txt"
File file = new File( (String) strtmp.elementAt(i) + ".txt");
if (file.exists()) {
file.delete();
tmpok = tmpok + file.toString();
System.out.println("tmpok "+tmpok);
}
}
System.out.println(tmpok + "被删除");
}
void madeFile(Vector strtmp) {
//将Vector中的量强制转换成String,并建立File
strpath = jTextField1.getText();
File filepath = new File(strpath);
if (filepath.isDirectory()) {
String tmpok = "";
for (int i = 0; i < strtmp.size(); i++) {
//将要建立的文件名加入后缀".txt"
File file = new File( (String) strtmp.elementAt(i) + ".txt");
//弹出窗口中的字符
tmpok = " " + file.toString() + tmpok;
//判断文件是否存在
if (!file.exists()) {
try {
FileWriter filewrite = new FileWriter(file, true);
PrintWriter pwrite = new PrintWriter(filewrite);
//调用函数serchfile,传入三个参数 文件路径, 文件后缀名 ,写入的文件流
searchfile(filepath, (String) strtmp.elementAt(i),
pwrite);
pwrite.close();
filewrite.close();
// pwrite.print();
}
catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.toString());
}
}
}
JOptionPane.showMessageDialog(null,
tmpok + " 文件建立成功");
}
else {
JOptionPane.showMessageDialog(null, "该目录不存在请重新输入");
// jTextField1.setVerifyInputWhenFocusTarget(true);
}
}
//搜索文件filepath判断该文件是否是已txtformat格式结尾的,写入tempwrite
void searchfile(File filepath, String txtformat, PrintWriter tempwrite) {
//获取文件列表
File[] allfile = filepath.listFiles();
for (int i = 0; i < allfile.length; i++) {
if (allfile[i].isDirectory()) { //是目录调自己
searchfile(allfile[i], txtformat, tempwrite);
}
else if (allfile[i].isFile()) { //是文件
//转化获取的文件名为小写字母
String str = allfile[i].getName().toLowerCase();
String strP=allfile[i].getParent().substring(allfile[i].getParent().lastIndexOf("//")+1,allfile[i].getParent().length());
System.out.println(strP+" "+str);
if (str.endsWith(txtformat)) { //判断是否是该文件格式
//该文件名写入tempwrite
// tempwrite.println(str);
tempwrite.println(strP+" "+str);
// System.out.println(allfile[i].getName()+"该文件是已"+txtformat+"为后缀");
}
}
}
}
//判断是否存在该字符串
public static void main(String[] args) {
Mp3Seach mp3seach = new Mp3Seach();
mp3seach.setSize(400, 300);
mp3seach.setVisible(true);
// mp3seach.searchfile(new File("c:/1"),"mp3");
}
JButton btn_search = new JButton("浏览");
JTextArea jTextArea1 = new JTextArea(4, 20);
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
}