JAVA读取遍历文件夹中的文件

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class updatefiles extends JFrame implements ActionListener

{

public static void main(String[] args)

{

// TODO 自动生成的方法存根

updatefiles updatefiles = new updatefiles();

}

public updatefiles()

{

JButton jb_ok = new JButton("提交");

JPanel jp1 = new JPanel();

JPanel jp2 = new JPanel();

jp1.setPreferredSize(new Dimension(300, 150));

jp2.setPreferredSize(new Dimension(300, 150));

this.setLayout(new BorderLayout());

this.add(jp1, BorderLayout.NORTH);

this.add(jp2, BorderLayout.SOUTH);

this.add(jb_ok, BorderLayout.CENTER);

this.setSize(300, 400);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

jb_ok.addActionListener(this);

}

@Override

public void actionPerformed(ActionEvent e)

{

// TODO 自动生成的方法存根

listfile("D:");

JOptionPane.showMessageDialog(null, "修改成功", "系统提示", JOptionPane.PLAIN_MESSAGE);

}

public void listfile(String filepath)

{

try

{

File file = new File(filepath);

String[] files = file.list();

for (int i = 0; i < files.length; i++)

{

File filedest = new File(filepath + "\\" + files[i]);

if (filedest.isDirectory())

{

listfile(filedest.getPath());

} else

{

String filename = filedest.getName();

String filepaths = filedest.getPath().toString();

int index = filename.indexOf(":");

// if (index != -1)

// {

String newfilename = filename;

for (int j = 0; j < 25; j++)

{

if (newfilename.indexOf(Integer.toString(j)) == 0)

{

newfilename = newfilename.replace(Integer.toString(j), "");

}

}

String fullpath = filedest.getParent() + "\\" + newfilename;

File oldfile = new File(filepaths);

oldfile.renameTo(new File(fullpath));//移动文件

// }

}

}

} catch (Exception e)

{

// TODO: handle exception

e.printStackTrace();

}

}

}

你可能感兴趣的:(JAVA读取遍历文件夹中的文件)