JAVA实验5 流.doc_第1页
JAVA实验5 流.doc_第2页
JAVA实验5 流.doc_第3页
JAVA实验5 流.doc_第4页
JAVA实验5 流.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验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 static 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(); / 读入一行字符串 if (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)printContentsInOneDirectory:能够将输入参数path所指定的本地磁盘路径下的所有目录和文件的名称(指明是目录还是文件,格式见下图)打印出来;2)readFileAndDirectory:能够将输入参数path所指定的本地磁盘路径下的所有目录(包含子目录)和文件的名称(指明是目录还是文件,格式见下图)以层次化结构打印出来。例如,某个目录下面有子目录a和文件Teacher.class,目录a下面有子目录b(下面有文件Teacher.java)和c(下面有文件Test.java和Test.class)以及文件1.txt,将该目录对应的路径作为输入参数调用该方法,程序的输出如下图所示。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) 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 new 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(); readFileAndDirectory(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. 假设某个餐馆平时使用: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;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/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.Entry 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 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(); catch (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.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);return orderDetail;private Map resolveDishesDatas(List datas) Map dishesDetail = new HashMap();String 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信息的保存功能:通过控制台输入若干个学生的学号、姓名以及每个学生所修课程的课程名和成绩,将其信息保存到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 String getName() return name;public void setName(String 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 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.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);while (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(学生姓名:);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; else if (#3.equalsIgnoreCase(code) try if(datas.size() 0 )saveStudents(datas);elseSystem.err.println(无可保存的学生信息); catch (IOException e) System.err.println(保存学生信息异常);e.printStackTrace();else if (#2.equalsIgnoreCase(code) List students = readStudentDat(studentDat);if(students = null | students.size() = 0)System.err.println(暂无学生信息);showMenu();elseSystem.err.println(已有学生人数:+students.size();for(int i=0;istudents.size();i+)/这里输出学生信息elseSystem.err.println(无法识别的菜单);showMenu();public static List readStudentDat(String fileName) 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 = 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 file = new File(studentDat);if (!file.exists() file.createNewFile();BufferedWriter bw = new BufferedWriter(new FileWriter(file,true);StringBuffer sb = new StringBuffer();Student s = null;for (int i = 0; i students.size(); i+) s = students.get(i);sb.setLength(0);sb.append(s.getNumber() + # + s.getName() + #+ s.getCourseName() + # + s.getScore();bw.write(sb.toString();bw.write(n);bw.flush();bw.close();public static void showMenu() System.out.println(-);System.out.println(-#1、录入学生信息-);System.out.println(-#2、查看学生信息-);System.out.println(-#3、保存学生信息-);System.out.println(-#4、退出-);System.out.println(-);5. 编写程序,在控制台窗口提示输入两个整数,然后接收这两个整数,并输出它们的和。(要求:键盘输入通过流封装System.in获取,不要使用Scanner类)InputStreamTest.javapackage com.ly.stream;import java.io.BufferedReader;import java.io.InputStreamReader;public class InputStreamTest public static void main(String 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= + String.valueOf(a+b); catch(Exception e) System.out.println(输入错误!); System.exit(0); 6.设计学生类Student,属性:编号(整型);姓名(字符串),成绩(整型)。编写一个程序:要求:(1)输入3个学生的姓名和成绩,将其姓名和成绩保存到data.txt中;(2)然后从该文件中读取数据,求得这五个学生的平均成绩。Student.javapackage com.ly.file;public class Student int id; String name; int score; Student() public void setId(int id) this.id = id; public void setName(String name) = name; public void setScore(int score) this.score = score; public String toString() return this.id + t + + t + this.score + n; Main.javapackage com.ly.file;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论