求一道简单的JAVA程序,请高手帮帮忙....谢谢

用java 写一个程序,提供菜单功能。可以将键盘输入的指定文件名的文件加密解密。加密的方法是将每个字符+1。
请问这个程序应该怎么写?????
谢谢,请高手帮帮忙.
写好的程序请在必要关键的地方做出注释
要求菜单功能是在黑屏上显示的
1。加密
2. 解密
3。退出
当输入某个数字的时候,进行相应的操作

------------Encryption.java---------------

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class Encryption {

/**
* 显示出该路径下所有文件名
*
* @param path
*/
public static void listFiles(String path) {
File f = new File(path);
if (!f.exists()) {
// 如果文件夹不存在,创建文件夹
f.mkdir();
return;
}

File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
// 列出该文件夹下有效文件,文件夹除外
System.out.println(files[i].getName());
}
}
}

/**
* 为文件加密
*
* @param path
* @param file
* @throws IOException
*/
public static void encrypt(String path, String file) throws IOException {
File f = new File(path, file);
// 如果输入的文件不存在,或者输入的为文件夹,则不进入下面的流程。
if (!f.exists() || f.isDirectory()) {
System.out.println("文件不存在,或者不可读.");
return;
}
InputStream input = new FileInputStream(f);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
// 每次对一行数据操作
String value = reader.readLine();
while (value != null) {
char[] ch = value.toCharArray();
for (int i = 0; i < ch.length; i++) {
ch[i] = (char) (ch[i] + 1);
}
buffer.append(new String(ch) + "\r\n");
value = reader.readLine();
}
// 去掉多余的换行符
buffer.replace(buffer.lastIndexOf("\r\n"),buffer.length(),"");
System.out.println("----------加密完成-----------");
// 将加密后文件写入加密文件夹,名字默认为源文件文件名
OutputStream output = new FileOutputStream(f);
output.write(buffer.toString().getBytes());

output.close();
reader.close();
input.close();
}

/**
* 读取加密文件
*
* @param path
* @param file
* @throws IOException
*/
public static void decrypt(String path, String file) throws IOException {
File f = new File(path, file);
if (!f.exists() || f.isDirectory()) {
System.out.println("文件不存在,或者不可读.");
return;
}
InputStream input = new FileInputStream(f);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String value = reader.readLine();
while (value != null) {
char[] ch = value.toCharArray();
for (int i = 0; i < ch.length; i++) {
ch[i] = (char) (ch[i] - 1);
}
buffer.append(new String(ch) + "\r\n");
value = reader.readLine();
}
// 因为多加了一个换行符,需要去掉。
buffer.replace(buffer.lastIndexOf("\r\n"),buffer.length(),"");
OutputStream output = new FileOutputStream(f);
output.write(buffer.toString().getBytes());
System.out.println("-------------解密完成----------------");
output.close();
reader.close();
input.close();
}
}

--------------EncryptMain.java-----------------

import java.io.IOException;
import java.util.Scanner;

public class EncryptMain {

public static void main(String args[]) throws IOException {
Scanner s = new Scanner(System.in);
String operation = "";
String opition = "";
while (true) {
System.out.println("-----------------文件加密,解密------------------");
System.out.println("1.加密 2.解密 3.退出");
int op = 0;
try {
opition = s.nextLine();
op = Integer.parseInt(opition);
} catch (Exception e) {
System.out.println("必须在1,2,3之中选择,请选择:");
opition = s.nextLine();
op = Integer.parseInt(opition);
}
switch (op) {
case 1:
do {
System.out.println("输入文件路径:");
String path = s.nextLine();
// 列出该文件夹下的文件名
Encryption.listFiles(path);
System.out.println("输入加密的文件名:");
String name = s.nextLine();
// 加密文件
Encryption.encrypt(path, name);
System.out.println("继续加密?(y/n)");
operation = s.nextLine();

} while (operation.equalsIgnoreCase("y"));
break;
case 2:
do {
System.out.println("输入文件路径:");
String path = s.nextLine();
// 列出该文件夹下的文件名
Encryption.listFiles(path);
System.out.println("输入解密的文件名:");
String name = s.nextLine();
Encryption.decrypt(path, name);
System.out.println("继续解密?(y/n)");
operation = s.nextLine();
} while (operation.equalsIgnoreCase("y"));
break;
case 3:
System.exit(0);
default:
System.out.println("无此选项,重新选择");
break;
}
}
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2007-11-30
每个字符加密.....应该是换成ASC2码+1吧
你这个东西不好搞哦
第2个回答  2007-11-30
输入文件全路径,
会在此文件所在目录下生成一个result.txt文件,
目前我所知道只能操作.txt文件成功,
其他没试过
import java.io.*;
import java.util.*;
public class acm_problem {
private String fileName;
private File file;
public acm_problem() {
getInputFileName();
checkFile();
manageData();
}
public void getInputFileName(){
System.out.println("请输入要进行计算的文件路径:");
Scanner s = new Scanner(new BufferedInputStream(System.in));
fileName = s.nextLine();
System.out.println(fileName);
s.close();

}
public void checkFile(){
file = new File(fileName);
if(!file.exists())
{
System.out.println("文件不存在!退出...");
System.exit(0);
}
if(!file.canExecute())
{
System.out.println("不能处理文件!退出...");
System.exit(0);
}
}
public void manageData(){
System.out.println("处理数据中...");
try{
BufferedReader br = new BufferedReader(new FileReader(file));
String s1 = file.getParent();
s1 = s1+"result.txt";
//System.out.println(s1);
File file1 = new File(s1);
BufferedWriter bw = new BufferedWriter(
new FileWriter(file1));
String s = null;
StringBuffer sb = null;
while( ( s = br.readLine())!= null){
sb = new StringBuffer(s);
for(int i = 0; i< sb.length();i++)
sb.setCharAt(i,(char)((int)sb.charAt(i)+1));
bw.write(new String(sb));
bw.newLine();

}
br.close();
bw.close();
System.out.println("数据处理完毕");
}catch(Exception e){
e.printStackTrace();

}

}
}
相似回答