IO流加集合加强练习题附答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

休息日补充加强练习题:题目一:将两个集合{“a”,“b”,“c”,“d”,“e”}和{“d”,“e”,“f”,“g”,“h”},把这两个集合去除重复项合并成一个,实现需求importjava.util.ArrayList;/*题目一:将两个集合{“a”,“b”,“c”,“d”,“e”}和{“d”,“e”,“f”,“g”,“h”},把这两个集合去除重复项合并成一个,实现需求*/publicclassDemo10{publicstaticvoidmain(String[]args){ArrayListStringlist1=newArrayListString();list1.add(a);list1.add(b);list1.add(c);list1.add(d);list1.add(e);ArrayListStringlist2=newArrayListString();list2.add(d);list2.add(e);list2.add(f);list2.add(g);list2.add(h);myAdd(list1,list2);}privatestaticvoidmyAdd(ArrayListStringlist1,ArrayListStringlist2){for(Stringstring:list2){if(!list1.contains(string)){list1.add(string);}}System.out.println(list1);}}题目二:已知文件a.txt文件中的内容为“AAbcdea22dferwplkCC321ou1”,请编写程序读取该文件内容,要求去掉重复字母(区分大小写字母)并按照自然排序顺序后输出到b.txt文件中。即b.txt文件内容应为abc......123...这样的顺序输出importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.TreeSet;publicclassDemo11{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderfr=newBufferedReader(newFileReader(C:\\Users\\xiawei\\Desktop\\a.txt));BufferedWriterbw=newBufferedWriter(newFileWriter(C:\\Users\\xiawei\\Desktop\\b.txt));Stringline=null;while((line=fr.readLine())!=null){line=sort(line);bw.write(line);}bw.close();fr.close();}privatestaticStringsort(Stringline){TreeSetCharacterset=newTreeSetCharacter();StringBuildersb=newStringBuilder();char[]ch=line.toCharArray();for(charc:ch){set.add(c);}for(Charactercharacter:set){sb.append(character);}returnsb.toString();}}题目三:把指定目录下(包含子目录)的所有图片,复制到另一个指定目录下importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;//题目三:把指定目录下(包含子目录)的所有图片,复制到另一个指定目录下publicclassDemo4{staticStringregex=.+\\.([j][p][g]|[p][n][g]|[g][i][f]);publicstaticvoidmain(String[]args)throwsIOException{Filefile=newFile(E:\\图片);copyPhoto(file);System.out.println(传输完成!);}privatestaticvoidcopyPhoto(Filefile)throwsIOException{File[]files=file.listFiles();for(Filefile2:files){if(file2.isDirectory()){copyPhoto(file2);}elseif(file2.getName().matches(regex)){BufferedInputStreambis=newBufferedInputStream(newFileInputStream(file2));byte[]buf=newbyte[1024*20];intlenght=0;BufferedOutputStreambos=newBufferedOutputStream(newFileOutputStream(C:\\Users\\xiawei\\Desktop\\新建文件夹\\+file2.getName()));while((lenght=bis.read(buf))!=-1){bos.write(buf,0,lenght);}bos.close();bis.close();}}}}题目四:模拟用户登录功能实现。接收用户输入的用户名和密码,然后和文件中存储的用户名、密码匹配importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Scanner;//模拟用户登录功能实现。接收用户输入的用户名和密码,然后和文件中存储的用户名、密码匹配publicclassDemo13{static{Filefile=newFile(C:\\Users\\xiawei\\Desktop\\config.properties);try{file.createNewFile();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}staticScannersc=newScanner(System.in);publicstaticvoidmain(String[]args)throwsException{while(true){System.out.println(请选择功能A:注册B:登录);Strings=sc.next();if(a.equalsIgnoreCase(s)){//注册register();}elseif(b.equalsIgnoreCase(s)){//登录login();break;}else{System.out.println(您的输入不合法,请输入\a\或者\b\);}}}//注册privatestaticvoidregister()throwsIOException{System.out.println(请输入用户名:);StringAdmin=sc.next();System.out.println(请输入密码:);Stringpassword=sc.next();Stringregisterline=password+=+Admin;BufferedWriterbw=newBufferedWriter(newFileWriter(C:\\Users\\xiawei\\Desktop\\config.properties,true));bw.write(registerline);bw.newLine();System.out.println(注册成功!您的账户名:+Admin+您的密码:+password);bw.close();}//登录privatestaticvoidlogin()throwsIOException,Exception{intcount=0;while(true){System.out.println(请输入用户名:);StringAdmin=sc.next();System.out.println(请输入密码:);Stringpassword=sc.next();Stringline=password+=+Admin;BufferedReaderbr=newBufferedReader(newFileReader(C:\\Users\\xiawei\\Desktop\\config.properties));Stringstr=null;booleanisLogin=false;while((str=br.readLine())!=null){if(line.equals(str)){isLogin=true;break;}}br.close();if(isLogin){System.out.println(Admin+欢迎您回来);break;}else{count++;System.out.println(密码输入错误!);if(count==5){System.out.println(您已多次输入错误,帐号已被锁定30秒,请稍后重试!);Thread.sleep(30000);}if(count==8){System.out.println(程序10秒后自动关闭!);Thread.sleep(10000);System.exit(0);}}}}}题目五:在c盘下有篇txt文本,读取这个文本并统计出这个文本中指定字符的个数(如指定字符为”我”),将该字符与对应的次数输出到当前项目的key.txt中,例如:输出格式为:我=10importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.ArrayList;importjava.util.Scanner;publicclassDemo2{staticScannersc=newScanner(System.in);publicstaticvoidmain(String[]args)throwsIOException{ArrayListCharacterlist=newArrayListCharacter();Filefile=newFile(C:\\Users\\xiawei\\Desktop\\b.txt);BufferedReaderbr=newBufferedReader(newFileReader(file));intlen=0;while((len=br.read())!=-1){list.add((char)len);}br.close();intcount=0;for(Characterc:list){if(c=='我'){count++;}}Stringline=我=+count;Buf

1 / 29
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功