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





《2022年《Java程序设计》实验 .pdf》由会员分享,可在线阅读,更多相关《2022年《Java程序设计》实验 .pdf(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Page 1 of 21Java语言程序设计上机实验报告实验名称:实验九班级:软件工程 133 姓名:陈超学号: 1130299452 上机时间: 2014.12.11 指导教师:虞建东名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 21 页 - - - - - - - - - Page 2 of 21一、实验内容或题目:一、编程。编写应用程序,使用System.in.read()方法读取用户从键盘输入的字节数据,回车后,把从键盘输入的数据存放到数组buffer 中,并将
2、用户输入的数据保存为指定路径下的文件。二、编程。编写应用程序, 分别使用 FileInputStream 和 FileReader 类对象读取程序本身(或其他目录下的文件)并显示在屏幕上。同时,比较两种方法的性能。三、编程。编写应用程序, 程序中创建一个文件输入流对象fis, 读取当前目录下文本文件test1.txt,该文件内容有如下两行文本:Java program is easy. I like it. 从文件输入流fis 中读取 5 个字节数据存放到数组b 中,字节数据存放的位置从数组下标 3 开始,将读取的数据在屏幕输出。四、编程。使用异常处理机制和输入/输出处理机制编写一个程序,实现
3、当用户输入一个文件名时,判断这个文件名是否存在,若不存在, 允许用户重新输入,直到输入了一个正确的文件名后,则打开这个文件并将文件中的内容输出到屏幕上。【提示】(1)定义从键盘接收数据的标准输入流对象和文件对象;(2)在此文件对象的基础上建立缓冲文件输入流,如果文件不存在,通过异常处理机制显示错误的处理结果,并要求重新输入文件路径;(3)如果输入的文件路径正确,使用缓冲文件输入流对象的方法将文件中的内容读取出来,并在屏幕上显示,然后退出整个程序。五、编程。设计一个通讯录,保存读者信息。要求:通讯录中包括一般通讯录中的基本信息,也需要实现普通的检索功能。通讯录写入文件中。六、编程。一家杂货店的店
4、主,需要查询、输入、修改任何一件商品的品名、价格、货存量信息。请用随机存取文件满足其要求,可以更新、查询信息。每件商品的标志为其记录号。【提示】名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 21 页 - - - - - - - - - Page 3 of 21使用文件输入类FileReader 只能将文件内容全部读入。如果要选择读入文件的内容,可使用随机文件类RandomAccessFile。七、编程。编写一个文件操作类FileOperate,实现以下方法:(1) 创建
5、指定的目录makeDirectory :如果指定的目录的父目录不存在则创建其目录树上所有需要的父目录。(2) 清空指定目录中的所有文件emptyDirectory方法:这个方法将尽可能删除所有的文件,但是只要有一个文件没有被删除都会返回false。(3) listAll 方法:列出目录中的所有内容,包括其子目录中的内容。(4)getTypePart 方法:得到指定目录下所有以某后缀命名的所有文件名。(5) 搜索文件SearchFile 方法 :搜索给定目录下的指定文件,支持模糊查询和深度搜索。如:test.* ,则返回所有以test.开头的文件名。(6) 拷贝文件 Copy方法: 参数 1:f
6、romFileName 源文件名; 参数 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.InputStre
7、amReader; 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
8、= 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 FileInputStrea
9、m 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( 内容为:
10、+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 / T
11、ODO 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 j
12、ava.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( 请输入
13、文件名 ); 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 (Exce
14、ption 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
15、.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 页 - - - - - - - -
16、 - 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
17、; 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.pri
18、ntStackTrace(); 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 = newBuffere
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java程序设计 2022年Java程序设计实验 2022 Java 程序设计 实验

限制150内