java按指定后缀复制文件并保留原始文件夹路径
为了只复制java原代码文件,不要class文件或其他不需要的文件,写的一个java窗口小程序
指定文件后缀,原始文件文件夹路径,目的文件夹路径,如原始路径为,D:\Projects,目的路径为C:\User\ABC\Desktop\newProject\,
要复制.java文件,复制后的路径为C:\User\ABC\Desktop\newProject\Projects\...不改变原路径,只复制.java文件
1 package copyfile; 2 /** 3 * @author ycl 4 * @date 2017年6月9日 下午7:31:18 5 * for copy file by postfix 6 */ 7 8 import java.awt.*; 9 import java.awt.event.*; 10 import java.io.BufferedReader; 11 import java.io.BufferedWriter; 12 import java.io.File; 13 import java.io.FileReader; 14 import java.io.FileWriter; 15 import java.io.Reader; 16 import java.io.Writer; 17 import java.util.ArrayList; 18 import java.util.List; 19 20 import javax.swing.*; 21 import javax.swing.filechooser.FileSystemView; 22 23 import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper; 24 25 public class CopyFile extends JFrame implements ActionListener { 26 /** 27 * 28 */ 29 private static final long serialVersionUID = 1L; 30 JLabel jlblSourcePath; 31 JLabel jlblDestPath; 32 JLabel jlblPostfix; 33 34 JLabel jlblStatu; 35 36 JTextField jtfSourcePath; 37 JTextField jtfDestPath; 38 JTextField jtfPostfix; 39 40 JButton jbSelectSP; 41 JButton jbSelectDP; 42 43 JButton jbOK; 44 45 JPanel jPanel = new JPanel(); 46 List<String> list = new ArrayList<>(); 47 48 public CopyFile() { 49 this.setTitle("Copy File By Postfix"); 50 this.setContentPane(jPanel); 51 this.setLayout(null); 52 this.setBounds(500, 200, 600, 300); 53 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 54 55 Font font = new Font("Courier", Font.ROMAN_BASELINE, 16); 56 jlblSourcePath = new JLabel("Source Path:"); 57 jlblSourcePath.setBounds(10, 10, 100, 20); 58 jlblSourcePath.setFont(font); 59 jPanel.add(jlblSourcePath); 60 61 jlblDestPath = new JLabel("Destination Path:"); 62 jlblDestPath.setBounds(10, 70, 200, 20); 63 jlblDestPath.setFont(font); 64 jPanel.add(jlblDestPath); 65 66 jlblPostfix = new JLabel("postfix"); 67 jlblPostfix.setBounds(320, 10, 100, 20); 68 jlblPostfix.setFont(font); 69 jPanel.add(jlblPostfix); 70 71 jtfSourcePath = new JTextField("select source path"); 72 jtfSourcePath.setBounds(10, 35, 300, 25); 73 jPanel.add(jtfSourcePath); 74 jtfDestPath = new JTextField("select distinction path"); 75 jtfDestPath.setBounds(10, 95, 300, 25); 76 jPanel.add(jtfDestPath); 77 jtfPostfix = new JTextField("java"); // 后缀 78 jtfPostfix.setBounds(320, 35, 80, 25); 79 jPanel.add(jtfPostfix); 80 81 jbSelectSP = new JButton("SELECT"); 82 jbSelectSP.setBounds(410, 35, 100, 25); 83 jPanel.add(jbSelectSP); 84 jbSelectSP.setFont(font); 85 jbSelectSP.addActionListener(this); 86 87 jbSelectDP = new JButton("SELECT"); 88 jbSelectDP.setBounds(320, 95, 190, 25); 89 jPanel.add(jbSelectDP); 90 jbSelectDP.addActionListener(this); 91 jbSelectDP.setFont(font); 92 93 jbOK = new JButton("OK"); 94 jbOK.setBounds(10, 150, 510, 30); 95 jPanel.add(jbOK); 96 jbOK.addActionListener(this); 97 jbOK.setFont(new Font("Courier", Font.BOLD, 20)); 98 jbOK.setForeground(Color.blue); 99 100 101 jlblStatu = new JLabel("CopyRight@2017 By Yin.cl", JLabel.CENTER); 102 jlblStatu.setBounds(10, 197, 520, 25); 103 jPanel.add(jlblStatu); 104 105 this.setVisible(true); 106 this.setResizable(false); 107 108 } 109 110 // get all filePath list from given path 111 public void getFileList(String path, String postfix) { 112 File file = new File(path); 113 postfix = jtfPostfix.getText(); 114 File[] fileList = null; 115 if (file.exists()) { 116 fileList = file.listFiles(); 117 if (fileList != null && fileList.length > 0) { 118 for (File f : fileList) { 119 if (f.isDirectory()) { 120 getFileList(f.getAbsolutePath(), postfix); 121 } else if (f.isFile()) { 122 String[] strArr = f.getName().split("\\."); 123 if (strArr[strArr.length - 1].equalsIgnoreCase(postfix)) { 124 list.add(f.getAbsolutePath()); 125 } 126 } 127 } 128 } 129 } else { 130 System.out.println("Given path is not exist!"); 131 } 132 } 133 134 // get select directory path for button select path 135 public String getSelectPath(String title) { 136 int result = 0; 137 String path = null; 138 JFileChooser fileChooser = new JFileChooser(); 139 FileSystemView fsv = FileSystemView.getFileSystemView(); // this is importent 140 fileChooser.setCurrentDirectory(fsv.getHomeDirectory()); 141 fileChooser.setDialogTitle(title); 142 fileChooser.setApproveButtonText("确定"); 143 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 144 result = fileChooser.showOpenDialog(this); 145 if (JFileChooser.APPROVE_OPTION == result) { 146 path = fileChooser.getSelectedFile().getPath(); 147 } 148 return path; 149 } 150 151 // click buttonOK 152 public void startExec() { 153 String path = jtfSourcePath.getText(); 154 String postfix = jtfPostfix.getText(); 155 list.clear(); //clean pre times fileList 156 getFileList(path, postfix); 157 String dpath = jtfDestPath.getText(); 158 if (list != null) { 159 for (String str : list) { 160 try { 161 Reader r = new FileReader(new File(str)); 162 String tempStr = str.split("\\:")[1]; 163 File newFile = new File(dpath + tempStr); 164 if (!newFile.getParentFile().exists()) { 165 newFile.getParentFile().mkdirs(); 166 } 167 Writer w = new FileWriter(newFile); 168 BufferedReader br = new BufferedReader(r); 169 BufferedWriter bw = new BufferedWriter(w); 170 String line = null; 171 while ((line = br.readLine()) != null) { 172 bw.write(line + "\r\n"); 173 } 174 bw.close(); 175 w.close(); 176 br.close(); 177 r.close(); 178 } catch (Exception ex) { 179 showMessage(ex.toString()); 180 } 181 } 182 showMessage("--Success--\n"); 183 } 184 } 185 186 // Check the sourcepath is equals distinationpath 187 public boolean checkPath(String spath, String dpath) { 188 if (spath != null && dpath != null) { 189 if (spath.equalsIgnoreCase(dpath)) { 190 return true; 191 } 192 } 193 return false; 194 } 195 196 // show message 197 public void showMessage(String msg) { 198 JOptionPane.showInternalMessageDialog(jPanel, msg, "Tips", JOptionPane.INFORMATION_MESSAGE); 199 } 200 201 @Override 202 public void actionPerformed(ActionEvent e) { 203 String path = null; 204 if (e.getSource().equals(jbSelectSP)) { 205 path = getSelectPath("Select Source Path"); 206 jtfSourcePath.setText(path); 207 if (checkPath(path, jtfDestPath.getText())) { 208 showMessage("This path is equals the distination path\n please select another one"); 209 jtfSourcePath.setText(""); 210 211 } 212 } else if (e.getSource().equals(jbSelectDP)) { 213 path = getSelectPath("Select Destination Path"); 214 jtfDestPath.setText(path); 215 if (checkPath(path, jtfSourcePath.getText())) { 216 jtfDestPath.setText(""); 217 showMessage("This path is equals the source path\n please select another one"); 218 } 219 } else if (e.getSource().equals(jbOK)) { 220 if (jtfSourcePath.getText().isEmpty() || jtfDestPath.getText().isEmpty()) { 221 showMessage("The Folder path is empty,\nplease check it."); 222 } else { 223 startExec(); 224 } 225 } 226 227 } 228 229 public static void main(String[] args) { 230 try { 231 BeautyEyeLNFHelper.launchBeautyEyeLNF(); 232 UIManager.put("RootPane.setupButtonVisible", false); 233 } catch (Exception e) { 234 e.printStackTrace(); 235 } 236 new CopyFile(); 237 } 238 }
效果图(皮肤jar包名:SwingSets3.jar)>>>