日期:2014-05-20 浏览次数:20784 次
import java.io.*; import java.awt.*; import java.awt.event.*; public class Example10_6{ public static void main(String args[]){ char a[]="今晚10点进攻".toCharArray(); int n=0,m=0; try{ File f=new File("secert.txt"); for(int i=0;i<a.length;i++){ a[i]=(char)(a[i]^'R'); } FileWriter out=new FileWriter(f); out.write(a,0,a.length); out.close(); FileReader in=new FileReader(f); char tom[]=new char[10]; System.out.println("密文:"); while((n=in.read(tom,0,10))!=-1){ String s=new String(tom,0,n); System.out.print(s); } in.close(); in=new FileReader(f); System.out.println(""); System.out.println("明文"); while((n=in.read(tom,0,10))!=-1){ for(int i=0;i<n;i++){ tom[i]=(char)(tom[i]^'R'); } String s=new String (tom,0,n); System.out.print(s); } in.close(); } catch(IOException e){ System.out.println("File read Error"); } } }
package com.design.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class test { public static void main(String args[]) { byte[] a="今晚10点进攻".getBytes(); //char a[] = "今晚10点进攻".toCharArray(); int n = 0, m = 0; try { File f = new File("D://secert.txt"); for (int i = 0; i < a.length; i++) { a[i] = (byte) (a[i] ^ 'R'); } FileOutputStream out=new FileOutputStream(f); //FileWriter out = new FileWriter(f); out.write(a, 0, a.length); out.close(); FileInputStream in=new FileInputStream(f); // FileReader in = new FileReader(f); //char tom[] = new char[10]; byte tom[]=new byte[1024]; System.out.println("密文:"); while ((n = in.read(tom, 0, 10)) != -1) { String s = new String(tom, 0, n); System.out.print(s); } in.close(); //in = new FileReader(f); in=new FileInputStream(f); System.out.println(""); System.out.println("明文"); while ((n = in.read(tom, 0, 10)) != -1) { for (int i = 0; i < n; i++) { tom[i] = (byte) (tom[i] ^ 'R'); } String s = new String(tom, 0, n); System.out.print(s); } in.close(); } catch (IOException e) { System.out.println("File read Error"); } } }