ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:22.91KB ,
资源ID:4598363      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4598363.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JAVA实验5流.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JAVA实验5流.docx

1、JAVA实验5流实验5 流1. 编写程序,要求:用户在键盘每输入一行文本,程序将这段文本显示在控制台中。当用户输入的一行文本是“exit”(不区分大小写)时,程序将用户所有输入的文本都写入到文件log.txt中,并退出。(要求:控制台输入通过流封装System.in获取,不要使用Scanner)package shiyanwu1;import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class test1 / 获得系统换行符 private stat

2、ic final String LINE_SEP = System.getProperty(line.separator); public static void main(String args) throws Exception try (BufferedReader in = new BufferedReader( new InputStreamReader(System.in) String line; StringBuilder sBuilder = new StringBuilder(); while (true) line = in.readLine(); / 读入一行字符串 i

3、f (line = null | exit.equals(line)|EXIT.equals(line) break; sBuilder.append(line).append(LINE_SEP); / 将 sBuilder 中的数据写入 log.txt try (PrintWriter writer = new PrintWriter(D:/java examples/my java/src/shiyanwu1/show.txt) writer.print(sBuilder.toString(); 2. 查看File类的API文档,使用该类实现一个类FileList,它提供两个静态方法:1)

4、printContentsInOneDirectory:能够将输入参数path所指定的本地磁盘路径下的所有目录和文件的名称(指明是目录还是文件,格式见下图)打印出来;2)readFileAndDirectory:能够将输入参数path所指定的本地磁盘路径下的所有目录(包含子目录)和文件的名称(指明是目录还是文件,格式见下图)以层次化结构打印出来。例如,某个目录下面有子目录a和文件Teacher.class,目录a下面有子目录b(下面有文件Teacher.java)和c(下面有文件Test.java和Test.class)以及文件1.txt,将该目录对应的路径作为输入参数调用该方法,程序的输出如

5、下图所示。package shiyanwu2;import java.io.File; public class test2 Public static void printContentsInOneDirectory(String path) File file = new File(path); if (!file.exists() throw new RuntimeException(String.format(文件 %s 不存在!, path); File childFiles = file.listFiles(); for (File childFile : childFiles)

6、if (childFile.isFile() System.out.format(文件 %sn, childFile.getName(); else System.out.format(目录 %sn, childFile.getName(); /* * * param path 目录路径 * param indent 当前缩进(即输出 - ) 的个数 */ public static void readFileAndDirectory(String path, int indent) File file = new File(path); if (!file.exists() throw ne

7、w RuntimeException(String.format(文件 %s 不存在!, path); File childFiles = file.listFiles(); for (File childFile : childFiles) for (int i = 0; i indent; +i) System.out.print(-); if (childFile.isFile() System.out.format(文件 %sn, childFile.getName(); else System.out.format(目录 %sn, childFile.getName(); readF

8、ileAndDirectory(childFile.getAbsolutePath(), indent + 2); public static void main(String args) throws Exception System.out.println(测试 printContentsInOneDirectory 方法:); printContentsInOneDirectory(some_dir); System.out.println(n测试 readFileAndDirectory 方法:); readFileAndDirectory(some_dir, 0); 3. 假设某个餐

9、馆平时使用:1)文本文件(orders.txt)记录顾客的点菜信息,每桌顾客的点菜记录占一行。每行顾客点菜信息的记录格式是“菜名:数量,菜名:数量,菜名:数量”。例如:“烤鸭:1,土豆丝:2,烤鱼:1”。2)文本文件(dishes.txt)记录每种菜的具体价格,每种菜及其价格占一行,记录格式为“菜名:价格“。例如:“烤鸭:169”。编写一个程序,能够计算出orders.txt中所有顾客消费的总价格。package shiyanwu3;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;

10、import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class order public static void main(String args) order first = new order(); List orderDatas = first.readFile(D:/java examples/my java/src/

11、shiyanwu3/orders.txt); List dishesDatas = first.readFile(D:/java examples/my java/src/shiyanwu3/dishes.txt); Map orderDetail = first.resolveOrderDatas(orderDatas); Map dishesDetail = firs .resolveOrderDatas(dishesDatas); String dishesName = null; int dishesCount = 0; int totalPrice = 0; for (Map.Ent

12、ry e : orderDetail.entrySet() dishesName = e.getKey(); dishesCount = e.getValue(); totalPrice += dishesDetail.get(dishesName) * dishesCount; System.err.println(dishesName+总消费为:+totalPrice); private List readFile(String fileName) if (fileName != null & !.equals(fileName) File file = null; file = new

13、File(fileName); if (file.exists() List datas = new ArrayList(); try InputStream is = new FileInputStream(file); BufferedReader br = new BufferedReader( new InputStreamReader(is,gb2312); String str = null; while (true) str = br.readLine(); if (str != null) datas.add(str); else break; br.close(); catc

14、h (Exception e) return datas; return null; private Map resolveOrderDatas(List datas) String temp1 = null, temp2 = null; String detailStr = null; Map orderDetail = new HashMap(); for (int i = 0; i datas.size(); i+) temp1 = datas.get(i).split(,); for (int j = 0; j temp1.length; j+) temp2 = temp1j.spli

15、t(:); if (temp2.length = 2) if (orderDetail.get(temp20) != null) orderDetail.put(temp20, Integer.parseInt(temp21) + orderDetail.get(temp20); else orderDetail.put(temp20, Integer.parseInt(temp21); return orderDetail; private Map resolveDishesDatas(List datas) Map dishesDetail = new HashMap(); String

16、temp = null; for (int i = 0; i datas.size(); i+) temp = datas.get(i).split(:); if (temp.length = 2) dishesDetail.put(temp0, Integer.parseInt(temp1); return dishesDetail; 4. 设计学生类Student,属性:学号(整型);姓名(字符串),选修课程(名称)及课程成绩(整型)。编写一个控制台程序,能够实现Student信息的保存、读取。具体要求:(1)提供Student信息的保存功能:通过控制台输入若干个学生的学号、姓名以及每个学

17、生所修课程的课程名和成绩,将其信息保存到data.dat中;(2)数据读取显示:能够从data.dat文件中读取学生及其课程成绩并显示于控制台。package shiyanwu4;public class Student private int number; private String name; private String courseName; private int score; public int getNumber() return number; public void setNumber(int number) this.number = number; public S

18、tring getName() return name; public void setName(String name) this.name = name; public String getCourseName() return courseName; public void setCourseName(String courseName) this.courseName = courseName; public int getScore() return score; public void setScore(int score) this.score = score; package

19、shiyanwu4;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.

20、Scanner;public class StudentMgr public static final String studentDat = E:/data.dat; public static void main(String args) showMenu(); Scanner s = null; s = new Scanner(System.in); String code = null; Student student; List datas = new ArrayList(); List savedStudents = readStudentDat(studentDat); whil

21、e (true) code = s.next(); if (#4.equalsIgnoreCase(code) System.err.println(程序已退出); break; else if (#1.equalsIgnoreCase(code) String tmpStr = null; int tmpInt; while (true) System.out.print(学生学号:); tmpInt = s.nextInt(); student = new Student(); student.setNumber(tmpInt); System.out.print(学生姓名:); tmpS

22、tr = 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(tmpS

23、tr) break; if (exit.equalsIgnoreCase(tmpStr) showMenu(); continue; else if (#3.equalsIgnoreCase(code) try if(datas.size() 0 ) saveStudents(datas); else System.err.println(无可保存的学生信息); catch (IOException e) System.err.println(保存学生信息异常); e.printStackTrace(); else if (#2.equalsIgnoreCase(code) List stud

24、ents = readStudentDat(studentDat); if(students = null | students.size() = 0) System.err.println(暂无学生信息); showMenu(); else System.err.println(已有学生人数:+students.size(); for(int i=0;istudents.size();i+) /这里输出学生信息 else System.err.println(无法识别的菜单); showMenu(); public static List readStudentDat(String file

25、Name) if (fileName != null & !.equals(fileName) File file = null; file = new File(fileName); Student student = null; if (file.exists() List datas = new ArrayList(); try InputStream is = new FileInputStream(file); BufferedReader br = new BufferedReader( new InputStreamReader(is, gb2312); String str =

26、 null; String infos = null; while (true) str = br.readLine(); if (str != null) student = new Student(); str = br.readLine(); infos = str.split(#); student.setNumber(Integer.parseInt(infos0); student.setName(infos1); student.setCourseName(infos2); student.setScore(Integer.parseInt(infos3); datas.add(student); else break; br.close(); catch (Exception e) e.printStackTrace(); return datas; return null; public static void saveStudents(List students) throws IOException File

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

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