java_day13练习题
练习1
1.1.训练描述:【try..catch】
一、需求说明:编写代码,产生ArithmeticException异常,并使用try..catch进行处理。
二、处理方式:将异常信息输出在控制台
1.2.操作步骤描述
1.编写打印语句 xx / 0 将问题产生。
2.使用try语句对其包裹。
3.在catch小括中编写对应的ArithmeticException进行捕获。
4.使用printStackTrace方法将异常信息输出在控制台。
public class ExceptionDemo {
public static void main ( String[ ] args) {
System. out. println ( "程序开始" ) ;
try {
method ( ) ;
}
catch ( ArithmeticException e) {
e. printStackTrace ( ) ;
} finally {
System. out. println ( "程序结果" ) ;
}
}
public static void method ( ) {
int a= 10 ;
int b = 0 ;
System. out. println ( a/ b) ;
}
}
练习2
需求说明:拷问文本文件
2.2.操作步骤描述
1.创建FileInputStream对象关联源文件。
2.创建FileOutputStream对象关联目标文件。
3.自定义字节数组提高读写效率。
4.通过while循环不断地将数据读取到数组中。
5.在循环过程中将读取到的数据从数组中写出到目标文件。
6.关闭流释放资源
package com. scy13;
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
import java. io. IOException;
public class CopyTxtTest {
public static void main ( String[ ] args) throws IOException {
FileInputStream fis = new FileInputStream ( "d:\\窗里窗外.txt" ) ;
FileOutputStream fos = new FileOutputStream ( "窗里窗外.txt" ) ;
byte [ ] bys = new byte [ 1024 ] ;
int len;
while ( ( len = fis. read ( ) ) != - 1 ) {
fos. write ( bys, 0 , len) ;
}
fos. close ( ) ;
fis. close ( ) ;
}
}
练习3
一、分析以下需求,并用代码实现:
需求: 读取当前项目下的info1.txt
文件内容如下 : aaaaaaaaaaabbbbbbbbbbbbccdefg
要求将数据去重后写回
最终效果 : fgdebca
1.2.操作步骤描述
1.创建HashSet集合对象用于存储读取到的每一个字符(用于去重)
2.创建字节输入流对象
3.将读取到的字符存储到集合中
4.创建输入流对象关联目标文件
5.遍历Set集合获取到每一个数据, 并调用write方法写出
6.关闭流释放资源
package com. scy13;
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
import java. io. IOException;
import java. util. HashSet;
public class TaskTest {
public static void main ( String[ ] args) throws IOException {
FileInputStream fis = new FileInputStream ( "info1.txt" ) ;
HashSet< Character> ch = new HashSet < Character> ( ) ;
int by;
while ( ( by= fis. read ( ) ) != - 1 ) {
ch. add ( ( char ) by) ;
}
FileOutputStream fos = new FileOutputStream ( "info1.txt" ) ;
System. out. println ( ch) ;
for ( Character c: ch) {
fos. write ( ( int ) c) ;
}
fos. close ( ) ;
fis. close ( ) ;
}
}
练习4
需求分析:统计当前项目下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.关闭流释放资源
package com. scy13;
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
import java. io. IOException;
import java. util. HashMap;
import java. util. HashSet;
import java. util. Set;
public class TaskTest {
public static void main ( String[ ] args) throws IOException {
FileInputStream fis = new FileInputStream ( "info2.txt" ) ;
HashMap< Character, Integer> hm = new HashMap < Character, Integer> ( ) ;
int by;
while ( ( by= fis. read ( ) ) != - 1 ) {
if ( ! hm. containsKey ( ( char ) by) ) {
hm. put ( ( char ) by, 1 ) ;
}
else {
hm. put ( ( char ) by, hm. get ( ( char ) by) + 1 ) ;
}
}
StringBuilder sb = new StringBuilder ( ) ;
Set< Character> set = hm. keySet ( ) ;
for ( Character ch: set) {
sb. append ( ch) ;
sb. append ( "(" ) ;
Integer in = hm. get ( ch) ;
sb. append ( in) ;
sb. append ( ")" ) ;
}
System. out. println ( sb. toString ( ) ) ;
fis. close ( ) ;
}
}
练习5
一、分析以下需求,并用代码实现
需求 : 键盘录入一个字符(以字符串的形式录入)
判断当前字符在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.打印出现的次数
package com. scy13;
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
import java. io. IOException;
import java. util. HashMap;
import java. util. HashSet;
import java. util. Scanner;
import java. util. Set;
public class TaskTest {
public static void main ( String[ ] args) throws IOException {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "键盘录入一个字符(以字符串的形式录入)" ) ;
char ch = sc. nextLine ( ) . charAt ( 0 ) ;
FileInputStream fis = new FileInputStream ( "info3.txt" ) ;
int by;
int count = 0 ;
while ( ( by= fis. read ( ) ) != - 1 ) {
if ( ( int ) ch == by) {
count++ ;
}
}
System. out. println ( "该字符:" + ch+ "出现了" + count+ "次" ) ;
if ( count== 0 ) {
System. out. println ( "该字符不存在" ) ;
}
}
}