如何根据文件名将文件移动到不同的目录?

好的,我创建了一个程序,它将根据创建日期重命名目录中的文件。 我现在需要能够根据创建日期将这些文件移动到不同的目录中 – 20131202-1.jpg将进入名为20131202的文件夹和名为20131203-2.jpg的文件到名为20131203,20131130-1的文件夹中。 jpg将进入名为20131130的文件夹 – 如果需要,创建目录等。有一种简单的方法吗? 这是我用于重命名的代码:

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.text.SimpleDateFormat; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class FileRenameAndMove extends JFrame implements ActionListener { private JTextField filePath; private JTextField outputFile; private JButton input; private JButton output; private JButton rename; JFileChooser chooser ; File input_Folder = null; File output_Folder = null; public ExtraCredit(){ filePath = new JTextField(); outputFile = new JTextField(); chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); input = new JButton("select input Folder"); output = new JButton("select output Folder"); rename = new JButton("rename File"); setTitle("File Mover"); setLayout(null); setVisible(true); setSize(500,200); filePath.setBounds(10,10,250,30); outputFile.setBounds(10,50,250,30); input.setBounds(280,10,150,30); output.setBounds(280,50,150,30); rename.setBounds(280, 90,150,30); add(filePath); add(outputFile); add(input); add(output); add(rename); input.addActionListener(this); output.addActionListener(this); rename.addActionListener(this); public static void main(String[] args) { new ExtraCredit(); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==input){ int option = chooser.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { input_Folder=chooser.getSelectedFile(); filePath.setText(input_Folder.getAbsolutePath()); } } else if(e.getSource()==output){ int option = chooser.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { output_Folder=chooser.getSelectedFile(); outputFile.setText(output_Folder.getAbsolutePath()); } } else if(e.getSource()==rename){ if(input_Folder==null||output_Folder==null){ JOptionPane.showMessageDialog(this, "Please select the source and target folder", "File error", JOptionPane.ERROR_MESSAGE); } else{ if(input_Folder.exists()){ SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd"); File[] fileList = input_Folder.listFiles(); for(int i=0;i<fileList.length;i++){ new File(output_Folder.getAbsolutePath()+"/"+dateFormat.format(fileList[i].lastModified())).mkdir(); fileList[i].renameTo(new File(output_Folder.getAbsolutePath()+"/"+dateFormat.format(fileList[i].lastModified())+"/"+fileList[i].getName())); } JOptionPane.showMessageDialog(this, "Files renamed successfully!!", "Information", JOptionPane.INFORMATION_MESSAGE); } else JOptionPane.showMessageDialog(this, "Source folder doesn't exists", "File error", JOptionPane.ERROR_MESSAGE); } } } } 

如果您可以提供有关如何进行复制的示例,我将不胜感激。

我写了一个方法来做到这一点。 它将首先获取当前目录并收集其中所有文件+文件夹的列表(不是递归的)。 然后,它将遍历内容并确保它是一个文件中包含“ – ”,在这种情况下,它将生成文件夹并移动文件。

 import java.io.*; public class tmp { public static void main(String[] args) throws IOException { File folder = new File(System.getProperty("user.dir")); File[] files = folder.listFiles(); for(int i = 0; i < files.length; i++) { if(files[i].isFile()) { String name = files[i].getName(); if(name.indexOf("-") != -1) { System.out.println(name); name = name.substring(0, name.indexOf("-")); new File(System.getProperty("user.dir") + "/" + name + "/").mkdirs(); files[i].renameTo(new File(System.getProperty("user.dir") + "/" + name + "/" + files[i].getName())); } } } } } 
  1. 尝试使用mkdirs()而不是mkidr()来创建目录,因为: mkdirs()创建此抽象路径名所指定的目录,包括任何必要但不存在的父目录。

  2. file1.renameTo(file2)将文件从一个目录移动到另一个目录是不可靠的。 文档说:

此方法行为的许多方面本质上都依赖于平台: 重命名操作可能无法将文件从一个文件系统移动到另一个文件系统,它可能不是primefaces的,如果具有目标抽象路径名的文件,它可能不会成功已经存在。 应始终检查返回值以确保重命名操作成功。

因此,首先使用流复制器(读取器和/或编写器)方法将目标文件复制到目标目录,然后使用delete()函数delete()目标文件。 或者您可以使用JAVA NIO支持性能:

  public static void copyFile(File sourceFile, File destFile) throws IOException { if(!destFile.exists()) { destFile.createNewFile(); } try( FileChannel source = new FileInputStream(sourceFile).getChannel(); FileChannel destination = new FileOutputStream(destFile).getChannel() ) { destination.transferFrom(source, 0, source.size()); } }