JAVA高级练习题.docx
《JAVA高级练习题.docx》由会员分享,可在线阅读,更多相关《JAVA高级练习题.docx(26页珍藏版)》请在冰点文库上搜索。
JAVA高级练习题
1、编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。
2、编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e出现的次数。
packagezuoye;
/**
*输岀e的岀现次数
*/import;
publicclassTest2{
publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);
Stringstr=input.next();
intcount=0;
for(inti=0;i{if(str.charAt(i)=='e'){count++;}}"e的出现次数是:"+count);}}3、生成十个0~100之间的随机数,放到数组中,然后排序输出。packagezuoye;import/***生成十个0〜100之间的随机数,放到数组中,然后排序输岀*@authorAdministrator*/publicclassTest3{publicstaticvoidmain(String[]args){intarr[]=newint[10];//定义一个长度为10的int数组for(inti=0;iarr[i]=(int)(Math.random()*100);//取0~1的随机数*100,即为0~100的随机数}Arrays.sort(arr);//排序方法for(inta:arr)//增强for循环{"");}}4、巴黎时间比北京时间晚7个小时,纽约时间比北京时间晚12个小时,试编写一程序,根据输入的北京时间输出相应的巴黎和纽约时间。packagezuoye;import;import;publicclassTest4{publicvoidgetTime(inty,intm,intd,inth,intmi,ints){//获取Calendar实例Calendartime=Calendar.getInstance();//设置巴黎时间time.set(y,m,d,h-7,mi,s);//输出巴黎时间"巴黎时间是"+time.get(Calendar.YEAR)+'年"+((time.get(Calendar.MONTH)))+"月"+time.get(Calendar.DAY_OF_MONTH)+"日"+time.get(Calendar.HOUR_OF_DAY)+'时"+time.get(Calendar.MINUTE)+"分"+time.get(Calendar.SECOND)+'秒");//设置纽约时间time.set(y,m,d,h-12,mi);//输出纽约时间"纽约时间是"+time.get(Calendar.YEAR)+'年"+((time.get(Calendar.MONTH)))+"月"+time.get(Calendar.DAY_OF_MONTH)+"日"+time.get(Calendar.HOUR_OF_DAY)+'时"+time.get(Calendar.MINUTE)+"分"+time.get(Calendar.SECOND)+'秒");}publicstaticvoidmain(String[]args){Test4time=newTest4();//设置北京时间time.getTime(2018,3,15,16,53,9);}}5、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名如果邮箱地址不合法则显示不合法的原因提示:邮箱地址不合法的因素:1)邮箱地址中不包含@或。2)邮箱地址中含有多了@或。3)邮箱地址中。出现在@的前面4)用户名里有其他字符实现步骤:(1)创建一个类,类名:mailtest类图如下:(类名和方法名必须与要求一样。区分大小写)packageyouxiang;import;publicclassMailTest{publicstaticbooleantestMail(){Scannerinput=newScanner(System.in);Strings=input.next();if(s.indexOf("@")==-1||s.indexOf(".")==-1){"邮箱地址中不包含@或.");returnfalse;}if(s.indexOf("@")!=s.lastlndexOf("@")||s.indexOf(".")!=s.lastlndexOf(".")){"邮箱地址中含有多余的@或.");returnfalse;}if(s.indexOf("@")>s.lastIndexOf(".")){"邮箱地址中.出现在@的前面");returnfalse;}for(inti=0;i{if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')){}else{"用户名里有其他字符”);returnfalse;}}returntrue;}}packageyouxiang;publicclassTest{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubif(MailTest.testMail()){"邮箱格式合法”);}else{"邮箱格式不合法");}}}6、分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。packagezuoye;import;/***控制台输入一段字符串,再查询一段子字符串岀现的次数*/publicclassTest6{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);Stringa=input.next();Stringb=input.next();intcount=0;//计数器for(inti=0;i{if(a.indexOf(b,i)!=-1){count++;}}>}}集合类1、请使用LinkedList来模拟一个队列(先进先出的特性):1)拥有放入对象的方法voidput(Objecto)2)取出对象的方法Objectget()3)判断队列当中是否为空的方法booleanisEmpty();并且,编写测试代码,验证你的队列是否正确。publicclassLinkeds{Listl;Linkeds(){l=newLinkedList();}publicvoidput(Objecto){l.add(o);}publicObjectget(){Objecto=l.get(0);l.remove(0);returno;}publicbooleanisEmpty(){if(l.isEmpty()){returntrue;}returnfalse;}publicstaticvoidmain(String[]args){//定义一个空队列Linkedsl=newLinkeds();//往队列中放入对象l.put("Tom1");l.put("John2");l.put("Mary3");//如果队列不为空,依次输出队列中的元素while(!l.isEmpty()){}}}2、假设顺序列表ArrayList中存储的元素是整型数字1~5,遍历每个元素,将每个元素顺序输出。packagezuoye;import;import;import;publicclassTest1{publicstaticvoidmain(String[]args){Listlist=newArrayList<>();Numbern1=newNumber(1);Numbern2=newNumber(2);Numbern3=newNumber(3);Numbern4=newNumber(4);Numbern5=newNumber(5);list.add(n1);list.add(n2);list.add(n3);list.add(n4);list.add(n5);Iteratorit=list.iterator();while(it.hasNext()){}}}3、在一个歹U表中存储以下元素:apple,grape,banana,pear1)返回集合中的最大的和最小的元素2)将集合进行排序,并将排序后的结果打印在控制台上packagezuoye;publicclassFruit{privateStringfruit;publicFruit(){}publicFruit(Stringfruit){this.fruit=fruit;}publicStringgetFruit(){returnfruit;}publicvoidsetFruit(Stringfruit){this.fruit=fruit;}}packagezuoye;import;import;import;publicclassFruitTest{publicstaticvoidmain(String[]args){Listfruit=Arrays.asList("applegradebananapear".split(""));"最大值是:"+Collections.max(fruit));"最小值是:"+Collections.min(fruit));}}(其中储户的主要信4、编写一个程序,创建一个HashMap对象,用于存储银行储户的信息息有储户的ID,姓名和余额)。另外,计算并显示其中某个储户的当前余额。packagezuoye;publicclassBank{privateStringid;privateStringname;privatedoublemoneypublicBank(){}publicBank(Stringid,Stringname,doublemoney){this.id=id;this.name=name;this.money=money;}publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetMoney(){returnmoney;}publicvoidsetMoney(doublemoney){this.money=money;}}packagezuoye;import;import;import;import;publicclassBankTest{publicstaticvoidmain(String[]args){Mapmap=newHashMap();map.put("用户1",newBank("1","张三",Math.random()*100000));map.put("用户2",newBank("2","李四",Math.random()*100000));map.put("用户3",newBank("3","王五",Math.random()*100000));map.put("用户4",newBank("4","赵六",Math.random()*100000));SetkeySet=map.keySet();Iteratorit=keySet.iterator();doublesum=0;while(it.hasNext()){Objectkey=it.next();Banka=map.get(key);"账户"+a.getId()+"的余额:"+a.getMoney());//计算所有帐户的余额之和sum+=a.getMoney();}"所有账户余额:"+sum);}}5、从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。packagezuoye;import;import;import;publicclassTest5{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);Listlist=newArrayList();Stringstr=input.nextLine();String[]arr=str.split("");Stringtmp;for(inti=0;ifor(intj=i+1;jif(arr[i].compareToIgnoreCase(arr[j])>0){str=arr[j];arr[j]=arr[j+1];arr[j+1]=str;}}}for(Stringa:arr){list.add(a);J}}}IO流1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:路径名:c:\temp\def.txtpackage作业;import;publicclassTest1{publicstaticvoidmain(String[]args){Filefile=newFile(新建文件夹");String[]files=file.list();for(Stringo:files){"文件名:"+o);"路径:"+file.getAbsolutePath()+"\\"+o);"");}}}2、编写一个java程序实现文件复制功能,要求将d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。package作业;import;import;import;/***文件的拷贝*/publicclassTest2{publicstaticvoidmain(String[]args){Filefile=newFile("file\\abc.txt");Filefile_back=newFile("file\\abc_back.txt");try(FileInputStreamfi=newFileInputStream(file);FileOutputStreamfo=newFileOutputStream(file_back);){intb=-1;while((b=fi.read())!=-1){fo.write(b);}}catch(Exceptione){e.printStackTrace();}"拷貝成功!");}}3、创建c:/test.txt文件并在其中输入"helloworld"创建一个输入流读取该文件中的文本并且把小写的l变成大写L再利用输出流写入到d:\test.txt中实现步骤:1.在本地硬盘C盘下创建一个文件test.txt2.创建一个包含main()方法的类,并在main中编写代码3.运行代码并且测试结果实现过滤器的功能效果显示:package作业;import;import;import;import;import;import;/***替换文件中的小写字母l为大写字母L*/publicclassTest3{publicstaticvoidmain(String[]args){FileReaderf;Strings="";BufferedReaderbr=null;try{f=newFileReader("F:\\test.txt");br=newBufferedReader(f);s=br.readLine();"源文件"+s);s=s.replace("l","L");}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{try{br.close();}catch(IOExceptione){e.printStackTrace();}}BufferedWriterbw=null;Jtry{FileWriterfw=newFileWriter("F:\\test.txt");bw=newBufferedWriter(fw);bw.write(s);}catch(IOExceptione){e.printStackTrace();}finally{try{bw.close();}catch(IOExceptione){e.printStackTrace();}}}}4、在程序中创建一个Student类型的对象,并把对象信息保存到d:/io/student.txt文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:package作业;import;publicclassStudent{privateintid;privateprivatepublicStringname;Datebirth;Student(){super();}publicStudent(intid,Stringname,Datebirth){super();this.id=id;this.name=name;this.birth=birth;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicDategetBirth(){returnbirth;}publicvoidsetBirth(Datebirth){this.birth=birth;}@OverridepublicStringtoString(){return"Student[id="+id+",name="+name+",birth="+birth+"]";}}publicclassSerializationExercise{publicstaticvoidmain(Stringargs[]){Students1=newStudent("1","zhangsan","1999-11-11");try{FileOutputStreamfos=newFileOutputStream("d:\\io\\student.txt");ObjectOutputStreamoos=newObjectOutputStream(fos);"对象序列化...");oos.writeObject(s1);oos.flush();oos.close();}catch(FileNotFoundExceptione1){J}catch(IOExceptione2){J}try{FileInputStreamfis=newFileInputStream("d:\\io\\student.txt");ObjectInputStreamois=newOb
{
if(str.charAt(i)=='e')
count++;
}
"e的出现次数是:
"+count);
3、生成十个0~100之间的随机数,放到数组中,然后排序输出。
import/**
*生成十个0〜100之间的随机数,放到数组中,然后排序输岀
*@authorAdministrator
*/
publicclassTest3{
intarr[]=newint[10];//定义一个长度为10的int数组
for(inti=0;iarr[i]=(int)(Math.random()*100);//取0~1的随机数*100,即为0~100的随机数}Arrays.sort(arr);//排序方法for(inta:arr)//增强for循环{"");}}4、巴黎时间比北京时间晚7个小时,纽约时间比北京时间晚12个小时,试编写一程序,根据输入的北京时间输出相应的巴黎和纽约时间。packagezuoye;import;import;publicclassTest4{publicvoidgetTime(inty,intm,intd,inth,intmi,ints){//获取Calendar实例Calendartime=Calendar.getInstance();//设置巴黎时间time.set(y,m,d,h-7,mi,s);//输出巴黎时间"巴黎时间是"+time.get(Calendar.YEAR)+'年"+((time.get(Calendar.MONTH)))+"月"+time.get(Calendar.DAY_OF_MONTH)+"日"+time.get(Calendar.HOUR_OF_DAY)+'时"+time.get(Calendar.MINUTE)+"分"+time.get(Calendar.SECOND)+'秒");//设置纽约时间time.set(y,m,d,h-12,mi);//输出纽约时间"纽约时间是"+time.get(Calendar.YEAR)+'年"+((time.get(Calendar.MONTH)))+"月"+time.get(Calendar.DAY_OF_MONTH)+"日"+time.get(Calendar.HOUR_OF_DAY)+'时"+time.get(Calendar.MINUTE)+"分"+time.get(Calendar.SECOND)+'秒");}publicstaticvoidmain(String[]args){Test4time=newTest4();//设置北京时间time.getTime(2018,3,15,16,53,9);}}5、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名如果邮箱地址不合法则显示不合法的原因提示:邮箱地址不合法的因素:1)邮箱地址中不包含@或。2)邮箱地址中含有多了@或。3)邮箱地址中。出现在@的前面4)用户名里有其他字符实现步骤:(1)创建一个类,类名:mailtest类图如下:(类名和方法名必须与要求一样。区分大小写)packageyouxiang;import;publicclassMailTest{publicstaticbooleantestMail(){Scannerinput=newScanner(System.in);Strings=input.next();if(s.indexOf("@")==-1||s.indexOf(".")==-1){"邮箱地址中不包含@或.");returnfalse;}if(s.indexOf("@")!=s.lastlndexOf("@")||s.indexOf(".")!=s.lastlndexOf(".")){"邮箱地址中含有多余的@或.");returnfalse;}if(s.indexOf("@")>s.lastIndexOf(".")){"邮箱地址中.出现在@的前面");returnfalse;}for(inti=0;i{if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')){}else{"用户名里有其他字符”);returnfalse;}}returntrue;}}packageyouxiang;publicclassTest{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubif(MailTest.testMail()){"邮箱格式合法”);}else{"邮箱格式不合法");}}}6、分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。packagezuoye;import;/***控制台输入一段字符串,再查询一段子字符串岀现的次数*/publicclassTest6{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);Stringa=input.next();Stringb=input.next();intcount=0;//计数器for(inti=0;i{if(a.indexOf(b,i)!=-1){count++;}}>}}集合类1、请使用LinkedList来模拟一个队列(先进先出的特性):1)拥有放入对象的方法voidput(Objecto)2)取出对象的方法Objectget()3)判断队列当中是否为空的方法booleanisEmpty();并且,编写测试代码,验证你的队列是否正确。publicclassLinkeds{Listl;Linkeds(){l=newLinkedList();}publicvoidput(Objecto){l.add(o);}publicObjectget(){Objecto=l.get(0);l.remove(0);returno;}publicbooleanisEmpty(){if(l.isEmpty()){returntrue;}returnfalse;}publicstaticvoidmain(String[]args){//定义一个空队列Linkedsl=newLinkeds();//往队列中放入对象l.put("Tom1");l.put("John2");l.put("Mary3");//如果队列不为空,依次输出队列中的元素while(!l.isEmpty()){}}}2、假设顺序列表ArrayList中存储的元素是整型数字1~5,遍历每个元素,将每个元素顺序输出。packagezuoye;import;import;import;publicclassTest1{publicstaticvoidmain(String[]args){Listlist=newArrayList<>();Numbern1=newNumber(1);Numbern2=newNumber(2);Numbern3=newNumber(3);Numbern4=newNumber(4);Numbern5=newNumber(5);list.add(n1);list.add(n2);list.add(n3);list.add(n4);list.add(n5);Iteratorit=list.iterator();while(it.hasNext()){}}}3、在一个歹U表中存储以下元素:apple,grape,banana,pear1)返回集合中的最大的和最小的元素2)将集合进行排序,并将排序后的结果打印在控制台上packagezuoye;publicclassFruit{privateStringfruit;publicFruit(){}publicFruit(Stringfruit){this.fruit=fruit;}publicStringgetFruit(){returnfruit;}publicvoidsetFruit(Stringfruit){this.fruit=fruit;}}packagezuoye;import;import;import;publicclassFruitTest{publicstaticvoidmain(String[]args){Listfruit=Arrays.asList("applegradebananapear".split(""));"最大值是:"+Collections.max(fruit));"最小值是:"+Collections.min(fruit));}}(其中储户的主要信4、编写一个程序,创建一个HashMap对象,用于存储银行储户的信息息有储户的ID,姓名和余额)。另外,计算并显示其中某个储户的当前余额。packagezuoye;publicclassBank{privateStringid;privateStringname;privatedoublemoneypublicBank(){}publicBank(Stringid,Stringname,doublemoney){this.id=id;this.name=name;this.money=money;}publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetMoney(){returnmoney;}publicvoidsetMoney(doublemoney){this.money=money;}}packagezuoye;import;import;import;import;publicclassBankTest{publicstaticvoidmain(String[]args){Mapmap=newHashMap();map.put("用户1",newBank("1","张三",Math.random()*100000));map.put("用户2",newBank("2","李四",Math.random()*100000));map.put("用户3",newBank("3","王五",Math.random()*100000));map.put("用户4",newBank("4","赵六",Math.random()*100000));SetkeySet=map.keySet();Iteratorit=keySet.iterator();doublesum=0;while(it.hasNext()){Objectkey=it.next();Banka=map.get(key);"账户"+a.getId()+"的余额:"+a.getMoney());//计算所有帐户的余额之和sum+=a.getMoney();}"所有账户余额:"+sum);}}5、从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。packagezuoye;import;import;import;publicclassTest5{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);Listlist=newArrayList();Stringstr=input.nextLine();String[]arr=str.split("");Stringtmp;for(inti=0;ifor(intj=i+1;jif(arr[i].compareToIgnoreCase(arr[j])>0){str=arr[j];arr[j]=arr[j+1];arr[j+1]=str;}}}for(Stringa:arr){list.add(a);J}}}IO流1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:路径名:c:\temp\def.txtpackage作业;import;publicclassTest1{publicstaticvoidmain(String[]args){Filefile=newFile(新建文件夹");String[]files=file.list();for(Stringo:files){"文件名:"+o);"路径:"+file.getAbsolutePath()+"\\"+o);"");}}}2、编写一个java程序实现文件复制功能,要求将d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。package作业;import;import;import;/***文件的拷贝*/publicclassTest2{publicstaticvoidmain(String[]args){Filefile=newFile("file\\abc.txt");Filefile_back=newFile("file\\abc_back.txt");try(FileInputStreamfi=newFileInputStream(file);FileOutputStreamfo=newFileOutputStream(file_back);){intb=-1;while((b=fi.read())!=-1){fo.write(b);}}catch(Exceptione){e.printStackTrace();}"拷貝成功!");}}3、创建c:/test.txt文件并在其中输入"helloworld"创建一个输入流读取该文件中的文本并且把小写的l变成大写L再利用输出流写入到d:\test.txt中实现步骤:1.在本地硬盘C盘下创建一个文件test.txt2.创建一个包含main()方法的类,并在main中编写代码3.运行代码并且测试结果实现过滤器的功能效果显示:package作业;import;import;import;import;import;import;/***替换文件中的小写字母l为大写字母L*/publicclassTest3{publicstaticvoidmain(String[]args){FileReaderf;Strings="";BufferedReaderbr=null;try{f=newFileReader("F:\\test.txt");br=newBufferedReader(f);s=br.readLine();"源文件"+s);s=s.replace("l","L");}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{try{br.close();}catch(IOExceptione){e.printStackTrace();}}BufferedWriterbw=null;Jtry{FileWriterfw=newFileWriter("F:\\test.txt");bw=newBufferedWriter(fw);bw.write(s);}catch(IOExceptione){e.printStackTrace();}finally{try{bw.close();}catch(IOExceptione){e.printStackTrace();}}}}4、在程序中创建一个Student类型的对象,并把对象信息保存到d:/io/student.txt文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:package作业;import;publicclassStudent{privateintid;privateprivatepublicStringname;Datebirth;Student(){super();}publicStudent(intid,Stringname,Datebirth){super();this.id=id;this.name=name;this.birth=birth;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicDategetBirth(){returnbirth;}publicvoidsetBirth(Datebirth){this.birth=birth;}@OverridepublicStringtoString(){return"Student[id="+id+",name="+name+",birth="+birth+"]";}}publicclassSerializationExercise{publicstaticvoidmain(Stringargs[]){Students1=newStudent("1","zhangsan","1999-11-11");try{FileOutputStreamfos=newFileOutputStream("d:\\io\\student.txt");ObjectOutputStreamoos=newObjectOutputStream(fos);"对象序列化...");oos.writeObject(s1);oos.flush();oos.close();}catch(FileNotFoundExceptione1){J}catch(IOExceptione2){J}try{FileInputStreamfis=newFileInputStream("d:\\io\\student.txt");ObjectInputStreamois=newOb
arr[i]=(int)(Math.random()*100);//取0~1的随机数*100,即为0~100的随机数
Arrays.sort(arr);//排序方法
for(inta:
arr)//增强for循环
"");
4、巴黎时间比北京时间晚7个小时,纽约时间比北京时间晚12个小时,试编写一程序,
根据输入的北京时间输出相应的巴黎和纽约时间。
import;
publicclassTest4{
publicvoidgetTime(inty,intm,intd,inth,intmi,ints){
//获取Calendar实例
Calendartime=Calendar.getInstance();
//设置巴黎时间
time.set(y,m,d,h-7,mi,s);
//输出巴黎时间
"巴黎时间是"+time.get(Calendar.YEAR)+'年"+
((time.get(Calendar.MONTH)))+"月"+
time.get(Calendar.DAY_OF_MONTH)+"日"+
time.get(Calendar.HOUR_OF_DAY)+'时"+time.get(Calendar.MINUTE)+"分"+time.get(Calendar.SECOND)+'秒");
//设置纽约时间
time.set(y,m,d,h-12,mi);
//输出纽约时间
"纽约时间是"+time.get(Calendar.YEAR)+'年"+
Test4time=newTest4();
//设置北京时间time.getTime(2018,3,15,16,53,9);
5、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名如果邮箱地址不合法则显示不合法的原因提示:
邮箱地址不合法的因素:
1)邮箱地址中不包含@或。
2)邮箱地址中含有多了@或。
3)邮箱地址中。
出现在@的前面
4)用户名里有其他字符
实现步骤:
(1)创建一个类,类名:
mailtest
类图如下:
(类名和方法名必须与要求一样。
区分大小写)
packageyouxiang;
publicclassMailTest{
publicstaticbooleantestMail(){
Strings=input.next();
if(s.indexOf("@")==-1||s.indexOf(".")==-1)
"邮箱地址中不包含@或.");
returnfalse;
if(s.indexOf("@")!
=s.lastlndexOf("@")||s.indexOf(".")!
=s.lastlndexOf("."))
"邮箱地址中含有多余的@或.");
if(s.indexOf("@")>s.lastIndexOf("."))
"邮箱地址中.出现在@的前面");
for(inti=0;i{if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')){}else{"用户名里有其他字符”);returnfalse;}}returntrue;}}packageyouxiang;publicclassTest{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubif(MailTest.testMail()){"邮箱格式合法”);}else{"邮箱格式不合法");}}}6、分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。packagezuoye;import;/***控制台输入一段字符串,再查询一段子字符串岀现的次数*/publicclassTest6{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);Stringa=input.next();Stringb=input.next();intcount=0;//计数器for(inti=0;i{if(a.indexOf(b,i)!=-1){count++;}}>}}集合类1、请使用LinkedList来模拟一个队列(先进先出的特性):1)拥有放入对象的方法voidput(Objecto)2)取出对象的方法Objectget()3)判断队列当中是否为空的方法booleanisEmpty();并且,编写测试代码,验证你的队列是否正确。publicclassLinkeds{Listl;Linkeds(){l=newLinkedList();}publicvoidput(Objecto){l.add(o);}publicObjectget(){Objecto=l.get(0);l.remove(0);returno;}publicbooleanisEmpty(){if(l.isEmpty()){returntrue;}returnfalse;}publicstaticvoidmain(String[]args){//定义一个空队列Linkedsl=newLinkeds();//往队列中放入对象l.put("Tom1");l.put("John2");l.put("Mary3");//如果队列不为空,依次输出队列中的元素while(!l.isEmpty()){}}}2、假设顺序列表ArrayList中存储的元素是整型数字1~5,遍历每个元素,将每个元素顺序输出。packagezuoye;import;import;import;publicclassTest1{publicstaticvoidmain(String[]args){Listlist=newArrayList<>();Numbern1=newNumber(1);Numbern2=newNumber(2);Numbern3=newNumber(3);Numbern4=newNumber(4);Numbern5=newNumber(5);list.add(n1);list.add(n2);list.add(n3);list.add(n4);list.add(n5);Iteratorit=list.iterator();while(it.hasNext()){}}}3、在一个歹U表中存储以下元素:apple,grape,banana,pear1)返回集合中的最大的和最小的元素2)将集合进行排序,并将排序后的结果打印在控制台上packagezuoye;publicclassFruit{privateStringfruit;publicFruit(){}publicFruit(Stringfruit){this.fruit=fruit;}publicStringgetFruit(){returnfruit;}publicvoidsetFruit(Stringfruit){this.fruit=fruit;}}packagezuoye;import;import;import;publicclassFruitTest{publicstaticvoidmain(String[]args){Listfruit=Arrays.asList("applegradebananapear".split(""));"最大值是:"+Collections.max(fruit));"最小值是:"+Collections.min(fruit));}}(其中储户的主要信4、编写一个程序,创建一个HashMap对象,用于存储银行储户的信息息有储户的ID,姓名和余额)。另外,计算并显示其中某个储户的当前余额。packagezuoye;publicclassBank{privateStringid;privateStringname;privatedoublemoneypublicBank(){}publicBank(Stringid,Stringname,doublemoney){this.id=id;this.name=name;this.money=money;}publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetMoney(){returnmoney;}publicvoidsetMoney(doublemoney){this.money=money;}}packagezuoye;import;import;import;import;publicclassBankTest{publicstaticvoidmain(String[]args){Mapmap=newHashMap();map.put("用户1",newBank("1","张三",Math.random()*100000));map.put("用户2",newBank("2","李四",Math.random()*100000));map.put("用户3",newBank("3","王五",Math.random()*100000));map.put("用户4",newBank("4","赵六",Math.random()*100000));SetkeySet=map.keySet();Iteratorit=keySet.iterator();doublesum=0;while(it.hasNext()){Objectkey=it.next();Banka=map.get(key);"账户"+a.getId()+"的余额:"+a.getMoney());//计算所有帐户的余额之和sum+=a.getMoney();}"所有账户余额:"+sum);}}5、从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。packagezuoye;import;import;import;publicclassTest5{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);Listlist=newArrayList();Stringstr=input.nextLine();String[]arr=str.split("");Stringtmp;for(inti=0;ifor(intj=i+1;jif(arr[i].compareToIgnoreCase(arr[j])>0){str=arr[j];arr[j]=arr[j+1];arr[j+1]=str;}}}for(Stringa:arr){list.add(a);J}}}IO流1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:路径名:c:\temp\def.txtpackage作业;import;publicclassTest1{publicstaticvoidmain(String[]args){Filefile=newFile(新建文件夹");String[]files=file.list();for(Stringo:files){"文件名:"+o);"路径:"+file.getAbsolutePath()+"\\"+o);"");}}}2、编写一个java程序实现文件复制功能,要求将d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。package作业;import;import;import;/***文件的拷贝*/publicclassTest2{publicstaticvoidmain(String[]args){Filefile=newFile("file\\abc.txt");Filefile_back=newFile("file\\abc_back.txt");try(FileInputStreamfi=newFileInputStream(file);FileOutputStreamfo=newFileOutputStream(file_back);){intb=-1;while((b=fi.read())!=-1){fo.write(b);}}catch(Exceptione){e.printStackTrace();}"拷貝成功!");}}3、创建c:/test.txt文件并在其中输入"helloworld"创建一个输入流读取该文件中的文本并且把小写的l变成大写L再利用输出流写入到d:\test.txt中实现步骤:1.在本地硬盘C盘下创建一个文件test.txt2.创建一个包含main()方法的类,并在main中编写代码3.运行代码并且测试结果实现过滤器的功能效果显示:package作业;import;import;import;import;import;import;/***替换文件中的小写字母l为大写字母L*/publicclassTest3{publicstaticvoidmain(String[]args){FileReaderf;Strings="";BufferedReaderbr=null;try{f=newFileReader("F:\\test.txt");br=newBufferedReader(f);s=br.readLine();"源文件"+s);s=s.replace("l","L");}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{try{br.close();}catch(IOExceptione){e.printStackTrace();}}BufferedWriterbw=null;Jtry{FileWriterfw=newFileWriter("F:\\test.txt");bw=newBufferedWriter(fw);bw.write(s);}catch(IOExceptione){e.printStackTrace();}finally{try{bw.close();}catch(IOExceptione){e.printStackTrace();}}}}4、在程序中创建一个Student类型的对象,并把对象信息保存到d:/io/student.txt文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:package作业;import;publicclassStudent{privateintid;privateprivatepublicStringname;Datebirth;Student(){super();}publicStudent(intid,Stringname,Datebirth){super();this.id=id;this.name=name;this.birth=birth;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicDategetBirth(){returnbirth;}publicvoidsetBirth(Datebirth){this.birth=birth;}@OverridepublicStringtoString(){return"Student[id="+id+",name="+name+",birth="+birth+"]";}}publicclassSerializationExercise{publicstaticvoidmain(Stringargs[]){Students1=newStudent("1","zhangsan","1999-11-11");try{FileOutputStreamfos=newFileOutputStream("d:\\io\\student.txt");ObjectOutputStreamoos=newObjectOutputStream(fos);"对象序列化...");oos.writeObject(s1);oos.flush();oos.close();}catch(FileNotFoundExceptione1){J}catch(IOExceptione2){J}try{FileInputStreamfis=newFileInputStream("d:\\io\\student.txt");ObjectInputStreamois=newOb
if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z'))
}else
"用户名里有其他字符”);
returntrue;
publicclassTest{
//TODOAuto-generatedmethodstub
if(MailTest.testMail()){
"邮箱格式合法”);
}else{
"邮箱格式不合法");
6、分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。
*控制台输入一段字符串,再查询一段子字符串岀现的次数
*/publicclassTest6{
Stringa=input.next();
Stringb=input.next();
intcount=0;//计数器
for(inti=0;i{if(a.indexOf(b,i)!=-1){count++;}}>}}集合类1、请使用LinkedList来模拟一个队列(先进先出的特性):1)拥有放入对象的方法voidput(Objecto)2)取出对象的方法Objectget()3)判断队列当中是否为空的方法booleanisEmpty();并且,编写测试代码,验证你的队列是否正确。publicclassLinkeds{Listl;Linkeds(){l=newLinkedList();}publicvoidput(Objecto){l.add(o);}publicObjectget(){Objecto=l.get(0);l.remove(0);returno;}publicbooleanisEmpty(){if(l.isEmpty()){returntrue;}returnfalse;}publicstaticvoidmain(String[]args){//定义一个空队列Linkedsl=newLinkeds();//往队列中放入对象l.put("Tom1");l.put("John2");l.put("Mary3");//如果队列不为空,依次输出队列中的元素while(!l.isEmpty()){}}}2、假设顺序列表ArrayList中存储的元素是整型数字1~5,遍历每个元素,将每个元素顺序输出。packagezuoye;import;import;import;publicclassTest1{publicstaticvoidmain(String[]args){Listlist=newArrayList<>();Numbern1=newNumber(1);Numbern2=newNumber(2);Numbern3=newNumber(3);Numbern4=newNumber(4);Numbern5=newNumber(5);list.add(n1);list.add(n2);list.add(n3);list.add(n4);list.add(n5);Iteratorit=list.iterator();while(it.hasNext()){}}}3、在一个歹U表中存储以下元素:apple,grape,banana,pear1)返回集合中的最大的和最小的元素2)将集合进行排序,并将排序后的结果打印在控制台上packagezuoye;publicclassFruit{privateStringfruit;publicFruit(){}publicFruit(Stringfruit){this.fruit=fruit;}publicStringgetFruit(){returnfruit;}publicvoidsetFruit(Stringfruit){this.fruit=fruit;}}packagezuoye;import;import;import;publicclassFruitTest{publicstaticvoidmain(String[]args){Listfruit=Arrays.asList("applegradebananapear".split(""));"最大值是:"+Collections.max(fruit));"最小值是:"+Collections.min(fruit));}}(其中储户的主要信4、编写一个程序,创建一个HashMap对象,用于存储银行储户的信息息有储户的ID,姓名和余额)。另外,计算并显示其中某个储户的当前余额。packagezuoye;publicclassBank{privateStringid;privateStringname;privatedoublemoneypublicBank(){}publicBank(Stringid,Stringname,doublemoney){this.id=id;this.name=name;this.money=money;}publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetMoney(){returnmoney;}publicvoidsetMoney(doublemoney){this.money=money;}}packagezuoye;import;import;import;import;publicclassBankTest{publicstaticvoidmain(String[]args){Mapmap=newHashMap();map.put("用户1",newBank("1","张三",Math.random()*100000));map.put("用户2",newBank("2","李四",Math.random()*100000));map.put("用户3",newBank("3","王五",Math.random()*100000));map.put("用户4",newBank("4","赵六",Math.random()*100000));SetkeySet=map.keySet();Iteratorit=keySet.iterator();doublesum=0;while(it.hasNext()){Objectkey=it.next();Banka=map.get(key);"账户"+a.getId()+"的余额:"+a.getMoney());//计算所有帐户的余额之和sum+=a.getMoney();}"所有账户余额:"+sum);}}5、从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。packagezuoye;import;import;import;publicclassTest5{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);Listlist=newArrayList();Stringstr=input.nextLine();String[]arr=str.split("");Stringtmp;for(inti=0;ifor(intj=i+1;jif(arr[i].compareToIgnoreCase(arr[j])>0){str=arr[j];arr[j]=arr[j+1];arr[j+1]=str;}}}for(Stringa:arr){list.add(a);J}}}IO流1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:路径名:c:\temp\def.txtpackage作业;import;publicclassTest1{publicstaticvoidmain(String[]args){Filefile=newFile(新建文件夹");String[]files=file.list();for(Stringo:files){"文件名:"+o);"路径:"+file.getAbsolutePath()+"\\"+o);"");}}}2、编写一个java程序实现文件复制功能,要求将d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。package作业;import;import;import;/***文件的拷贝*/publicclassTest2{publicstaticvoidmain(String[]args){Filefile=newFile("file\\abc.txt");Filefile_back=newFile("file\\abc_back.txt");try(FileInputStreamfi=newFileInputStream(file);FileOutputStreamfo=newFileOutputStream(file_back);){intb=-1;while((b=fi.read())!=-1){fo.write(b);}}catch(Exceptione){e.printStackTrace();}"拷貝成功!");}}3、创建c:/test.txt文件并在其中输入"helloworld"创建一个输入流读取该文件中的文本并且把小写的l变成大写L再利用输出流写入到d:\test.txt中实现步骤:1.在本地硬盘C盘下创建一个文件test.txt2.创建一个包含main()方法的类,并在main中编写代码3.运行代码并且测试结果实现过滤器的功能效果显示:package作业;import;import;import;import;import;import;/***替换文件中的小写字母l为大写字母L*/publicclassTest3{publicstaticvoidmain(String[]args){FileReaderf;Strings="";BufferedReaderbr=null;try{f=newFileReader("F:\\test.txt");br=newBufferedReader(f);s=br.readLine();"源文件"+s);s=s.replace("l","L");}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{try{br.close();}catch(IOExceptione){e.printStackTrace();}}BufferedWriterbw=null;Jtry{FileWriterfw=newFileWriter("F:\\test.txt");bw=newBufferedWriter(fw);bw.write(s);}catch(IOExceptione){e.printStackTrace();}finally{try{bw.close();}catch(IOExceptione){e.printStackTrace();}}}}4、在程序中创建一个Student类型的对象,并把对象信息保存到d:/io/student.txt文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:package作业;import;publicclassStudent{privateintid;privateprivatepublicStringname;Datebirth;Student(){super();}publicStudent(intid,Stringname,Datebirth){super();this.id=id;this.name=name;this.birth=birth;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicDategetBirth(){returnbirth;}publicvoidsetBirth(Datebirth){this.birth=birth;}@OverridepublicStringtoString(){return"Student[id="+id+",name="+name+",birth="+birth+"]";}}publicclassSerializationExercise{publicstaticvoidmain(Stringargs[]){Students1=newStudent("1","zhangsan","1999-11-11");try{FileOutputStreamfos=newFileOutputStream("d:\\io\\student.txt");ObjectOutputStreamoos=newObjectOutputStream(fos);"对象序列化...");oos.writeObject(s1);oos.flush();oos.close();}catch(FileNotFoundExceptione1){J}catch(IOExceptione2){J}try{FileInputStreamfis=newFileInputStream("d:\\io\\student.txt");ObjectInputStreamois=newOb
if(a.indexOf(b,i)!
=-1)
>
集合类
1、请使用LinkedList来模拟一个队列(先进先出的特性):
1)拥有放入对象的方法voidput(Objecto)
2)取出对象的方法Objectget()
3)判断队列当中是否为空的方法booleanisEmpty();并且,编写测试代码,验证你的
队列是否正确。
publicclassLinkeds{
Listl;
Linkeds(){
l=newLinkedList();
publicvoidput(Objecto){
l.add(o);
publicObjectget(){
Objecto=l.get(0);
l.remove(0);
returno;
publicbooleanisEmpty(){
if(l.isEmpty()){
//定义一个空队列
Linkedsl=newLinkeds();
//往队列中放入对象
l.put("Tom1");
l.put("John2");
l.put("Mary3");
//如果队列不为空,依次输出队列中的元素while(!
l.isEmpty()){
2、假设顺序列表ArrayList中存储的元素是整型数字1~5,遍历每个元素,将每个元素顺序输出。
publicclassTest1{
Listlist=newArrayList<>();
Numbern1=newNumber
(1);
Numbern2=newNumber
(2);
Numbern3=newNumber(3);
Numbern4=newNumber(4);
Numbern5=newNumber(5);
list.add(n1);
list.add(n2);
list.add(n3);
list.add(n4);
list.add(n5);
Iteratorit=list.iterator();
while(it.hasNext())
3、在一个歹U表中存储以下元素:
apple,grape,banana,pear
1)返回集合中的最大的和最小的元素
2)将集合进行排序,并将排序后的结果打印在控制台上
publicclassFruit{
privateStringfruit;
publicFruit()
publicFruit(Stringfruit)
this.fruit=fruit;
publicStringgetFruit(){
returnfruit;
publicvoidsetFruit(Stringfruit){
publicclassFruitTest{
Listfruit=Arrays.asList("applegradebananapear".split(""));
"最大值是:
"+Collections.max(fruit));
"最小值是:
"+Collections.min(fruit));
(其中储户的主要信
4、编写一个程序,创建一个HashMap对象,用于存储银行储户的信息息有储户的ID,姓名和余额)。
另外,计算并显示其中某个储户的当前余额。
publicclass
Bank{
private
String
id;
name;
double
money
public
Bank()
publicBank(Stringid,Stringname,doublemoney)
this.id=id;
this.name=name;
this.money=money;
publicStringgetId(){
returnid;
publicvoidsetId(Stringid){this.id=id;
}publicStringgetName(){returnname;
}publicvoidsetName(Stringname){
publicdoublegetMoney(){
returnmoney;
publicvoidsetMoney(doublemoney){
}packagezuoye;import;import;import;import;
publicclassBankTest{
Mapmap=newHashMap();map.put("用户1",newBank("1","张三",Math.random()*100000));map.put("用户2",newBank("2","李四",Math.random()*100000));map.put("用户3",newBank("3","王五",Math.random()*100000));map.put("用户4",newBank("4","赵六",Math.random()*100000));SetkeySet=map.keySet();
Iteratorit=keySet.iterator();doublesum=0;while(it.hasNext()){Objectkey=it.next();Banka=map.get(key);
"账户"+a.getId()+"的余额:
"+a.getMoney());
//计算所有帐户的余额之和
sum+=a.getMoney();
"所有账户余额:
"+sum);
5、从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。
publicclassTest5{
Listlist=newArrayList();
Stringstr=input.nextLine();
String[]arr=str.split("");
Stringtmp;
for(inti=0;ifor(intj=i+1;jif(arr[i].compareToIgnoreCase(arr[j])>0){str=arr[j];arr[j]=arr[j+1];arr[j+1]=str;}}}for(Stringa:arr){list.add(a);J}}}IO流1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:路径名:c:\temp\def.txtpackage作业;import;publicclassTest1{publicstaticvoidmain(String[]args){Filefile=newFile(新建文件夹");String[]files=file.list();for(Stringo:files){"文件名:"+o);"路径:"+file.getAbsolutePath()+"\\"+o);"");}}}2、编写一个java程序实现文件复制功能,要求将d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。package作业;import;import;import;/***文件的拷贝*/publicclassTest2{publicstaticvoidmain(String[]args){Filefile=newFile("file\\abc.txt");Filefile_back=newFile("file\\abc_back.txt");try(FileInputStreamfi=newFileInputStream(file);FileOutputStreamfo=newFileOutputStream(file_back);){intb=-1;while((b=fi.read())!=-1){fo.write(b);}}catch(Exceptione){e.printStackTrace();}"拷貝成功!");}}3、创建c:/test.txt文件并在其中输入"helloworld"创建一个输入流读取该文件中的文本并且把小写的l变成大写L再利用输出流写入到d:\test.txt中实现步骤:1.在本地硬盘C盘下创建一个文件test.txt2.创建一个包含main()方法的类,并在main中编写代码3.运行代码并且测试结果实现过滤器的功能效果显示:package作业;import;import;import;import;import;import;/***替换文件中的小写字母l为大写字母L*/publicclassTest3{publicstaticvoidmain(String[]args){FileReaderf;Strings="";BufferedReaderbr=null;try{f=newFileReader("F:\\test.txt");br=newBufferedReader(f);s=br.readLine();"源文件"+s);s=s.replace("l","L");}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{try{br.close();}catch(IOExceptione){e.printStackTrace();}}BufferedWriterbw=null;Jtry{FileWriterfw=newFileWriter("F:\\test.txt");bw=newBufferedWriter(fw);bw.write(s);}catch(IOExceptione){e.printStackTrace();}finally{try{bw.close();}catch(IOExceptione){e.printStackTrace();}}}}4、在程序中创建一个Student类型的对象,并把对象信息保存到d:/io/student.txt文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:package作业;import;publicclassStudent{privateintid;privateprivatepublicStringname;Datebirth;Student(){super();}publicStudent(intid,Stringname,Datebirth){super();this.id=id;this.name=name;this.birth=birth;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicDategetBirth(){returnbirth;}publicvoidsetBirth(Datebirth){this.birth=birth;}@OverridepublicStringtoString(){return"Student[id="+id+",name="+name+",birth="+birth+"]";}}publicclassSerializationExercise{publicstaticvoidmain(Stringargs[]){Students1=newStudent("1","zhangsan","1999-11-11");try{FileOutputStreamfos=newFileOutputStream("d:\\io\\student.txt");ObjectOutputStreamoos=newObjectOutputStream(fos);"对象序列化...");oos.writeObject(s1);oos.flush();oos.close();}catch(FileNotFoundExceptione1){J}catch(IOExceptione2){J}try{FileInputStreamfis=newFileInputStream("d:\\io\\student.txt");ObjectInputStreamois=newOb
for(intj=i+1;jif(arr[i].compareToIgnoreCase(arr[j])>0){str=arr[j];arr[j]=arr[j+1];arr[j+1]=str;}}}for(Stringa:arr){list.add(a);J}}}IO流1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:路径名:c:\temp\def.txtpackage作业;import;publicclassTest1{publicstaticvoidmain(String[]args){Filefile=newFile(新建文件夹");String[]files=file.list();for(Stringo:files){"文件名:"+o);"路径:"+file.getAbsolutePath()+"\\"+o);"");}}}2、编写一个java程序实现文件复制功能,要求将d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。package作业;import;import;import;/***文件的拷贝*/publicclassTest2{publicstaticvoidmain(String[]args){Filefile=newFile("file\\abc.txt");Filefile_back=newFile("file\\abc_back.txt");try(FileInputStreamfi=newFileInputStream(file);FileOutputStreamfo=newFileOutputStream(file_back);){intb=-1;while((b=fi.read())!=-1){fo.write(b);}}catch(Exceptione){e.printStackTrace();}"拷貝成功!");}}3、创建c:/test.txt文件并在其中输入"helloworld"创建一个输入流读取该文件中的文本并且把小写的l变成大写L再利用输出流写入到d:\test.txt中实现步骤:1.在本地硬盘C盘下创建一个文件test.txt2.创建一个包含main()方法的类,并在main中编写代码3.运行代码并且测试结果实现过滤器的功能效果显示:package作业;import;import;import;import;import;import;/***替换文件中的小写字母l为大写字母L*/publicclassTest3{publicstaticvoidmain(String[]args){FileReaderf;Strings="";BufferedReaderbr=null;try{f=newFileReader("F:\\test.txt");br=newBufferedReader(f);s=br.readLine();"源文件"+s);s=s.replace("l","L");}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{try{br.close();}catch(IOExceptione){e.printStackTrace();}}BufferedWriterbw=null;Jtry{FileWriterfw=newFileWriter("F:\\test.txt");bw=newBufferedWriter(fw);bw.write(s);}catch(IOExceptione){e.printStackTrace();}finally{try{bw.close();}catch(IOExceptione){e.printStackTrace();}}}}4、在程序中创建一个Student类型的对象,并把对象信息保存到d:/io/student.txt文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:package作业;import;publicclassStudent{privateintid;privateprivatepublicStringname;Datebirth;Student(){super();}publicStudent(intid,Stringname,Datebirth){super();this.id=id;this.name=name;this.birth=birth;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicDategetBirth(){returnbirth;}publicvoidsetBirth(Datebirth){this.birth=birth;}@OverridepublicStringtoString(){return"Student[id="+id+",name="+name+",birth="+birth+"]";}}publicclassSerializationExercise{publicstaticvoidmain(Stringargs[]){Students1=newStudent("1","zhangsan","1999-11-11");try{FileOutputStreamfos=newFileOutputStream("d:\\io\\student.txt");ObjectOutputStreamoos=newObjectOutputStream(fos);"对象序列化...");oos.writeObject(s1);oos.flush();oos.close();}catch(FileNotFoundExceptione1){J}catch(IOExceptione2){J}try{FileInputStreamfis=newFileInputStream("d:\\io\\student.txt");ObjectInputStreamois=newOb
if(arr[i].compareToIgnoreCase(arr[j])>0){
str=arr[j];
arr[j]=arr[j+1];
arr[j+1]=str;
for(Stringa:
arr){
list.add(a);
J
IO流
1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:
路径名:
c:
\temp\def.txt
package作业;
Filefile=newFile(新建文件夹");
String[]files=file.list();
for(Stringo:
files){
"文件名:
"+o);
"路径:
"+file.getAbsolutePath()+"\\"+o);
2、编写一个java程序实现文件复制功能,要求将d:
/io/copysrc.doc中的内容复制到d:
/io/copydes.doc中。
*文件的拷贝
Filefile=newFile("file\\abc.txt");
Filefile_back=newFile("file\\abc_back.txt");
try(FileInputStreamfi=newFileInputStream(file);FileOutputStreamfo=newFileOutputStream(file_back);){
intb=-1;
while((b=fi.read())!
=-1){fo.write(b);
}catch(Exceptione){e.printStackTrace();
"拷貝成功!
");
3、创建c:
/test.txt文件并在其中输入"helloworld"创建一个输入流读取该文件中的文本
并且把小写的l变成大写L再利用输出流写入到d:
\test.txt中实现步骤:
1.在本地硬盘C盘下创建一个文件test.txt
2.创建一个包含main()方法的类,并在main中编写代码3.运行代码并且测试结果
实现过滤器的功能
效果显示:
package作业;import;import;import;import;import;import;
*替换文件中的小写字母l为大写字母L
*/publicclassTest3{
publicstaticvoidmain(String[]args){FileReaderf;
Strings="";BufferedReaderbr=null;try{
f=newFileReader("F:
\\test.txt");br=newBufferedReader(f);s=br.readLine();
"源文件"+s);
s=s.replace("l","L");
}catch(FileNotFoundExceptione){e.printStackTrace();
}catch(IOExceptione){e.printStackTrace();
}finally{try{
br.close();
BufferedWriterbw=null;
try{
FileWriterfw=newFileWriter("F:
\\test.txt");bw=newBufferedWriter(fw);bw.write(s);
bw.close();
}catch(IOExceptione){
e.printStackTrace();
4、在程序中创建一个Student类型的对象,并把对象信息保存到d:
/io/student.txt文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:
publicclassStudent{
intid;
privateprivatepublic
Stringname;Datebirth;
Student(){
super();
publicStudent(intid,Stringname,Datebirth){super();
this.birth=birth;
publicintgetId(){
publicvoidsetId(intid){
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
publicDategetBirth(){
returnbirth;
publicvoidsetBirth(Datebirth){
@Override
publicStringtoString(){
return"Student[id="+id+",name="+name+",birth="+birth+"]";
publicclassSerializationExercise{
publicstaticvoidmain(Stringargs[]){
Students1=newStudent("1","zhangsan","1999-11-11");try{
FileOutputStreamfos=newFileOutputStream("d:
\\io\\student.txt");ObjectOutputStreamoos=newObjectOutputStream(fos);"对象序列化...");
oos.writeObject(s1);
oos.flush();
oos.close();
}catch(FileNotFoundExceptione1){
}catch(IOExceptione2){
}try{
FileInputStreamfis=newFileInputStream("d:
\\io\\student.txt");ObjectInputStreamois=newOb
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2