需求:编写一个加密程序,对用户输入的字符串加密后输出。
加密方法是将每个字符映射成字母表中的对称字符。
例如a对应z,b对应y,字符串“Java”加密后就成为“Qzez”
思路:
①创建字节输出流将数据写入文件中
②创建字节输入流将读取文件数据并输出
④加密文件内容并输出
⑤释放资源
package com.daiziru.ioc.beans;
import java.io.*;
import java.util.Scanner;
public class Filea {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
FileOutputStream fos = new FileOutputStream("D:\\AllFile\\input.txt",true);//追加写入
String str = sc.nextLine();
fos.write(str.getBytes());
File file = new File("D:\\AllFile\\input.txt");
FileInputStream fis = new FileInputStream(file);
int read = fis.read();//返回第一个字节 保存到read里
while(read != -1){
if(read <65 || read >90 && read <97 || read > 122)
System.out.println("Error: ");
if(read >= 65 && read <= 90)
read = 155-read;
if(read >= 97 && read<= 122)
read= 219-read;
System.out.print((char)read);
read = fis.read();//当未读取到文件末尾时 继续读取下一个字节
}
fos.close();
fis.close();
}
}
参考:https://blog.csdn.net/qq_38225558/article/details/82494656?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163525546516780274144699%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=163525546516780274144699&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_v2~rank_v29-1-82494656.pc_v2_rank_blog_default&utm_term=FileInputStream&spm=1018.2226.3001.4450