Java核心语法练习题——IO流

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();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈年_H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值