package file;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.IOException;
public class Encoder {
public static void main(String[] args){
File f1 = new File("D:/AA/A/test4.txt");
File f2 = new File("D:/AA/A/test5.txt");
File f3 = new File("D:/AA/A/test6.txt");
encodeFile(f1,f2);
decodeFile(f2,f3);
}
public static void encodeFile(File encodingFile,File encodedFile){
try(FileReader fr =new FileReader(encodingFile)){
char all[] = new char[(int) encodingFile.length()];
fr.read(all);
for(char b:all)
{
System.out.print(b);
}
System.out.println();
for(int i=0;i<all.length;i++){
if(Character.isDigit(all[i])){
if(all[i]=='9'){
all[i]='0';
}
else{
all[i] = (char) (all[i]+1);
}
}
else if(Character.isLetter(all[i])){
if(all[i]=='z'|all[i]=='Z'){
all[i]=(char)(all[i]-25);
}
else{
all[i]=(char)(all[i]+1);
}
}
else if(!Character.isLetter(all[i])){
// all[i]=(char)all[i];
continue;
}
}
for(char b : all){
System.out.print(b);
}
System.out.println();
try(FileWriter fw = new FileWriter(encodedFile)){
fw.write(all);
}catch(IOException e){
e.printStackTrace();
}
}catch(IOException e)
{
e.printStackTrace();
}
}
public static void decodeFile(File decodingFile, File decodedFile){
try(FileReader fr =new FileReader(decodingFile)){
char all[] = new char[(int) decodingFile.length()];
fr.read(all);
for(char b:all)
{
System.out.print(b);
}
System.out.println();
for(int i=0;i<all.length;i++){
if(Character.isDigit(all[i])){
if(all[i]=='0'){
all[i]='9';
}
else{
all[i] = (char) (all[i]-1);
}
}
else if(Character.isLetter(all[i])){
if(all[i]=='a'|all[i]=='A'){
all[i]=(char)(all[i]+25);
}
else{
all[i]=(char)(all[i]-1);
}
}
else if(!Character.isLetter(all[i])){
all[i]=(char)all[i];
}
}
for(char b : all){
System.out.print(b);
}
try(FileWriter fw = new FileWriter(decodedFile)){
fw.write(all);
}catch(IOException e){
e.printStackTrace();
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
好像文字有点问题