java读取加密的文件_Java 文件读写,读文件后加密写入输出文件 | 学步园

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

class Cipher3{

public

static int initial = 3;

String Plain = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

String Cipher;

Cipher3()

{

generateCipher();

}

int getini()

{

return this.initial;

}

void generateCipher()

{

this.Cipher = this.Plain.substring(this.initial);

this.Cipher = this.Cipher + this.Plain.substring(0, this.initial - 1);

}

// for eclipse default folder : File file = new File("src/accounts.txt");

void encode( String input, String output ) throws IOException

{

// read io

String line;

File file = new File(input);

FileReader fr = new FileReader(file);

BufferedReader inFile = new BufferedReader(fr);

line = inFile.readLine();

// write io

File outf = new File(output);

if (outf.exists()) {

//   System.out.println("file exit");

}

else {

//   System.out.println("file does not exit, creating");

if (outf.createNewFile()) {

//    System.out.println("succeed in creating");

}

else {

System.out.println("fail to create file");

}

}

BufferedWriter outFile = new BufferedWriter(new FileWriter(outf));

while (line != null)

{

// deal with line

String outstr = "";

for ( int i = 0; i < line.length(); i ++ )

{

String temp = line.substring(i, i+1);

int index = this.Plain.indexOf( temp );

if (index != -1)

{

outstr += this.Cipher.substring(index, index + 1);

}

else

{

outstr += temp;

}

}

outFile.write(outstr);

outFile.newLine();

line = inFile.readLine();

}

// read io close

inFile.close();

// write io close

outFile.close();

}

void decode( String input, String output ) throws IOException

{

// read io

String line;

File file = new File(input);

FileReader fr = new FileReader(file);

BufferedReader inFile = new BufferedReader(fr);

line = inFile.readLine();

// write io

File outf = new File(output);

if (outf.exists()) {

//   System.out.println("file exit");

}

else {

//   System.out.println("file does not exit, creating");

if (outf.createNewFile()) {

//    System.out.println("succeed in creating");

}

else {

System.out.println("fail to create file");

}

}

BufferedWriter outFile = new BufferedWriter(new FileWriter(outf));

while (line != null)

{

// deal with line

String outstr = "";

for ( int i = 0; i < line.length(); i ++ )

{

String temp = line.substring(i, i+1);

int index = this.Cipher.indexOf( temp );

if (index != -1)

{

outstr += this.Plain.substring(index, index + 1);

}

else

{

outstr += temp;

}

}

outFile.write(outstr);

outFile.newLine();

line = inFile.readLine();

}

// read io close

inFile.close();

// write io close

outFile.close();

}

}

参考: http://sharewind.javaeye.com/blog/227538

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值