JAVA实验5流.docx

上传人:b****6 文档编号:4598363 上传时间:2022-12-07 格式:DOCX 页数:17 大小:22.91KB
下载 相关 举报
JAVA实验5流.docx_第1页
第1页 / 共17页
JAVA实验5流.docx_第2页
第2页 / 共17页
JAVA实验5流.docx_第3页
第3页 / 共17页
JAVA实验5流.docx_第4页
第4页 / 共17页
JAVA实验5流.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

JAVA实验5流.docx

《JAVA实验5流.docx》由会员分享,可在线阅读,更多相关《JAVA实验5流.docx(17页珍藏版)》请在冰豆网上搜索。

JAVA实验5流.docx

JAVA实验5流

实验5流

1.编写程序,要求:

用户在键盘每输入一行文本,程序将这段文本显示在控制台中。

当用户输入的一行文本是“exit”(不区分大小写)时,程序将用户所有输入的文本都写入到文件log.txt中,并退出。

(要求:

控制台输入通过流封装System.in获取,不要使用Scanner)

packageshiyanwu1;

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

importjava.io.PrintWriter;

publicclasstest1{

//获得系统换行符

privatestaticfinalStringLINE_SEP=System.getProperty("line.separator");

publicstaticvoidmain(String[]args)throwsException{

try(BufferedReaderin=newBufferedReader(

newInputStreamReader(System.in))){

Stringline;

StringBuildersBuilder=newStringBuilder();

while(true){

line=in.readLine();//读入一行字符串

if(line==null||"exit".equals(line)||"EXIT".equals(line)){

break;

}

sBuilder.append(line).append(LINE_SEP);

}

//将sBuilder中的数据写入log.txt

try(PrintWriterwriter=newPrintWriter("D:

/javaexamples/myjava/src/shiyanwu1/show.txt")){

writer.print(sBuilder.toString());

}

}

}

}

2.查看File类的API文档,使用该类实现一个类FileList,它提供两个静态方法:

1)printContentsInOneDirectory:

能够将输入参数path所指定的本地磁盘路径下的所有目录和文件的名称(指明是目录还是文件,格式见下图)打印出来;2)readFileAndDirectory:

能够将输入参数path所指定的本地磁盘路径下的所有目录(包含子目录)和文件的名称(指明是目录还是文件,格式见下图)以层次化结构打印出来。

例如,某个目录下面有子目录a和文件Teacher.class,目录a下面有子目录b(下面有文件Teacher.java)和c(下面有文件Test.java和Test.class)以及文件1.txt,将该目录对应的路径作为输入参数调用该方法,程序的输出如下图所示。

packageshiyanwu2;

importjava.io.File;

publicclasstest2{

PublicstaticvoidprintContentsInOneDirectory(Stringpath){

Filefile=newFile(path);

if(!

file.exists()){

thrownewRuntimeException(String.format("文件%s不存在!

",path));

}

File[]childFiles=file.listFiles();

for(FilechildFile:

childFiles){

if(childFile.isFile()){

System.out.format("[文件]%s\n",childFile.getName());

}else{

System.out.format("[目录]%s\n",childFile.getName());

}

}

}

/**

*

*@parampath目录路径

*@paramindent当前缩进(即输出-)的个数

*/

publicstaticvoidreadFileAndDirectory(Stringpath,intindent){

Filefile=newFile(path);

if(!

file.exists()){

thrownewRuntimeException(String.format("文件%s不存在!

",path));

}

File[]childFiles=file.listFiles();

for(FilechildFile:

childFiles){

for(inti=0;i

System.out.print("-");

}

if(childFile.isFile()){

System.out.format("[文件]%s\n",childFile.getName());

}else{

System.out.format("[目录]%s\n",childFile.getName());

readFileAndDirectory(childFile.getAbsolutePath(),indent+2);

}

}

}

publicstaticvoidmain(String[]args)throwsException{

System.out.println("测试printContentsInOneDirectory方法:

");

printContentsInOneDirectory("some_dir");

System.out.println("\n测试readFileAndDirectory方法:

");

readFileAndDirectory("some_dir",0);

}

}

3.假设某个餐馆平时使用:

1)文本文件(orders.txt)记录顾客的点菜信息,每桌顾客的点菜记录占一行。

每行顾客点菜信息的记录格式是“菜名:

数量,菜名:

数量,…菜名:

数量”。

例如:

“烤鸭:

1,土豆丝:

2,烤鱼:

1”。

2)文本文件(dishes.txt)记录每种菜的具体价格,每种菜及其价格占一行,记录格式为“菜名:

价格“。

例如:

“烤鸭:

169”。

编写一个程序,能够计算出orders.txt中所有顾客消费的总价格。

packageshiyanwu3;

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

publicclassorder{

publicstaticvoidmain(String[]args){

orderfirst=neworder();

ListorderDatas=first.readFile("D:

/javaexamples/myjava/src/shiyanwu3/orders.txt");

ListdishesDatas=first.readFile("D:

/javaexamples/myjava/src/shiyanwu3/dishes.txt");

MaporderDetail=first.resolveOrderDatas(orderDatas);

MapdishesDetail=firs.resolveOrderDatas(dishesDatas);

StringdishesName=null;

intdishesCount=0;

inttotalPrice=0;

for(Map.Entrye:

orderDetail.entrySet()){

dishesName=e.getKey();

dishesCount=e.getValue();

totalPrice+=dishesDetail.get(dishesName)*dishesCount;

System.err.println(dishesName+"总消费为:

"+totalPrice);

}

}

privateListreadFile(StringfileName){

if(fileName!

=null&&!

"".equals(fileName)){

Filefile=null;

file=newFile(fileName);

if(file.exists()){

Listdatas=newArrayList();

try{

InputStreamis=newFileInputStream(file);

BufferedReaderbr=newBufferedReader(

newInputStreamReader(is,"gb2312"));

Stringstr=null;

while(true){

str=br.readLine();

if(str!

=null){

datas.add(str);

}else{

break;

}

}

br.close();

}catch(Exceptione){

}

returndatas;

}

}

returnnull;

}

privateMapresolveOrderDatas(Listdatas){

Stringtemp1[]=null,temp2[]=null;

StringdetailStr=null;

MaporderDetail=newHashMap<>();

for(inti=0;i

temp1=datas.get(i).split(",");

for(intj=0;j

temp2=temp1[j].split(":

");

if(temp2.length==2){

if(orderDetail.get(temp2[0])!

=null){

orderDetail.put(temp2[0],Integer.parseInt(temp2[1])

+orderDetail.get(temp2[0]));

}else{

orderDetail.put(temp2[0],Integer.parseInt(temp2[1]));

}

}

}

}

returnorderDetail;

}

privateMapresolveDishesDatas(Listdatas){

MapdishesDetail=newHashMap<>();

Stringtemp[]=null;

for(inti=0;i

temp=datas.get(i).split(":

");

if(temp.length==2){

dishesDetail.put(temp[0],Integer.parseInt(temp[1]));

}

}

returndishesDetail;

}

}

4.设计学生类Student,属性:

学号(整型);姓名(字符串),选修课程(名称)及课程成绩(整型)。

编写一个控制台程序,能够实现Student信息的保存、读取。

具体要求:

(1)提供Student信息的保存功能:

通过控制台输入若干个学生的学号、姓名以及每个学生所修课程的课程名和成绩,将其信息保存到data.dat中;

(2)数据读取显示:

能够从data.dat文件中读取学生及其课程成绩并显示于控制台。

packageshiyanwu4;

publicclassStudent{

privateintnumber;

privateStringname;

privateStringcourseName;

privateintscore;

publicintgetNumber(){

returnnumber;

}

publicvoidsetNumber(intnumber){

this.number=number;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetCourseName(){

returncourseName;

}

publicvoidsetCourseName(StringcourseName){

this.courseName=courseName;

}

publicintgetScore(){

returnscore;

}

publicvoidsetScore(intscore){

this.score=score;

}

}

packageshiyanwu4;

importjava.io.BufferedReader;

importjava.io.BufferedWriter;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileWriter;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.util.ArrayList;

importjava.util.List;

importjava.util.Scanner;

publicclassStudentMgr{

publicstaticfinalStringstudentDat="E:

/data.dat";

publicstaticvoidmain(String[]args){

showMenu();

Scanners=null;

s=newScanner(System.in);

Stringcode=null;

Studentstudent;

Listdatas=newArrayList();

ListsavedStudents=readStudentDat(studentDat);

while(true){

code=s.next();

if("#4".equalsIgnoreCase(code)){

System.err.println("程序已退出");

break;

}elseif("#1".equalsIgnoreCase(code)){

StringtmpStr=null;

inttmpInt;

while(true){

System.out.print("学生学号:

");

tmpInt=s.nextInt();

student=newStudent();

student.setNumber(tmpInt);

System.out.print("学生姓名:

");

tmpStr=s.next();

student.setName(tmpStr);

System.out.print("学生课程:

");

tmpStr=s.next();

student.setCourseName(tmpStr);

System.out.print("课程成绩:

");

tmpInt=s.nextInt();

student.setScore(tmpInt);

datas.add(student);

System.out.println("输入exit结束信息录入,输入其他继续录入");

tmpStr=s.next();

if("exit".equalsIgnoreCase(tmpStr)){

break;

}

}

if("exit".equalsIgnoreCase(tmpStr)){

showMenu();

continue;

}

}elseif("#3".equalsIgnoreCase(code)){

try{

if(datas.size()>0){

saveStudents(datas);

}else{

System.err.println("无可保存的学生信息");

}

}catch(IOExceptione){

System.err.println("保存学生信息异常");

e.printStackTrace();

}

}elseif("#2".equalsIgnoreCase(code)){

Liststudents=readStudentDat(studentDat);

if(students==null||students.size()==0){

System.err.println("暂无学生信息");

showMenu();

}else{

System.err.println("已有学生人数:

"+students.size());

for(inti=0;i

//这里输出学生信息

}

}

}else{

System.err.println("无法识别的菜单");

showMenu();

}

}

}

publicstaticListreadStudentDat(StringfileName){

if(fileName!

=null&&!

"".equals(fileName)){

Filefile=null;

file=newFile(fileName);

Studentstudent=null;

if(file.exists()){

Listdatas=newArrayList();

try{

InputStreamis=newFileInputStream(file);

BufferedReaderbr=newBufferedReader(

newInputStreamReader(is,"gb2312"));

Stringstr=null;

String[]infos=null;

while(true){

str=br.readLine();

if(str!

=null){

student=newStudent();

str=br.readLine();

infos=str.split("#");

student.setNumber(Integer.parseInt(infos[0]));

student.setName(infos[1]);

student.setCourseName(infos[2]);

student.setScore(Integer.parseInt(infos[3]));

datas.add(student);

}else{

break;

}

}

br.close();

}catch(Exceptione){

e.printStackTrace();

}

returndatas;

}

}

returnnull;

}

publicstaticvoidsaveStudents(Liststudents)throwsIOException{

File

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1