JAVA实验5 流.docx

上传人:小飞机 文档编号:3159633 上传时间:2023-03-11 格式:DOCX 页数:16 大小:40.90KB
返回 下载 相关 举报
JAVA实验5 流.docx_第1页
第1页 / 共16页
JAVA实验5 流.docx_第2页
第2页 / 共16页
JAVA实验5 流.docx_第3页
第3页 / 共16页
JAVA实验5 流.docx_第4页
第4页 / 共16页
JAVA实验5 流.docx_第5页
第5页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《JAVA实验5 流.docx》由会员分享,可在线阅读,更多相关《JAVA实验5 流.docx(16页珍藏版)》请在三一办公上搜索。

1、JAVA实验5 流实验5 流 1. 编写程序,要求:用户在键盘每输入一行文本,程序将这段文本显示在控制台中。当用户输入的一行文本是“exit”时,程序将用户所有输入的文本都写入到文件log.txt中,并退出。 package shiyanwu1; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class test1 / 获得系统换行符 private static final String LINE_SEP = System.getPrope

2、rty(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; / 读入一行字符串 if (line = null | exit.equals(line)|EXIT.equals

3、(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)printContentsInOneDirectory:能够将输入参数path所指定的本地磁盘

4、路径下的所有目录和文件的名称打印出来;2)readFileAndDirectory:能够将输入参数path所指定的本地磁盘路径下的所有目录和文件的名称以层次化结构打印出来。例如,某个目录下面有子目录a和文件Teacher.class,目录a下面有子目录b和c以及文件1.txt,将该目录对应的路径作为输入参数调用该方法,程序的输出如下图所示。 package shiyanwu2; import java.io.File; public class test2 Public static void printContentsInOneDirectory(String path) File file

5、 = new File(path); if (!file.exists) throw new RuntimeException(String.format(文件 %s 不存在!, path); File childFiles = file.listFiles; for (File childFile : childFiles) if (childFile.isFile) System.out.format(文件 %sn, childFile.getName); else System.out.format(目录 %sn, childFile.getName); /* * * param pat

6、h 目录路径 * param indent 当前缩进文本文件记录顾客的点菜信息,每桌顾客的点菜记录占一行。每行顾客点菜信息的记录格式是“菜名:数量,菜名:数量,菜名:数量”。例如:“烤鸭:1,土豆丝:2,烤鱼:1”。2)文本文件记录每种菜的具体价格,每种菜及其价格占一行,记录格式为“菜名:价格“。例如:“烤鸭:169”。编写一个程序,能够计算出orders.txt中所有顾客消费的总价格。 package shiyanwu3; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; i

7、mport 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 private List readFile(String fileName) if (fileName != null & !.equals(fileName) File file = null; file = new File(fileNa

8、me); if (file.exists) String dishesName = null; int dishesCount = 0; int totalPrice = 0; for (Map.Entry e : orderDetail.entrySet) dishesName = e.getKey; dishesCount = e.getValue; totalPrice += dishesDetail.get(dishesName) * dishesCount; System.err.println(dishesName+总消费为:+totalPrice); Map orderDetai

9、l = first.resolveOrderDatas(orderDatas); Map dishesDetail = firs .resolveOrderDatas(dishesDatas); public static void main(String args) order first = new order; List orderDatas = first.readFile(D:/java examples/my java/src/shiyanwu3/orders.txt); List dishesDatas = first.readFile(D:/java examples/my j

10、ava/src/shiyanwu3/dishes.txt); List datas = new ArrayList; try InputStream is = new FileInputStream(file); BufferedReader br = new BufferedReader( new InputStreamReader(is,gb2312); String str = null; while (true) br.close; str = br.readLine; if (str != null) datas.add(str); else break; catch (Except

11、ion 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+) return orderDetail; temp1 = datas.get(i).split(,); for (int j = 0; j temp1.length; j+) temp2 = temp

12、1j.split(:); 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); 4. private Map resolveDishesDatas(List datas) Map dishesDetail = new HashMap; String temp = null;

13、 for (int i = 0; i datas.size; i+) return dishesDetail; temp = datas.get(i).split(:); if (temp.length = 2) dishesDetail.put(temp0, Integer.parseInt(temp1); 设计学生类Student,属性:学号;姓名,选修课程及课程成绩。编写一个控制台程序,能够实现Student信息的保存、读取。具体要求:提供Student信息的保存功能:通过控制台输入若干个学生的学号、姓名以及每个学生所修课程的课程名和成绩,将其信息保存到data.dat中;数据读取显示:

14、能够从data.dat文件中读取学生及其课程成绩并显示于控制台。 package shiyanwu4; public class Student private int number; private String name; private String courseName; private int score; public int getNumber public void setNumber(int number) public String getName public void setName(String name) public String getCourseName pu

15、blic void setCourseName(String courseName) public int getScore this.courseName = courseName; return courseName; this.name = name; return name; this.number = number; return number; return score; public void setScore(int score) this.score = score; package shiyanwu4; import java.io.BufferedReader; impo

16、rt 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.Scanner; public class StudentMgr pu

17、blic 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); while (true) code = s.next; if (#4.equalsIgnoreCase(code) System.err.println(程序已退出); break; pub

18、lic static final String studentDat = E:/data.dat; 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(学生姓名:); tmpStr = s.next; student.setName(tmpStr); System

19、.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) showMen

20、u; 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 students = readStudentDat(studentDat); if(students = null | st

21、udents.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 fileName) if (fileName != null & !.equals(fileName) File file = null; f

22、ile = 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 = null; String infos = null; while (true) str = br.readLine; if (str !=

23、 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; retur

24、n datas; return null; public static void saveStudents(List students) throws IOException File file = new File(studentDat); if (!file.exists) BufferedWriter bw = new BufferedWriter(new FileWriter(file,true); StringBuffer sb = new StringBuffer; Student s = null; for (int i = 0; i students.size; i+) bw.

25、flush; bw.close; s = students.get(i); sb.setLength(0); sb.append(s.getNumber + # + s.getName + # + s.getCourseName + # + s.getScore); file.createNewFile; bw.write(sb.toString); bw.write(n); public static void showMenu System.out.println(-); System.out.println(-#1、录入学生信息-); System.out.println(-#2、查看学

26、生信息-); System.out.println(-#3、保存学生信息-); System.out.println(-#4、退出-); System.out.println(-); 5. 编写程序,在控制台窗口提示输入两个整数,然后接收这两个整数,并输出它们的和。 InputStreamTest.java package com.ly.stream; import java.io.BufferedReader; import java.io.InputStreamReader; public class InputStreamTest public static void main(Stri

27、ng args) BufferedReader in = new BufferedReader(new InputStreamReader(System.in); String line=null; try System.out.print(请输入第一个正整数a:); line = in.readLine; int a = Integer.parseInt(line); System.out.print(请输入第二个正整数b:); line = in.readLine; int b = Integer.parseInt(line); System.out.println(a+b= + Stri

28、ng.valueOf(a+b); catch(Exception e) System.out.println(输入错误!); System.exit(0); 6.设计学生类Student,属性:编号;姓名,成绩。编写一个程序:要求:输入3个学生的姓名和成绩,将其姓名和成绩保存到data.txt中;然后从该文件中读取数据,求得这五个学生的平均成绩。 Student.java package com.ly.file; public class Student int id; String name; int score; Student public void setId(int id) this

29、.id = id; public void setName(String name) this.name = name; public void setScore(int score) this.score = score; public String toString return this.id + t + this.name + t + this.score + n; Main.java package com.ly.file; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.Da

30、taOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; public class Main public static void main(String args) BufferedReader in = new BufferedReader(new InputStreamReader(Sys

31、tem.in); try DataOutputStream out = new DataOutputStream(new FileOutputStream(D:student.txt); String line; for(int i=0;i3;i+) Student stu = new Student; System.out.print(学号:); line = in.readLine; int id = Integer.parseInt(line); stu.setId(id); System.out.print(姓名:); String name = in.readLine; stu.se

32、tName(name); System.out.print(成绩:); line = in.readLine; int score = Integer.parseInt(line); stu.setScore(score); out.writeBytes(stu.toString); out.close; catch(IOException e) System.out.println(文件写入失败!); System.exit(0); try DataInputStream din = new DataInputStream(new FileInputStream(D:student.txt)

33、; int ScoreSum = 0; String line; for(int i=0;i3;i+) line = din.readLine; int score = Integer.parseInt(line.trim.split(t)2); ScoreSum += score; System.out.println(平均成绩: + ScoreSum/3); catch(FileNotFoundException e) System.out.println(文件不存在!); System.exit(0); catch(IOException e) System.out.println(文件读取失败!); System.exit(0);

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号