IO流的练习题
一、获取一个utf_8编码格式文本(a_utf8.txt)上的每个字符出现的次数,将结果写在编码格式为GBK的times_gbk.txt文件上
public class hmoework {
public static void main(String[] args) throws IOException {
InputStreamReader fr = new InputStreamReader(new FileInputStream("a_utf8.txt"),"utf8");//读取编码格式为utf8的文件
Map<String,Integer> m = new HashMap<String,Integer>();//键存文件中的内容,值存次数
int a;
char ch;//得到每一个字符,判断是否是特殊字符
String str;//用于特殊字符的转义
while((a = fr.read()) != -1) {//读取到的字符转为字节复制给a
ch = (char)a;
switch(ch) {//转义
case '\t' :
str = "\\t";
break;
case '\n' :
str = "\\n";
break;
case '\r' :
str = "\\r";
break;
case ' ' :
str = "空格";
break;
default :
str = String.valueOf(ch);
}
//给map集合赋值
m.put(str, m.containsKey(str) ? m.get(str)+1 : 1);
}
fr.close();
//将map集合显示到指定文件中
FileWriter fw = new FileWriter("times_gbk.txt");
for (Map.Entry<String, Integer> entry : m.entrySet()) {
fw.write(entry.getKey() + "(" + entry.getValue() + ")" + " ");
}
fw.close();
}
}
二、当我们下载一个试用版软件,没有购买正版的时候,每执行一次就会提醒我们还有多少次使用机会,用学过的IO流知识,模拟试用版软件,试用10次机会,执行一次就提示一次您还有几次机会,如果次数到了提示请购买正版
public class Homework {
public static void main(String[] args) throws IOException {
//count.txt中记录次数
FileReader fr = new FileReader("count.txt");
BufferedReader br = new BufferedReader(fr);
//这个位置不能写输出流,至少要写出在输入流相关读取操作完成之后。(因为他们都指向一个文件对象)
// FileWriter fw = new FileWriter("count.txt");
String str = br.readLine();
fr.close();
int count = Integer.parseInt(str);//得到文件中所记录的数字
if(count > 0) {
System.out.println("祝您学习愉快");
System.out.println("剩余的免费使用次数为:" + --count);
FileWriter fw = new FileWriter("count.txt");
fw.write(String.valueOf(count));//这里不能写出整型,不然会被解码为其他字符
fw.close();
}else {
System.out.println("免费次数已用完,请购买");
}
}
}
三、用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//…
(3)按照上面的打印格式将内容写入到D:\count.txt文件中(要求用高效流)
PS:这里使用的list集合,推荐使用Map集合(key存储值,value存储次数)
public class homework {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\\\count.txt"));
String str = "If you want to change your fate I think you must come to the ujiuye to learn java";
String[] ch = str.split(" ");//以空格切分为字符数组
List<String> list = new ArrayList<String>();
List<String> listcopy = new ArrayList<String>();//备份用于判断
for (String st : ch) {//向list中添加数据
list.add(st);
}
for (String string : list) {
//统计单词出现的次数
int count = Collections.frequency(list, string);
//防止单词重复出现
if(!listcopy.contains(string)) {
listcopy.add(string);
bw.write(string + "=" + count + " ");
bw.newLine();
}
}
bw.close();
}
}
四、产生10个1-100的随机数,并放到一个数组中
(1)把数组中大于等于10的数字放到一个list集合中,去重,并打印在控制台上。
(2)把数组中的数字到放当前文件夹的number.txt文件中
public class homework {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("number.txt");
Random ran = new Random();
int a;
LinkedHashSet<Integer> lhs = new LinkedHashSet<Integer>();
while(true) {
a = ran.nextInt(100);
if(a>=10) {
if(!lhs.contains(a)) {
System.out.print(a + " ");
lhs.add(a);
if(lhs.size() == 10)
break;
}
}
}
for (Integer integer : lhs) {
fw.write(integer + " ");//反之将int类型的数字,当成字节解码成对应字符。所以通过拼接把它变成string
}
fw.close();
}
}