FileReader类和FileWriter类的基本用法示例
package com.example.io; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class NewClass4 { public static void main(String[] args) { char a[] = "今晚10点发动总攻".toCharArray(); int n; try { File file = new File("d:\\java", "aa.java"); for (int i = 0; i < a.length; i++) { a[i] = (char) (a[i] ^ 'R'); } FileWriter out = new FileWriter(file); out.write(a, 0, a.length); out.close(); FileReader in = new FileReader(file); char[] buf = new char[10]; System.out.println("密文:"); while ((n = in.read(buf, 0, 10))!= -1) { // n 为实际读取的字符数 String s = new String(buf, 0, n); System.out.println(s); } in.close(); in = new FileReader(file); System.out.println("明文:"); while((n = in.read(buf, 0, 10))!= -1) { for (int i = 0; i < n; i++) { buf[i] = (char) (buf[i] ^ 'R'); } String str = new String(buf, 0, n); System.out.println(str); } in.close(); } catch (IOException e) { System.out.println(e); } } }