1、首先创建密文,内容为:明天。保存在d盘,文件路径d:\mima.txt.
2、代码如下:
import java.io.*;
public class encrypt {
public static void main(String[] args) {
File f = new File("d:/mima.txt");//根据文件夹路径创建File文件类的实例对象
char a[ ] = new char[3];int i=0;
try {
FileReader b = new FileReader(f);
System.out.println("密文:"); while ((i= b.read(a, 0, a.length)) != -1) //Reader类的read(char[],int off,int len)方法
{for ( i = 0; i < a.length-1; i++) {
a[i] = (char) (a[i] ^ 'A');}//利用异或运算进行加密 String s = new String(a, 0, i);//String(char[],int startIndex,int intCount)提取数组a中的字符创建字符串对象
System.out.println(s);//输出密文}
b.close();//关闭流
}
catch (IOException e) {
System.out.println(e);
}try {
FileReader b1 = new FileReader(f);
System.out.println("明文:");
while ((i= b1.read(a, 0, a.length)) != -1) {
String s = new String(a, 0, i);
System.out.println(s);}
b1.close();
}catch (IOException e) {
System.out.println(e);
}
}
}
3、运行结果: