Java作為世界上最受歡迎的一門編程語言,自然是有原因的。比如說我們可以直接的方便的調用其中的函數來實現我們想要的功能。
一個偶然的機會,我瀏覽API文檔時發現了一個名為FileDialog的類,然后就好奇并進行了查看,結果發現里面大有文章,藉此我是信了一個簡單的文件的遷移器。話不多說,請看代碼:
首先我們需要一個業務邏輯類,也就是對文件進行操作的類(我們需要注意的是它的構造函數有兩個參數,這是為了今后的調用方便而設計的),使用它來方便的幫助我們對文件進行管理:
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();
}
}
}
}
有了業務邏輯類,那么我們還需要一個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 {
/**
* 由于次數是建議的實現,所以界面不是很好看。
* 需要注意的是Open按鈕的監聽函數內的FielDialog的創建及使用
*/
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, "文件轉移完畢!!!");
}else{
JOptionPane.showConfirmDialog(null,"請選擇文件來源,或者確保您填寫了文件的輸出位置及相應的拓展名","警告!!!",JOptionPane.YES_NO_OPTION);
}
}catch(Exception ex){
ex.printStackTrace();
System.out.println("您還沒有選擇相應的路徑!");
}
}
});
}
}
由此,我們便完成了,是不是感覺很簡單啊 ?俗話說沒有“證據”不足以讓人信服,那么下面讓我們一起看一下程序完成的效果吧!