作业帮 > 综合 > 作业

编写一个Java应用程序,实现将一个由英文字母、数字及其他符号构成的文件(a.txt)通过加密之后写入另外

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/10/04 13:10:38
编写一个Java应用程序,实现将一个由英文字母、数字及其他符号构成的文件(a.txt)通过加密之后写入另外
个文件(b.txt)中.
加密算法为:如果是英文字母,则采用字母+1% 26的方式转换;如果是数字则采用(1234567890)的倒数的方式转换,除此之外的符号原样输出.
例如:源文件内的内容为ah0123,:Mz,则目标文件内容为bi1098,:Na
做回好人,回答你吧.直接看代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class jiami {
public static void main(String[] args) {
String source=null,target=null;
try {
FileInputStream fileread = new FileInputStream(new File("D:/a.txt"));//路径自己改
int length = fileread.available();
byte[] buffer = new byte[length];
fileread.read(buffer);
source = new String(buffer);//读取
fileread.close();
} catch (Exception e) {
e.printStackTrace();
}
if(source==null)
System.out.println("a.txt为空");
else{
System.out.println(source);
target=zhuanhuan(source);
System.out.println(target);
try {
FileOutputStream out = new FileOutputStream(new File("D:/b.txt"));
out.write(target.getBytes());//写入
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String zhuanhuan(String s){
char []array = s.toCharArray();
for(int i=0;i=65&&j=97&&j