欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    2022年《Java程序设计》实验 .pdf

    • 资源ID:27271700       资源大小:194.59KB        全文页数:21页
    • 资源格式: PDF        下载积分:4.3金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要4.3金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    2022年《Java程序设计》实验 .pdf

    Page 1 of 21Java语言程序设计上机实验报告实验名称:实验九班级:软件工程 133 姓名:陈超学号: 1130299452 上机时间: 2014.12.11 指导教师:虞建东名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 21 页 - - - - - - - - - Page 2 of 21一、实验内容或题目:一、编程。编写应用程序,使用System.in.read()方法读取用户从键盘输入的字节数据,回车后,把从键盘输入的数据存放到数组buffer 中,并将用户输入的数据保存为指定路径下的文件。二、编程。编写应用程序, 分别使用 FileInputStream 和 FileReader 类对象读取程序本身(或其他目录下的文件)并显示在屏幕上。同时,比较两种方法的性能。三、编程。编写应用程序, 程序中创建一个文件输入流对象fis, 读取当前目录下文本文件test1.txt,该文件内容有如下两行文本:Java program is easy. I like it. 从文件输入流fis 中读取 5 个字节数据存放到数组b 中,字节数据存放的位置从数组下标 3 开始,将读取的数据在屏幕输出。四、编程。使用异常处理机制和输入/输出处理机制编写一个程序,实现当用户输入一个文件名时,判断这个文件名是否存在,若不存在, 允许用户重新输入,直到输入了一个正确的文件名后,则打开这个文件并将文件中的内容输出到屏幕上。【提示】(1)定义从键盘接收数据的标准输入流对象和文件对象;(2)在此文件对象的基础上建立缓冲文件输入流,如果文件不存在,通过异常处理机制显示错误的处理结果,并要求重新输入文件路径;(3)如果输入的文件路径正确,使用缓冲文件输入流对象的方法将文件中的内容读取出来,并在屏幕上显示,然后退出整个程序。五、编程。设计一个通讯录,保存读者信息。要求:通讯录中包括一般通讯录中的基本信息,也需要实现普通的检索功能。通讯录写入文件中。六、编程。一家杂货店的店主,需要查询、输入、修改任何一件商品的品名、价格、货存量信息。请用随机存取文件满足其要求,可以更新、查询信息。每件商品的标志为其记录号。【提示】名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 21 页 - - - - - - - - - Page 3 of 21使用文件输入类FileReader 只能将文件内容全部读入。如果要选择读入文件的内容,可使用随机文件类RandomAccessFile。七、编程。编写一个文件操作类FileOperate,实现以下方法:(1) 创建指定的目录makeDirectory :如果指定的目录的父目录不存在则创建其目录树上所有需要的父目录。(2) 清空指定目录中的所有文件emptyDirectory方法:这个方法将尽可能删除所有的文件,但是只要有一个文件没有被删除都会返回false。(3) listAll 方法:列出目录中的所有内容,包括其子目录中的内容。(4)getTypePart 方法:得到指定目录下所有以某后缀命名的所有文件名。(5) 搜索文件SearchFile 方法 :搜索给定目录下的指定文件,支持模糊查询和深度搜索。如:test.* ,则返回所有以test.开头的文件名。(6) 拷贝文件 Copy方法: 参数 1:fromFileName 源文件名; 参数 2:toFileName 目标文件名;参数 3: override 目标文件存在时是否覆盖,返回类型为boolean 。二、实验目的与要求:1理解数据流的概念2理解 Java 流的层次结构3理解文件的概念三、实验步骤与源程序:(1)实验步骤a) 编辑源文件b) 编译c) 运行(2) 源代码【写出解决本次上机题目的源代码, 并提供语句的注释; 如程序过长则给出主要程序段并写出注释】题目 1 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.*; publicclass Aa publicstaticvoid main(String args) throws IOException 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 21 页 - - - - - - - - - Page 4 of 21BufferedReader in = new BufferedReader(newInputStreamReader(System.in ); String s = in.readLine(); byte buffer = s.getBytes(); System.out .println(buffer); FileWriter f = new FileWriter(D:1.txt); BufferedWriter writer = new BufferedWriter(f); writer.write(s); writer.close(); 题目 2:import java.io.*; publicclass Aa publicstaticvoid main(String args) throws IOException FileInputStream in=new FileInputStream(D:/test.txt); byte b=newbyte1024; int len1=in.read(b); in.close(); System.out .println( 内容为: +new String(b,0,len1); File f=new File(D:/test.txt); Reader input=null; input=new FileReader(f); char c=newchar1024; int len2=input.read(c); input.close(); System.out .println( 内容为: +new String(c,0,len2); 题目 3:import java.io.File; import java.io.FileInputStream; import java.io.InputStream; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 21 页 - - - - - - - - - Page 5 of 21publicclass Io publicstaticvoid main(String args ) throwsException / TODO Auto-generated method stub File fis =new File(d: +File.separator+test.txt); InputStream input =null; input =new FileInputStream(fis ); byteb= new byte 8; intlen =input.read(b,3,5); input .close(); for ( inti =3; i b. length; i +) System. out .println(char ) b i ); 题目 4:import java.io.*; import java.util.Scanner; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 21 页 - - - - - - - - - Page 6 of 21publicclass Io publicstaticvoid main(String args ) throwsException / TODO Auto-generated method stubbooleantest = true ; while ( test ) System. out .println( 请输入文件名 ); Scanner in = new Scanner(System.in ); String s = in .next(); File f = new File(d:+s); if ( f .exists() test = false; try InputStream input = null ; input = new FileInputStream(f ); byte b = new byte 1024; intlen = input .read(b); System. out .println( 内容为: + newString(b, 0, len ); catch (Exception e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 21 页 - - - - - - - - - Page 7 of 21System. out .println(e); 题目 5:import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; class AddList private String filePath = ; private String bakPath = ; private String content = ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 21 页 - - - - - - - - - Page 8 of 21 Scanner sc = new Scanner(System.in ); public String readFile() content = ; if (isNull(filePath) System.out .println( 文件存储路径: ); filePath = sc.nextLine(); File file = new File(filePath); FileReader fr = null; try if ( file.exists() fr = new FileReader(file); char chars = new char 1024; intn = 0; while ( n = fr .read(chars ) != -1) String string = new String(chars , 0, n); content = content + string; else System.out .println( 文件不存在 ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 21 页 - - - - - - - - - Page 9 of 21 catch (Exception e) e.printStackTrace(); finally if ( fr != null) try fr .close(); catch (IOException e) e.printStackTrace(); returncontent; publicvoid writeFile(String path ) File file = new File(path ); FileOutputStream fos = null ; mkDirs(path ); try fos = new FileOutputStream(file); BufferedOutputStream bos = newBufferedOutputStream(fos ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 21 页 - - - - - - - - - Page 10 of 21 PrintWriter pw = new PrintWriter(bos, true ); pw.print(content); pw.flush(); catch (FileNotFoundException e) e.printStackTrace(); finally if ( fos != null ) try fos .close(); catch (IOException e) e.printStackTrace(); publicvoid writeFile() if (isNull(filePath) System.out .println( 文件存储路径: ); filePath = sc.nextLine(); File file = new File(filePath); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 21 页 - - - - - - - - - Page 11 of 21 FileOutputStream fos = null ; mkDirs(filePath); try fos = new FileOutputStream(file); BufferedOutputStream bos = newBufferedOutputStream(fos ); PrintWriter pw = new PrintWriter(bos, true ); pw.print(content); pw.flush(); catch (FileNotFoundException e) e.printStackTrace(); finally if ( fos != null ) try fos .close(); catch (IOException e) e.printStackTrace(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 21 页 - - - - - - - - - Page 12 of 21publicvoid mkDirs(String filepath) if ( filepath.indexOf() != -1) filepath = filepath.replaceAll(, / ); intn = filepath.indexOf(/); String path = filepath.substring(0, n) + /; filepath = filepath.substring(filepath.indexOf(/) + 1, filepath.length(); String files = filepath.split(/ ); for ( inti = 0; i files. length - 1; i +) path = path + files i ; File file = new File(path ); if (!file.exists() file.mkdir(); publicvoid addImfor() System.out .println(-增加记录 -); String name = ; String tel = ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 21 页 - - - - - - - - - Page 13 of 21 String email = ; content = readFile(); while ( true ) System.out .println( 姓名: ); name = sc.next(); System.out .println( 电话: ); tel = sc.next(); System.out .println(Email : ); email = sc.next(); content = content + name + + tel + + email+ ; System.out .println(0 、Exit 1、继续 ); inti = sc.nextInt(); if ( i = 0) break ; writeFile(); publicvoid deleteImfor() System.out .println(-删除记录 -); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 21 页 - - - - - - - - - Page 14 of 21 String name = ; String imfors = null ; content = readFile(); while ( true ) System.out .println( 你要删除的姓名是: ); name = sc.next(); if ( content.indexOf(name) != -1) imfors = content.split( ); for ( inti = 0; i imfors. length ; i +) if ( imfors i .indexOf(name) != -1) imfors i = ; content = ; for ( inti = 0; i imfors. length ; i +) if (!isNull(imfors i ) content = content + imfors i + ; writeFile(); System.out .println( 删除成功 ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 21 页 - - - - - - - - - Page 15 of 21 else System.out .println( 此人不存在 ); System.out .println(0 、Exit 1、继续 ); inti = sc.nextInt(); if ( i = 0) break ; publicvoid viewAll() System.out .println(-显示所有-); content = readFile(); if (!isNull(content) String imfors = content.split( ); System.out .println( 姓名t电话 tEmail); for ( inti = 0; i imfors. length ; i +) String imfor = imfors i .split( ); for ( intj = 0; j imfor . length ; j +) System.out .print(imfor j + t); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 21 页 - - - - - - - - - Page 16 of 21 System.out .println(); else System.out .println( 暂时还没有记录 ); publicvoid queryImfor() System.out .println(-查找记录 -); content = readFile(); if (!isNull(content) String result = ; String imfors = null ; String imfor = null ; String name = ; booleanbool = false; while ( true ) result = ; System.out .println( 请输入关键字(按姓名查找) : ); name = sc.next(); bool = false; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 21 页 - - - - - - - - - Page 17 of 21if ( content.indexOf(name) != -1) imfors = content.split( ); for ( inti = 0; i imfors. length ; i +) if ( imfors i .indexOf(name) != -1) imfor = imfors i .split( ); if ( imfor 0.equals(name) bool = true ; result = result + imfors i + ; if ( bool ) imfors = result.split( ); System.out .println( 姓名t电话tEmail); for ( inti = 0; i imfors. length; i +) imfor = imfors i .split( ); for ( intj = 0; j imfor . length; j +) System.out .print(imfor j + t); System.out .println(); else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 21 页 - - - - - - - - - Page 18 of 21 System.out .println( 无此人信息 ); else System.out .println( 无此人信息 ); System.out .println(0 、Exit 1、继续 ); inti = sc.nextInt(); if ( i = 0) break ; else System.out .println( 文件还没有记录 ); publicvoid copy() System.out .println(-备份-); content = readFile(); if (isNull(bakPath ) System.out .println( 备份全路径: ); bakPath = sc.next(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 21 页 - - - - - - - - - Page 19 of 21 writeFile(bakPath ); System.out .println( 备份成功 ); publicboolean isNull(String string) if( null= string| = string| 0 = string.length() returntrue ; else returnfalse; publicstaticvoid main(String args ) AddList add = new AddList(); Scanner sc = new Scanner(System.in ); intoperater = 0; while ( true ) System.out .println( 选择功能: n1 、增加记录 2 、删除记录 3 、显示所有 4 、查询记录 5 、备份 6 、退出 ); operater = sc.nextInt(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 19 页,共 21 页 - - - - - - - - - Page 20 of 21if (1 = operater) add.addImfor(); elseif (2 = operater) add.deleteImfor(); elseif (3 = operater) add.viewAll(); elseif (4 = operater) add.queryImfor(); elseif (5 = operater) add.copy(); elseif (6 = operater) System.out .println( 谢谢使用 ); break ; 四、测试数据与实验结果(可以抓图粘贴):题目 1 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 20 页,共 21 页 - - - - - - - - - Page 21 of 21题目 2 题目 3题目 4:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 21 页,共 21 页 - - - - - - - - -

    注意事项

    本文(2022年《Java程序设计》实验 .pdf)为本站会员(Che****ry)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开