JAVA实验三.docx
《JAVA实验三.docx》由会员分享,可在线阅读,更多相关《JAVA实验三.docx(17页珍藏版)》请在冰点文库上搜索。
JAVA实验三
浙江理工大学
《Java程序设计》
实验报告
2015~2016学年第2学期
学院
班级
姓名
学号
任课教师
计算机科学与技术专业
2016年5月
《Java程序设计》实验报告三
实验名称:
异常处理&JavaIO流
实验地点:
所使用的工具软件及环境:
Myeclipse2015
Windows8.1
一、实验目的:
1.了解掌握用户自定义异常处理相关操作(定义——抛出——捕获)。
2.掌握Java中字符串的操作,掌握文件流的操作。
二、实验内容:
(1)用户自行定义一个异常,编程创建并抛出某个异常类的实例,运行该程序并观察执行结果。
例如:
用户密码的合法化验证,要求密码由4到6个数字组成,若长度不落在这个范围或不是由数字组成,抛出自己的异常。
(2)编写程序,接受用户输入的5个浮点数据和一个文件目录名,将这5个数据保存在文件中,再从该文件中读取出来并且进行从大到小排序,然后再一次追加保存到文件中。
(3)一家小型超市的店主,需要查询、输入、修改任何一件商品的品名、价格、库存量信息。
商品信息存储在文件中,每件商品的标志为其记录号。
三、源代码
1、
packagethree;
importjava.util.Scanner;
publicclassExceptionText{
classPasswordExceptionextendsException{
publicPasswordException(Stringmsg){
super(msg);
}
}
publicStringPasswordCompare()throwsPasswordException{
Scannerscanner=newScanner(System.in);
System.out.println("请输入密码:
");
Stringstr=scanner.nextLine();/*只有当scan有下一个的时候才会进来,而且它取得是下一个*/
try{
if(str.length()<4||str.length()>6){
PasswordExceptione;
e=newPasswordException("密码必须由4到6个数字组成!
");
throwe;
}
}
catch(NumberFormatExceptione){
System.out.println(e.toString());/*处理错误*/
}
try{
if(!
str.matches("\\d+")){/*输入的密码不是数字*/
PasswordExceptione;
e=newPasswordException("密码必须由4到6个数字组成!
");
throwe;
}
}
catch(NumberFormatExceptione){
System.out.println(e.toString());
}
returnstr;
}
publicstaticvoidmain(Stringargs[]){
try{
ExceptionTexttest;
test=newExceptionText();
Stringpassword=test.PasswordCompare();
System.out.println("您的密码为:
"+password);
}
catch(PasswordExceptione){
e.printStackTrace();
}
}
}
2、
packagethree;
importjava.util.Scanner;
importjava.util.Arrays;
importjava.io.*;
publicclassTextfile{
publicstaticvoidmain(Stringargs[]){
Scanners;
s=newScanner(System.in);
float[]a;
a=newfloat[6];
for(inti=0;i<5;i++){
a[i]=s.nextFloat();
}
Stringstr;
str=s.next();
Filef;
f=newFile(str);
try{
f.createNewFile();
}
catch(IOExceptione){
e.printStackTrace();
}
try{
FileWriterfw;
fw=newFileWriter(f);
for(inti=0;i<5;i++){
fw.write(String.valueOf(a[i])+"\r\n");
}
fw.close();
}
catch(IOExceptione){
e.printStackTrace();
}
try{
FileReaderfr;
fr=newFileReader(f);
BufferedReaderbr;
br=newBufferedReader(fr);
float[]b;
b=newfloat[5];
for(inti=0;i<5;i++){
b[i]=Float.parseFloat(br.readLine());
}
Arrays.sort(b);
try{
FileWriterfw=newFileWriter(f,true);
for(inti=0;i<5;i++){
fw.write(String.valueOf(b[i])+"\r\n");
}
fw.close();
}
catch(IOExceptione){
e.printStackTrace();
}
}
catch(FileNotFoundExceptione){
e.printStackTrace();
}
catch(IOExceptione){
e.printStackTrace();
}
}
}
3、
packagethree;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Scanner;
classProducts
{
privateintid;
privateStringname;
privatedoubleprice;
privateintstores;
publicProducts(intid,Stringname,doubleprice,intstores){
super();
this.id=id;
this.name=name;
this.price=price;
this.stores=stores;
}
publicProducts(){}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}
publicintgetStores(){
returnstores;
}
publicvoidsetStores(intstores){
this.stores=stores;
}
publicStringtoString(){
return"Products[id="+id+",name="+name+",price="+price+",stores="+stores+"]";
}}
classManager
{
Products[]p=newProducts[100];
Filefile1=newFile("store.txt");
publicManager(){
super();
for(inti=0;i<100;i++)
{
p[i]=newProducts();
}
}
publicvoidinit()
{
FileInputStreamfis=null;
Stringstr=null;
try{
fis=newFileInputStream(file1);
intlength=fis.available();
System.out.println(length);
byte[]b=newbyte[length];
fis.read(b);
str=newString(b,"UTF-8");
}
catch(IOExceptione){
e.printStackTrace();
}
finally
{
try{
fis.close();
}
catch(IOExceptione){
e.printStackTrace();
}
}
String[]strs=str.split("#");
for(inti=0,j=0;ip[j].setId(Integer.parseInt(strs[i]));
p[j].setName(strs[i+1]);
p[j].setPrice(Double.valueOf(strs[i+2]));
p[j].setStores(Integer.parseInt(strs[i+3]));
}}
publicvoidquery()
{
System.out.print("商品编号\t");
System.out.print("商品名字\t");
System.out.print("商品价格\t");
System.out.println("商品库存\t");
for(inti=0;i
{
if(p[i].getId()!
=0)
{
System.out.print(p[i].getId()+"\t");
System.out.print(p[i].getName()+"\t");
System.out.print(p[i].getPrice()+"\t");
System.out.println(p[i].getStores()+"\t");
}
}
}
publicvoidchange()
{
query();
intid1;Stringname;doubleprice;intstores;
System.out.println("输入修改的商品编号:
");
Scanners;
s=newScanner(System.in);
id1=s.nextInt();
System.out.println("输入修改后的信息:
");
System.out.print("商品名字:
");
name=s.next();
System.out.print("商品价格:
");
price=s.nextDouble();
System.out.print("商品库存:
");
stores=s.nextInt();
p[id1-1].setId(id1);
p[id1-1].setName(name);
p[id1-1].setPrice(price);
p[id1-1].setStores(stores);
save();
}
publicvoidsave()
{
FileOutputStreamfos=null;
try{
fos=newFileOutputStream(file1);
}
catch(FileNotFoundExceptione){
e.printStackTrace();
}
Stringstr=newString("");
intid2;Stringname1;doubleprice1;intstores1;
for(inti=0;i
{
if(p[i].getId()!
=0)
{
id2=p[i].getId();
name1=p[i].getName();
price1=p[i].getPrice();
stores1=p[i].getStores();
str=str+String.valueOf(id2)+"#"+name1+"#"+String.valueOf(price1)+"#"+String.valueOf(stores1)+"#";
}
}
byteb[]=newbyte[str.length()+1];
b=str.getBytes();
try{
fos.write(b);
fos.close();
}
catch(IOExceptione){
e.printStackTrace();
}
}
publicvoidadd()
{
inti,j=0;
for(i=0;i<100;i++)
{
if(p[i].getId()!
=0)
j++;
}
intid=j+1;
System.out.println("商店商品详情:
");
query();
System.out.println("输入添加的商品");
Scanners=newScanner(System.in);
Stringname;
doubleprice;
intstore;
System.out.println("增加商品的信息:
");
System.out.print("商品名字:
");
name=s.next();
System.out.print("商品价格:
");
price=s.nextDouble();
System.out.print("商品库存:
");
store=s.nextInt();
p[id-1].setId(id);
p[id-1].setName(name);
p[id-1].setPrice(price);
p[id-1].setStores(store);
save();
}
}
publicclassstore
{
publicstaticvoidmain(String[]args){
ManagerM;
M=newManager();
Scanners;
s=newScanner(System.in);
intn=0;
M.init();
while(n!
=4)
{
System.out.println("选择要进行的操作");
System.out.println("1:
查询商品信息2:
添加商品信息3:
修改商品信息4:
退出");
n=s.nextInt();
switch(n)
{
case1:
M.query();
break;
case2:
M.add();
break;
case3:
M.change();
break;
case4:
break;
}
System.out.println();
}
}
}
四、实验结果测试:
1、
2、
3、