Reader字符输入流
Writer字符输出流
专门用于字符的形式读取和写入数据
1,使用字符流读取文件
FileReader 是Reader子类,以FileReader 为例进行文件读取
// FileReader 继承 InputStreamReader
public class FileReader extends InputStreamReader{}
// InputStreamReader 继承 Reader
public class InputStreamReader extends Reader{}
public class AtWill {
public static void main(String[] args) {
// 准备文件lol.txt其中的内容是AB
File f = new File("H:\\码云project\\gogo\\lala2.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) {
e.printStackTrace();
}
}
}
2,使用字符流把字符串写入到文件
FileWriter 是Writer的子类,以FileWriter 为例把字符串写入到文件
// FileWriter 继承 OutputStreamWriter
public class FileWriter extends OutputStreamWriter{}
// OutputStreamWriter 继承 Writer
public class OutputStreamWriter extends Writer{}
public class AtWill {
public static void main(String[] args) {
// 准备文件lol2.txt
File f = new File("H:\\码云project\\gogo\\lala2.txt");
// 创建基于文件的Writer
try (FileWriter fr = new FileWriter(f)) {
// 以字符流的形式把数据写入到文件中
String data="abcdefg1234567890";
char[] cs = data.toCharArray();
fr.write(cs);
} catch (IOException e) {
e.printStackTrace();
}
}
}
3,练习-文件加密,解密
public class AtWill {
public static void encodeFile(File encodingFile, File encodedFile) throws IOException {
FileReader fr = new FileReader(encodingFile);
FileWriter fw = new FileWriter(encodedFile);
try {
char[] all = new char[(int) encodingFile.length()];
char[] bll = new char[all.length];
fr.read(all);
for (int i = 0; i < all.length; i++) {
bll[i] = all[i];
if ((all[i] >= '0' && all[i]< '9') || (all[i] >= 'a' && all[i] < 'z') || (all[i] >= 'A' && all[i] < 'Z')) {
bll[i] = (char) (bll[i] + 1);
} else if (all[i] == '9') {
bll[i] = '0';
} else if (all[i] == 'Z') {
bll[i] = 'A';
} else if (all[i] == 'z') {
bll[i] = 'a';
}
}
fw.write(bll);
} catch (IOException e) {
e.printStackTrace();
} finally {
fr.close();
fw.close();
}
}
public static void Decryption(File encodedFile,File encodingFile) {
File f = new File(encodedFile.getAbsolutePath());
if(!encodingFile.exists()) {
encodingFile.canExecute();
}
char[] c = new char[(int)encodedFile.length()];
try (FileReader fr = new FileReader(f)){
fr.read(c);
for (int i = 0; i < c.length; i++) {
if(c[i]>'0'&c[i]<='9'|c[i]>'a'&c[i]<='z'|c[i]>'A'&c[i]<='Z') {
c[i]=(char)(c[i]-1);
}
else if(c[i]=='0') {
c[i]='9';
}
else if(c[i]=='A') {
c[i]='Z';
}
else if(c[i]=='a') {
c[i]='z';
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f1 =new File(encodingFile.getAbsolutePath());
try(FileWriter fw = new FileWriter(f1)){
fw.write(c);
}catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
File f1 = new File("H:\\码云project\\gogo\\lala6.txt");
File f2 = new File("H:\\码云project\\gogo\\lala8.txt");
// try {
// encodeFile(f1, f2);
// } catch (IOException e) {
// e.printStackTrace();
// }
Decryption(f1, f2);
}
}
参考来源于:
http://how2j.cn/k/io/io-characterstream/341.html?p=48616