day13作业IO流

一、基础案例
1.训练案例1
1.1.训练描述:【try…catch】
一、需求说明:编写代码,产生ArithmeticException异常,并使用try…catch进行处理。
二、处理方式:将异常信息输出在控制台
1.2.操作步骤描述
1.编写打印语句 xx / 0 将问题产生。
2.使用try语句对其包裹。
3.在catch小括中编写对应的ArithmeticException进行捕获。
4.使用printStackTrace方法将异常信息输出在控制台。

public class test1 {
	public static void main(String[] args) {
		int a=10;
		int b=0;
		try {
			System.out.println(a/b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2.训练案例2
2.1.训练描述:【FileInputStream、FileOutputStream】
一、需求说明:拷问文本文件
2.2.操作步骤描述
1.创建FileInputStream对象关联源文件。
2.创建FileOutputStream对象关联目标文件。
3.自定义字节数组提高读写效率。
4.通过while循环不断地将数据读取到数组中。
5.在循环过程中将读取到的数据从数组中写出到目标文件。
6.关闭流释放资源

public class test02 {
	public static void main(String[] args) throws IOException {
		File f1=new File("01.txt");
		f1.createNewFile();
		
		System.out.println(f1.isDirectory());
		System.out.println(f1.isFile());
		
		FileOutputStream fo=new FileOutputStream(new File("b.txt"));
		
		
		FileInputStream fi=new FileInputStream("a.txt");
		
		byte[] b=new byte[1024];
		int len;
		while((len=fi.read(b)) !=-1){
			fo.write(b,0,len);
		}
		
		fi.close();
		fo.close();
		
	}
}

二、扩展案例
1.训练案例1
1.1.训练描述:
一、分析以下需求,并用代码实现:
需求: 读取当前项目下的info1.txt
文件内容如下 : aaaaaaaaaaabbbbbbbbbbbbccdefg
要求将数据去重后写回
最终效果 : fgdebca
1.2.操作步骤描述
1.创建HashSet集合对象用于存储读取到的每一个字符(用于去重)
2.创建字节输入流对象
3.将读取到的字符存储到集合中
4.创建输入流对象关联目标文件
5.遍历Set集合获取到每一个数据, 并调用write方法写出
6.关闭流释放资源

public class test03 {
	public static void main(String[] args) throws IOException {
		File f1=new File("info1.txt");
		f1.createNewFile();
		
		FileInputStream fi=new FileInputStream(f1);
		
		HashSet<Character> hs=new HashSet<>();
		
		int len;
		while((len=fi.read()) !=-1){
			hs.add((char) len);
			
		}		
		System.out.println(hs);		
		FileOutputStream fo=new FileOutputStream(f1);
		Iterator<Character> it=hs.iterator();
		while(it.hasNext()){
			char c=it.next();
			fo.write(c);
		}		
	}
}

2.训练案例2
2.1.训练描述:
需求分析:统计当前项目下info2.txt文件中, 每个字符出现的个数
文件内容如下:
welcome to itheima!!!
最终效果如下:
w(1) (2)!(3)t(2)e(3)c(1)a(1)o(2)l(1)m(2)h(1)i(2)
2.2.作步骤描述
1.创建HashMap集合, 用于统计每个字符出现的次数
2.创建输入流对象关联数据源
3.读取到文件中的每一个字符
4.判断字符是否是第一次出现
a)如果是的话, 键的位置存当前字符, 值的位置存1
b)如果不是第一次出现, 键的位置还是当前字符, 值的位置需要将原本记录的值取出, 然后+1存储
5.创建字符串缓冲区(StringBuilder), 用于拼接结果
6.将数据从集合中取出, 并拼接
7.创建输出流对象
8.调用write方法写出数据
9.关闭流释放资源

public class test04 {
	public static void main(String[] args) throws IOException {
		File f1=new File("info2.txt");
		f1.createNewFile();
		HashMap<Character, Integer> hs=new HashMap<>();
		FileInputStream fi=new FileInputStream(f1);
		
		
		int b;
		while((b=fi.read()) !=-1){
			char c=(char)b;
			if(!hs.containsKey(c)){
				hs.put(c, 1);
			}else{
				hs.put(c, hs.get(c) +1);
			}
		}
		
		StringBuilder sb=new StringBuilder();
		for(Entry<Character, Integer> en:hs.entrySet()){
			sb.append(en.getKey()).append("(").append(en.getValue()).append(")");
		}
		FileOutputStream fo=new FileOutputStream(f1);
		fo.write(sb.toString().getBytes());
		
		fi.close();
		fo.close();
		
	}
}

3.训练案例3
3.1.训练描述:
一、分析以下需求,并用代码实现
需求 : 键盘录入一个字符(以字符串的形式录入)
判断当前字符在info3.txt当中是否存在
如果不存在, 给出提示
如果存在, 请统计出该字符出现的次数

	Info3.txt内容如下:
		abcdefghijklmnopqrstuvwxyz,mnopqrstuvwxyz,mnopqrstuvwxyz,mnopqrstuvwxyz

3.2.操作步骤描述
1.键盘录入要查找的字符
2.创建输入流对象关联info3.txt
3.创建StringBuilder用于存储读取到的所有字符
4.将读取到的字符添加到StringBuilder当中
5.调用indexOf方法判断键盘录入的字符是否存在, 如果返回-1说明不存在
6.如果存在的话, 将StringBuilder拆分成字符数组
7.遍历字符数组, 获取到每一个字符
8.跟键盘录入的字符逐个进行匹配 (不要忘了将字符转换为字符串)
9.打印出现的次数

public static void main(String[] args) throws IOException {
		File f1=new File("inf3.txt");
		f1.createNewFile();
		
		Scanner sc=new Scanner(System.in);
		String a=sc.nextLine();
		
		FileInputStream fi=new FileInputStream(f1);
		
		StringBuilder sb=new StringBuilder();
		int len;
		while((len=fi.read()) !=-1){
			char c=(char)len;
			sb.append(c);
		}
		System.out.println(sb);
		
		
		if(sb.indexOf(a)==-1){
			System.out.println("不存在");
		}else{
			int count=0;
			char[] arr=sb.toString().toCharArray();
			for(char c:arr){
				if(a.equals(c+"")){
					
				
				count++;
				}
			}
			System.out.println(count);
		}
		
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值