日期:2014-05-20  浏览次数:20937 次

请问java中如何读取汉字
做这么样一个程序,
读取一段中文文字,然后使用某种算法,把这段文字中的每个字的次序打乱,也就是让别人很难读懂,然后文本输出;
大侠帮忙啊,实在没有分啊

------解决方案--------------------
package com.java.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Shuffle {

public static void main(String[] args) {
try {
readFile( "c:\\ ", "1.txt ");
} catch (Exception e) {
e.printStackTrace();
}
}

public static String Shuffle(String test) {
int length = test.length();
StringBuffer sb = new StringBuffer();
boolean[] cards = new boolean[length];
for (int index = 0; index < length; index++) {
cards[index] = false;
}
java.util.Random r = new java.util.Random();
int x;
for (int index = 0; index < length; index++) {
do {
x = java.lang.Math.abs(r.nextInt()) % length;
} while (cards[x] != false);
cards[x] = true;
sb.append(test.substring(x, x + 1));
}
return sb.toString();
}

/**
* 创建文件
*/
public static void readFile(String _path, String _fileName)
throws IOException {
String readline = null;
boolean isLast = false;
try {
File file = new File(_path + File.separator + _fileName, " ");
if (!file.exists()) {
System.out.println( "文件不存在! ");
}
BufferedReader read = new BufferedReader(new FileReader(_path
+ File.separator + _fileName));
while ((readline = (read.readLine())) != null) {
isLast = true;
writeFile( "c:\\ ", "2.txt ", Shuffle(readline), isLast);
}
System.out.println( "字符打乱成功! ");

} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 向文件中写入数据
*/
public static void writeFile(String _path, String _fileName,
String insertString, boolean isLast) throws IOException {

try {
BufferedWriter out = new BufferedWriter(new FileWriter(_path
+ File.separator + _fileName, true));
out.write(insertString);
if (isLast) {
out.newLine();
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}