Java_I/O输入输出_使用输入输出流读取文件,将一段文字加密后存入文件,然后读取,将加密前与后的文件输出


import java.io.*;

public class Example {
public static void main(String[] args) {
char a[] = "今天10点出发".toCharArray();
int n = 0;
try {
File out = new File("word.txt");
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 'R');
}
FileWriter fw = new FileWriter(out);
fw.write(a, 0, a.length);
fw.close();
FileReader fr = new FileReader(out);
char tom[] = new char[10];
System.out.println("加密后:");
while ((n = fr.read(tom, 0, 10)) != -1) {
String s = new String(tom, 0, n);
System.out.println(s);
}
fr.close();
fr = new FileReader(out);
System.out.println("明文:");
while ((n = fr.read(tom, 0, 10)) != -1) {
for (int j = 0; j < n; j++) {
tom[j] = (char) (tom[j] ^ 'R');
}
String str = new String(tom, 0, n);
System.out.println(str);
}

fr.close();
} catch (Exception e) {
e.printStackTrace();
}

}

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM