昨天写了一个非常简单的文件加密解密的代码,不带界面,也没有涉及算法。输入文件的时候记得输入名称的时候类似:E://1.txt这样才行。RandomAccessFile类是一个可读也可写的流! import java.io.*; public class EncryptionDecryption { static File sourceFile; public void encryption(int code) throws IOException { RandomAccessFile fra = new RandomAccessFile(sourceFile, "rw"); int a; while ((a = fra.read()) != -1) { fra.seek(fra.getFilePointer() - 1); a = a + code; fra.write(a); } } public void decryption(int code) throws IOException { RandomAccessFile fra = new RandomAccessFile(sourceFile, "rw"); int a; while ((a = fra.read()) != -1) { fra.seek(fra.getFilePointer() - 1); a = a - code; fra.write(a); } } public static void main(String args[]) throws IOException { System.out.println("welcome to the encryption and decryption function"); System.out.println("please input the filename"); DataInputStream ins = new DataInputStream(System.in); String fileName = ins.readLine(); sourceFile = new File(fileName); System.out.println("please choose :"); System.out.println("1 encryption"); System.out.println("2 decryption"); String choice = ins.readLine(); System.out.println("please input operate code from 1 to 10"); int code = Integer.parseInt(ins.readLine()); EncryptionDecryption ed = new EncryptionDecryption(); if (choice.equals("1")) { ed.decryption(code); } else if (choice.equals("2")) { ed.encryption(code); } else { System.out.println("your input is wrong"); } } }