Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
文件加密器,操作过程肯定涉及到文件选择器的使用,所以这里以文件加密器为例。下例为我自己写的一个文件加密器,没什么特别的加密算法,只为演示文件选择器JFileChooser的使用。
加密器界面如图:


项目目录结构如图:

下面贴出各个文件的源代码:
MainForm.java
package com.lidi; import javax.swing.*;
import java.awt.*; public class MainForm extends JFrame { /**
* 构造界面
*
* @author 1109030125
*/
private static final long serialVersionUID = 1L;
/* 主窗体里面的若干元素 */
private JFrame mainForm = new JFrame("TXT文件加密"); // 主窗体,标题为“TXT文件加密”
private JLabel label1 = new JLabel("请选择待加密或解密的文件:");
private JLabel label2 = new JLabel("请选择加密或解密后的文件存放位置:");
public static JTextField sourcefile = new JTextField(); // 选择待加密或解密文件路径的文本域
public static JTextField targetfile = new JTextField(); // 选择加密或解密后文件路径的文本域
public static JButton buttonBrowseSource = new JButton("浏览"); // 浏览按钮
public static JButton buttonBrowseTarget = new JButton("浏览"); // 浏览按钮
public static JButton buttonEncrypt = new JButton("加密"); // 加密按钮
public static JButton buttonDecrypt = new JButton("解密"); // 解密按钮 public MainForm() {
Container container = mainForm.getContentPane(); /* 设置主窗体属性 */
mainForm.setSize(400, 270);// 设置主窗体大小
mainForm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 设置主窗体关闭按钮样式
mainForm.setLocationRelativeTo(null);// 设置居于屏幕中央
mainForm.setResizable(false);// 设置窗口不可缩放
mainForm.setLayout(null);
mainForm.setVisible(true);// 显示窗口 /* 设置各元素位置布局 */
label1.setBounds(30, 10, 300, 30);
sourcefile.setBounds(50, 50, 200, 30);
buttonBrowseSource.setBounds(270, 50, 60, 30);
label2.setBounds(30, 90, 300, 30);
targetfile.setBounds(50, 130, 200, 30);
buttonBrowseTarget.setBounds(270, 130, 60, 30);
buttonEncrypt.setBounds(100, 180, 60, 30);
buttonDecrypt.setBounds(200, 180, 60, 30); /* 为各元素绑定事件监听器 */
buttonBrowseSource.addActionListener(new BrowseAction()); // 为源文件浏览按钮绑定监听器,点击该按钮调用文件选择窗口
buttonBrowseTarget.addActionListener(new BrowseAction()); // 为目标位置浏览按钮绑定监听器,点击该按钮调用文件选择窗口
buttonEncrypt.addActionListener(new EncryptAction()); // 为加密按钮绑定监听器,单击加密按钮会对源文件进行加密并输出到目标位置
buttonDecrypt.addActionListener(new DecryptAction()); // 为解密按钮绑定监听器,单击解密按钮会对源文件进行解密并输出到目标位置
sourcefile.getDocument().addDocumentListener(new TextFieldAction());// 为源文件文本域绑定事件,如果文件是.txt类型,则禁用解密按钮;如果是.kcd文件,则禁用加密按钮。 sourcefile.setEditable(false);// 设置源文件文本域不可手动修改
targetfile.setEditable(false);// 设置目标位置文本域不可手动修改 container.add(label1);
container.add(label2);
container.add(sourcefile);
container.add(targetfile);
container.add(buttonBrowseSource);
container.add(buttonBrowseTarget);
container.add(buttonEncrypt);
container.add(buttonDecrypt);
} public static void main(String args[]) {
new MainForm();
}
}
BrowseAction.java
package com.lidi; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter; public class BrowseAction implements ActionListener { @Override
public void actionPerformed(ActionEvent e) { if (e.getSource().equals(MainForm.buttonBrowseSource)) {
JFileChooser fcDlg = new JFileChooser();
fcDlg.setDialogTitle("请选择待加密或解密的文件...");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"文本文件(*.txt;*.kcd)", "txt", "kcd");
fcDlg.setFileFilter(filter);
int returnVal = fcDlg.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String filepath = fcDlg.getSelectedFile().getPath();
MainForm.sourcefile.setText(filepath);
}
} else if (e.getSource().equals(MainForm.buttonBrowseTarget)) {
JFileChooser fcDlg = new JFileChooser();
fcDlg.setDialogTitle("请选择加密或解密后的文件存放目录");
fcDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fcDlg.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String filepath = fcDlg.getSelectedFile().getPath();
MainForm.targetfile.setText(filepath);
}
}
} }
查看代码
EncryptAction.java
package com.lidi; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import javax.swing.JOptionPane; public class EncryptAction implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub if (MainForm.sourcefile.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择待加密文件!");
} else if (MainForm.targetfile.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择加密后文件存放目录!");
} else {
String sourcepath = MainForm.sourcefile.getText();
String targetpath = MainForm.targetfile.getText();
File file = new File(sourcepath);
String filename = file.getName();
File dir = new File(targetpath);
if (file.exists() && dir.isDirectory()) {
File result = new File(getFinalFile(targetpath, filename));
if (!result.exists()) {
try {
result.createNewFile();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null,
"目标文件创建失败,请检查目录是否为只读!");
}
} try {
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(result);
int ch = 0;
while ((ch = fr.read()) != -1) {
// System.out.print(Encrypt(ch));
fw.write(Encrypt(ch));
}
fw.close();
fr.close();
JOptionPane.showMessageDialog(null, "加密成功!"); } catch (Exception e1) {
JOptionPane.showMessageDialog(null, "未知错误!");
}
} else if (!file.exists()) {
JOptionPane.showMessageDialog(null, "待加密文件不存在!");
} else {
JOptionPane.showMessageDialog(null, "加密后文件存放目录不存在!");
}
}
} public char Encrypt(int ch) {
int x = ch + 1;
return (char) (x);
} public String getFinalFile(String targetpath, String filename) {
int length = filename.length();
String finalFileName = filename.substring(0, length - 4);
String finalFile = targetpath + "\\" + finalFileName + ".kcd";
return finalFile;
} }
查看代码
DecryptAction.java
package com.lidi; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import javax.swing.JOptionPane; public class DecryptAction implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub if (MainForm.sourcefile.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择待解密文件!");
} else if (MainForm.targetfile.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择解密后文件存放目录!");
} else {
String sourcepath = MainForm.sourcefile.getText();
String targetpath = MainForm.targetfile.getText();
File file = new File(sourcepath);
String filename = file.getName();
File dir = new File(targetpath);
if (file.exists() && dir.isDirectory()) {
File result = new File(getFinalFile(targetpath, filename));
if (!result.exists()) {
try {
result.createNewFile();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null,
"目标文件创建失败,请检查目录是否为只读!");
}
} try {
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(result);
int ch = 0;
while ((ch = fr.read()) != -1) {
// System.out.print(Encrypt(ch));
fw.write(Decrypt(ch));
}
fw.close();
fr.close();
JOptionPane.showMessageDialog(null, "解密成功!"); } catch (Exception e1) {
JOptionPane.showMessageDialog(null, "未知错误!");
}
} else if (!file.exists()) {
JOptionPane.showMessageDialog(null, "待解密文件不存在!");
} else {
JOptionPane.showMessageDialog(null, "解密后文件存放目录不存在!");
}
}
} public char Decrypt(int ch) {
// double x = 0 - Math.pow(ch, 2);
int x = ch - 1;
return (char) (x);
} public String getFinalFile(String targetpath, String filename) {
int length = filename.length();
String finalFileName = filename.substring(0, length - 4);
String finalFile = targetpath + "\\" + finalFileName + ".txt";
return finalFile;
} }
查看代码
TextFieldAction.java
package com.lidi; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; public class TextFieldAction implements DocumentListener { @Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub ButtonAjust();
} @Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
ButtonAjust(); } @Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
ButtonAjust(); } public void ButtonAjust() {
String file = MainForm.sourcefile.getText();
if (file.endsWith("txt")) {
MainForm.buttonDecrypt.setEnabled(false);
MainForm.buttonEncrypt.setEnabled(true);
}
if (file.endsWith("kcd")) {
MainForm.buttonEncrypt.setEnabled(false);
MainForm.buttonDecrypt.setEnabled(true);
}
} }
查看代码
Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例的更多相关文章
- 利用JFileChooser实现文件选择对话框
简单的文件选择对话框: package mypackage;/** * 打开文件和存储文件 */import java.awt.BorderLayout;import java.awt.Contain ...
- 文件选择对话框:CFileDialog
程序如下: CString FilePathName; //文件名参数定义 CFileDialog Dlg(TRUE,NULL,NULL, ...
- NX二次开发-UFUN文件选择对话框UF_UI_create_filebox
NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); //文件选择对话框 char sPromptSt ...
- VBScript - 弹出“文件选择对话框”方法大全!
本文记录,VBScript 中,各种打开 "文件选择对话框" 的方法. 实现方法-1 (mshta.exe): 首先,我们要实现的就是,弹出上面的这个"文件选择对话框&q ...
- Java基础知识强化之IO流笔记52:IO流练习之 把一个文件中的字符串排序后再写入另一个文件案例
1. 把一个文件中的字符串排序后再写入另一个文件 已知s.txt文件中有这样的一个字符串:"hcexfgijkamdnoqrzstuvwybpl" 请编写程序读取数据内容,把数据排 ...
- Shell 从日志文件中选择时间段内的日志输出到另一个文件
Shell 从日志文件中选择时间段内的日志输出到另一个文件 情况是这样的,某系统的日志全部写在一个日志文件内,所以这个文件非常大,非常长,每次查阅的时候非常的不方便.所以,相关人员希望能够查询某个时间 ...
- Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹
Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹 1.将一个文件夹下的所有内容复制到另一个文件夹下 cp -r /home/packageA/* /home/cp/packageB ...
- Java Swing提供的文件选择对话框 - JFileChooser
JFileChooser() 构造一个指向用户默认目录的 JFileChooser. JFileChooser(File currentDirectory) 使 ...
- SWT的文件选择对话框I的使用
swt文件选择框 FileDialog fileselect=new FileDialog(shell,SWT.SINGLE); fileselect ...
随机推荐
- mysql 5.7.15 vs mysql 5.6.31性能测试以及不同linux内核性能比较
最近,将部分开发和测试环境的mysql升级到5.7之后,今天抽时间测试了下5.6和5.7 PK查询的性能,使用mysqlslap进行测试,测试结果发现在低配下,percona 5.6.31大约比5.7 ...
- 不可小觑的SQL语句
在前面学的我们通过点鼠标给数据表插数据,虽然这种方法很靠谱,但是有那么的一些缺点,就是比较麻烦和效率不高.所以现在我们的好好学SQL语句,来弥补这么的一个漏洞,能提高我们工作的效率. SQL语句能做什 ...
- jQuery超酷下拉插件6种效果演示
原始的下拉框很丑啦, 给大家一款jQuery超酷下拉插件6种效果 效果预览 下载地址 实例代码 <div class="container"> <section ...
- 【javascript激增的思考03】MVVM与Knockout
前言 今天搞的有点快,因为上午简单研究了下MVC,发现MVC不太适合前端开发,然后之前看几位前端前辈都推荐前端使用MVVM,但是我对其还不甚了解,所以我觉得下午还是应该先看看他是神马先,后面再决定要不 ...
- 尝试加载 Oracle 客户端库时引发 BadImageFormatException。问题记录
电脑是win8 64位,安装oracle 11g r2 64位的,谁知道一切装完毕后,打开项目却连不上oracle数据了...首先是pl/sql连不上,装了oracle服务器,应该是不用再装客户端,p ...
- 总结CSS3新特性(选择器篇)
CSS3新增了嗯- -21个选择器,脚本通过控制台在这里运行; ~: p ~ p { color : red;/*此条规则将用于p后边所有的p...就是除了第一个p的所有p,规则同p:not(:nth ...
- SrsDataConnector The SQL Server Reporting Services account is a local user and is not supported.
这次使用OS+SQL的镜像还原系统后安装了CRM 2015,主要流程是 安装IIS/AD,SSRS ,CRM2015.自带的SQL中SSRS没有安装完全,需配置一下. 这一切都满顺利的,最后在安装 S ...
- SharePoint 2010升级到sharePoint 2013后,人员失去对网站的权限的原因及解决方法。The reason and solution for permission lost after the upgrading
昨天碰到了一个问题,一个网站在从SharePoint 2010升级到SharePoint 2013后,人员都不能登录了,必须重加赋权,人员才能登录,这样非常麻烦. 原因:是认证方式的问题.在Share ...
- SqlIte数据库并发性
把遇到的一些小问题都记下来,告诉自己,一些小细节会铸成打错的 今天没事复习以前的知识,用sqlite做数据库,发现修改数据的时候等好久才有反应,而且还失败,可是过一会之后又会好,好了以后又是一样,种以 ...
- Activity源码简要分析总结
Activity源码简要分析总结 摘自参考书籍,只列一下结论: 1. Activity的顶层View是DecorView,而我们在onCreate()方法中通过setContentView()设置的V ...