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 ...
随机推荐
- Openfire重新安装
由于忘记密码等原因,无法启动openfire ,那就重装吧,废话不多说,命令行按步骤粘贴执行就好了. 1.先删除mysql数据 mysql -u root -p 输入密码:例如,123456 show ...
- Android开发中Eclispe相关问题及相应解决(持续更新)
1.Eclipse项目中的Android Private Libraries没有自动生成. 一般而言,在Android开发中,项目中引用到的jar包会放到项目目录中的libs中,引入库会放到Andro ...
- static关键字详解
首先,要了解一下这些东西的存放位置 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm只有一个堆区(heap)被所有线程共享,堆 ...
- .NET DLR 上的IronScheme 语言互操作&&IronScheme控制台输入中文的问题
前言 一直以来对Lisp语言怀有很崇敬的心里,<黑客与画家>对Lisp更是推崇备至,虽然看了不少有关Lisp的介绍但都没有机会去写段程序试试,就像我对C++一样,多少有点敬畏.这个周末花了 ...
- [iOS] 建立与使用Framework
[iOS] 建立与使用Framework 前言 使用XCode开发iOS项目时,开发人员可以将可重用的程序代码,封装为Library或是Framework来提供其他开发人员使用.这两种封装方式在使用的 ...
- cssSlidy.js 响应式手机图片轮播
cssSlidy是一款支持手机移动端的焦点图轮播插件,支持标题设置,滑动动画,间隔时间等. 在线实例 实例演示 使用方法 <div id="slidy-container"& ...
- Ratatype - 在线打字教程,提高打字速度
Ratatype 是一个在线的打字教程网站,帮助人们提高键盘输入速度.开始掌握你的技能,挑战你的朋友或得到一个打字的证书.如果打字慢会浪费你宝贵的时间.如果你的打字速度提高30%,您可以每天节省20分 ...
- CSS中的margin、border、padding区别
CSS padding margin border属性详解 图解CSS padding.margin.border属性W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来 ...
- Android引用本地aar
先建立一个lib工程,然后build出aar. 接着把aar放入要引入它的工程module的libs中. 在project的build.gradle中: repositories { flatDir ...
- git 新建服务器的版本以及项目的用户
一, git客户端账号生成 1. git的客户端的公钥生成 ssh-keygen -t rsa -C "test@gmail.com" mac机器会在 /Users/用户/.ssh ...