Java作為世界上最受歡迎的一門編程語言纵诞,自然是有原因的苞七。比如說我們可以直接的方便的調(diào)用其中的函數(shù)來實現(xiàn)我們想要的功能。
一個偶然的機會夺衍,我瀏覽API文檔時發(fā)現(xiàn)了一個名為FileDialog的類狈谊,然后就好奇并進行了查看,結(jié)果發(fā)現(xiàn)里面大有文章沟沙,藉此我是信了一個簡單的文件的遷移器河劝。話不多說,請看代碼:
首先我們需要一個業(yè)務(wù)邏輯類矛紫,也就是對文件進行操作的類(我們需要注意的是它的構(gòu)造函數(shù)有兩個參數(shù)赎瞎,這是為了今后的調(diào)用方便而設(shè)計的),使用它來方便的幫助我們對文件進行管理:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileToFile {
private String urlFrom;
private String urlTo;
private FileInputStream fis = null;
private FileOutputStream fos = null;
private BufferedReader reader = null;
private BufferedWriter writer = null;
public FileToFile(String urlFrom, String urlTo) {
this.urlFrom = urlFrom;
this.urlTo = urlTo;
try {
fis = newFileInputStream(new File(urlFrom));
fos = newFileOutputStream(new File(urlTo));
// reader=newBufferedReader(fis);
// writer=newBufferedWriter(fos);
int length;
byte[] buffer = newbyte[1024];
while ((length =fis.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fis != null) {
fis.close();
fis = null;
}
if(fos != null) {
fos.close();
fos = null;
}
} catch (Exception e){
e.printStackTrace();
}
}
}
}
有了業(yè)務(wù)邏輯類颊咬,那么我們還需要一個test類來檢測不是务甥,請看代碼:
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TestDemo extends JFrame {
/**
* 由于次數(shù)是建議的實現(xiàn),所以界面不是很好看喳篇。
* 需要注意的是Open按鈕的監(jiān)聽函數(shù)內(nèi)的FielDialog的創(chuàng)建及使用
*/
private static final long serialVersionUID = 1L;
private JButton Open, Destination;
private JTextField tf;
private JLabel label = null;
static FileDialog dialog;
public static void main(String[] args) {
new TestDemo();
}
public TestDemo() {
Open = new JButton("打開文件的位置<<<<<");
Destination = new JButton("文件輸出位置敞临,記得先在后面的輸入框中輸入文件路徑及拓展名");
tf = new JTextField(32);
label = new JLabel();
label.setText("在這里顯示一些hint信息");
this.setTitle("文件遷移器!");
this.setSize(500, 400);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.add(Open);
this.add(Destination);
this.add(tf);
this.add(label, BorderLayout.CENTER);
Open.addActionListener(newActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
dialog = new FileDialog(TestDemo.this, "文件窗口",FileDialog.LOAD);
dialog.show();
label.setText("文件來源:" +dialog.getDirectory() + "\n"
+ dialog.getFile());
System.out.println("文件來源:" +dialog.getDirectory() + "\n"
+ dialog.getFile());
}
});
Destination.addActionListener(newActionListener(){
@Override
public voidactionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
String urlTo=tf.getText().trim().toString();
String urlFrom=dialog.getDirectory()+dialog.getFile().trim().toString();
try{
if(urlFrom!=null&&urlTo!=null){
FileToFile translator=newFileToFile(urlFrom,urlTo);
JOptionPane.showMessageDialog(TestDemo.this, "文件轉(zhuǎn)移完畢:枷丁S窗怼!");
}else{
JOptionPane.showConfirmDialog(null,"請選擇文件來源痰憎,或者確保您填寫了文件的輸出位置及相應(yīng)的拓展名","警告!E屎铣耘!",JOptionPane.YES_NO_OPTION);
}
}catch(Exception ex){
ex.printStackTrace();
System.out.println("您還沒有選擇相應(yīng)的路徑!");
}
}
});
}
}
由此以故,我們便完成了蜗细,是不是感覺很簡單啊 ?俗話說沒有“證據(jù)”不足以讓人信服怒详,那么下面讓我們一起看一下程序完成的效果吧炉媒!