首页 > 技术文章 > java-字符流

wangqianming12138 2021-02-08 16:56 原文

前言

昨天学习了java的字节流,可以用字节流来读写文件。但是读取和写入的内容并不是内容本身,而是其对应的ASCII码。今天学习的字符流就可以规避这个问题,将内容以字符的形式进行读写。


使用字符流读取文件

 1 package IO流;
 2 
 3 import java.io.FileReader;
 4 import java.io.File;
 5 import java.io.IOException;
 6 
 7 public class 字符流读取文件 {
 8     public static void main(String[] args) {
 9         File f=new File("D://1.txt");
10         try(FileReader fr=new FileReader(f)){//运用上午所学,通过try()中引用流实现流的自动关闭
11             char[] words=new char[(int) f.length()];//新建一个字符数组,用于存储读取的字符
12             fr.read(words);//读取操作
13             for(char w:words)
14             {
15                 System.out.println(w);
16             }
17         }catch(IOException e){
18             e.printStackTrace();
19         }
20     }
21 }

使用字符流把字符串写进文件

 1 package IO流;
 2 
 3 import java.io.File;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 
 7 public class 字符流写入文件 {
 8 
 9     public static void main(String[] args) {
10         File f=new File("d://1.txt");
11         try(FileWriter fr=new FileWriter(f)){
12             char[] words= {'a','b','c','d','e','f','g'};
13             fr.write(words);
14         }catch(IOException e){
15             e.printStackTrace();
16         }
17     }
18 }

 


练习题

准备一个文本文件(非二进制),其中包含ASCII码的字符和中文字符。
设计一个方法

 
public static void encodeFile(File encodingFile, File encodedFile);
 


在这个方法中把encodingFile的内容进行加密,然后保存到encodedFile文件中。
加密算法:
数字:
如果不是9的数字,在原来的基础上加1,比如5变成6, 3变成4
如果是9的数字,变成0
字母字符:
如果是非z字符,向右移动一个,比如d变成e, G变成H
如果是z,z->a, Z-A。
字符需要保留大小写
非字母字符
比如',&^ 保留不变,中文也保留不变

 1 package IO流;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.io.FileReader;
 6 import java.io.FileWriter;
 7 
 8 public class 加密文件 {
 9     public static void encodeFile(File encodingFile, File encodedFile)
10     {
11         FileReader fr=null;
12         FileWriter fw=null;
13         try{
14             fr=new FileReader(encodingFile);
15             fw=new FileWriter(encodedFile);
16             char[] words=new char[(int)encodingFile.length()];
17             char[] newwords=new char[(int)encodingFile.length()];
18             fr.read(words);
19             System.out.println(new String(words));
20             for(int i=0;i<words.length;i++)
21             {
22                 if(words[i]>='0'&&words[i]<'9')
23                 {
24                     newwords[i]=(char)(words[i]+1);
25                 }
26                 else if(words[i]=='9')
27                 {
28                     newwords[i]='0';
29                 }
30                 else if((words[i]>='a'&&words[i]<'z')||(words[i]>='A'&&words[i]<'Z'))
31                 {
32                     newwords[i]=(char)(words[i]+1);
33                 }
34                 else if(words[i]=='z')
35                 {
36                     newwords[i]='a';
37                 }
38                 else if(words[i]=='Z')
39                 {
40                     newwords[i]='A';
41                 }
42                 else
43                 {
44                     newwords[i]=words[i];
45                 }
46             }
47             fw.write(newwords);
48         } catch (IOException e) {
49             e.printStackTrace();
50         } finally {
51             if(fr!=null)
52             {
53                 try {
54                     fr.close();
55                 } catch (IOException e) {
56                     e.printStackTrace();
57                 }
58             }
59             if(fw!=null)
60             {
61                 try {
62                     fw.close();
63                 } catch (IOException e) {
64                     e.printStackTrace();
65                 }
66             }
67         }
68         
69     }
70     public static void main(String[] args) {
71         File f1=new File("D://1.txt");
72         File f2=new File("D://2.txt");
73         encodeFile(f1,f2);
74     }
75 }

这份代码对于中文的转换有一些问题,我翻过了很多人的代码,中文转化上都有和我一样的问题,等我研究明白出现问题的原因后,会回来修改代码(2021.2.11)


(2021.2.12)已经解决,原因是java中使用的是UNICODE编码,而记事本保存编码方式为UTF-8,只要更改记事本的编码方式即可,具体的分析写在了新的随笔中:https://www.cnblogs.com/wangqianming12138/p/14398530.html

 

推荐阅读