Java实现简易的文件的迁移器

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("您还没有选择相应的路径!");
            }
        }

    });
}

}

由此,我们便完成了,是不是感觉很简单啊 ?俗话说没有“证据”不足以让人信服,那么下面让我们一起看一下程序完成的效果吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值