java保存文件夹跟随路径_java按指定后缀复制文件并保留原始文件夹路径

这是一个Java程序,用于根据指定的文件后缀复制文件,并确保复制过程中保留原始文件夹路径。程序包含一个GUI界面,用户可以输入源路径、目标路径和文件后缀,然后选择文件夹进行复制操作。
摘要由CSDN通过智能技术生成

1 packagecopyfile;2 /**

3 *@authorycl4 * @date 2017年6月9日 下午7:31:185 * for copy file by postfix6 */

7

8 import java.awt.*;9 import java.awt.event.*;10 importjava.io.BufferedReader;11 importjava.io.BufferedWriter;12 importjava.io.File;13 importjava.io.FileReader;14 importjava.io.FileWriter;15 importjava.io.Reader;16 importjava.io.Writer;17 importjava.util.ArrayList;18 importjava.util.List;19

20 import javax.swing.*;21 importjavax.swing.filechooser.FileSystemView;22

23 importorg.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;24

25 public class CopyFile extends JFrame implementsActionListener {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 = newJPanel();46 List list = new ArrayList<>();47

48 publicCopyFile() {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 voidgetFileList(String path, String postfix) {112 File file = newFile(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 publicString getSelectPath(String title) {136 int result = 0;137 String path = null;138 JFileChooser fileChooser = newJFileChooser();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 returnpath;149 }150

151 //click buttonOK

152 public voidstartExec() {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(newFile(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 = newFileWriter(newFile);168 BufferedReader br = newBufferedReader(r);169 BufferedWriter bw = newBufferedWriter(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 booleancheckPath(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 voidshowMessage(String msg) {198 JOptionPane.showInternalMessageDialog(jPanel, msg, "Tips", JOptionPane.INFORMATION_MESSAGE);199 }200

201 @Override202 public voidactionPerformed(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 voidmain(String[] args) {230 try{231 BeautyEyeLNFHelper.launchBeautyEyeLNF();232 UIManager.put("RootPane.setupButtonVisible", false);233 } catch(Exception e) {234 e.printStackTrace();235 }236 newCopyFile();237 }238 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值