Java character stream Reader Writer
Reader character input stream
Writer character output stream
dedicated to the character in the form of read and write data
Step 1: Use character stream to read the file
FileReader is a subclass of Reader to read the file FileReader example.
package stream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class TestStream {
public static void main(String[] args) {
// 准备文件lol.txt其中的内容是AB
File f = new File("d:/lol.txt");
// 创建基于文件的Reader
try (FileReader fr = new FileReader(f)) {
// 创建字符数组,其长度就是文件的长度
char[] all = new char[(int) f.length()];
// 以字符流的形式读取文件所有内容
fr.read(all);
for (char b : all) {
// 打印出来是A B
System.out.println(b);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Step 2: Use a character string is written to the stream file
Writer FileWriter is a subclass to Example FileWriter writes a string to a file
package stream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class TestStream {
public static void main(String[] args) {
// 准备文件lol2.txt
File f = new File("d:/lol2.txt");
// 创建基于文件的Writer
try (FileWriter fr = new FileWriter(f)) {
// 以字符流的形式把数据写入到文件中
String data="abcdefg1234567890";
char[] cs = data.toCharArray();
fr.write(cs);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Exercise : File Encryption
Prepare a text file (non-binary), which contains the ASCII characters and Chinese characters.
A design method
public static void encodeFile(File encodingFile, File encodedFile);
In this method the encodingFile encrypt the contents, and then save the file to encodedFile.
Encryption Algorithm:
Digital:
If the number is not 9, adding 1 to the original basis, such as into 6 5, 3 becomes 4
if numbers 9, becomes 0
alphabetic characters:
if the character is non-z, a rightward such as d into e, G to H
if z, z-> a, ZA.
Characters need to keep sensitive
non-alphanumeric characters:
for example, '& ^ remain unchanged, Chinese also remain unchanged
answer:
package stream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestStream {
/**
*
* @param encodingFile
* 被加密的文件
* @param encodedFile
* 加密后保存的位置
*/
public static void encodeFile(File encodingFile, File encodedFile) {
try (FileReader fr = new FileReader(encodingFile); FileWriter fw = new FileWriter(encodedFile)) {
// 读取源文件
char[] fileContent = new char[(int) encodingFile.length()];
fr.read(fileContent);
System.out.println("加密前的内容:");
System.out.println(new String(fileContent));
// 进行加密
encode(fileContent);
// 把加密后的内容保存到目标文件
System.out.println("加密后的内容:");
System.out.println(new String(fileContent));
fw.write(fileContent);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void encode(char[] fileContent) {
for (int i = 0; i < fileContent.length; i++) {
char c = fileContent[i];
if (isLetterOrDigit(c)) {
switch (c) {
case '9':
c = '0';
break;
case 'z':
c = 'a';
break;
case 'Z':
c = 'A';
break;
default:
c++;
break;
}
}
fileContent[i] = c;
}
}
public static boolean isLetterOrDigit(char c) {
// 不使用Character类的isLetterOrDigit方法是因为,中文也会被判断为字母
String letterOrDigital = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
return -1 == letterOrDigital.indexOf(c) ? false : true;
}
public static void main(String[] args) {
File encodingFile = new File("E:/project/j2se/src/Test1.txt");
File encodedFile = new File("E:/project/j2se/src/Test2.txt");
encodeFile(encodingFile, encodedFile);
}
}