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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

《Java程序设计》实验9.docx

1、Java程序设计实验9Java语言程序设计上机实验报告实验名称: 实验九 班 级: 软件工程133 姓 名: 陈超 学 号: 1130299452 上机时间: 2014.12.11 指导教师: 虞建东 一、 实验内容或题目:一、 编程。编写应用程序,使用System.in.read()方法读取用户从键盘输入的字节数据,回车后,把从键盘输入的数据存放到数组buffer中,并将用户输入的数据保存为指定路径下的文件。二、 编程。编写应用程序,分别使用FileInputStream和FileReader类对象读取程序本身(或其他目录下的文件)并显示在屏幕上。同时,比较两种方法的性能。三、 编程。编写应

2、用程序,程序中创建一个文件输入流对象fis,读取当前目录下文本文件test1.txt,该文件内容有如下两行文本:Java program is easy.I like it.从文件输入流fis中读取5个字节数据存放到数组b中,字节数据存放的位置从数组下标3开始,将读取的数据在屏幕输出。四、 编程。使用异常处理机制和输入/输出处理机制编写一个程序,实现当用户输入一个文件名时,判断这个文件名是否存在,若不存在,允许用户重新输入,直到输入了一个正确的文件名后,则打开这个文件并将文件中的内容输出到屏幕上。【提示】(1)定义从键盘接收数据的标准输入流对象和文件对象;(2)在此文件对象的基础上建立缓冲文件

3、输入流,如果文件不存在,通过异常处理机制显示错误的处理结果,并要求重新输入文件路径;(3)如果输入的文件路径正确,使用缓冲文件输入流对象的方法将文件中的内容读取出来,并在屏幕上显示,然后退出整个程序。五、 编程。设计一个通讯录,保存读者信息。要求:通讯录中包括一般通讯录中的基本信息,也需要实现普通的检索功能。通讯录写入文件中。六、 编程。一家杂货店的店主,需要查询、输入、修改任何一件商品的品名、价格、货存量信息。请用随机存取文件满足其要求,可以更新、查询信息。每件商品的标志为其记录号。【提示】使用文件输入类FileReader只能将文件内容全部读入。如果要选择读入文件的内容,可使用随机文件类R

4、andomAccessFile。七、 编程。编写一个文件操作类FileOperate,实现以下方法:(1)创建指定的目录makeDirectory:如果指定的目录的父目录不存在则创建其目录树上所有需要的父目录。(2) 清空指定目录中的所有文件emptyDirectory方法: 这个方法将尽可能删除所有的文件,但是只要有一个文件没有被删除都会返回false。(3) listAll方法:列出目录中的所有内容,包括其子目录中的内容。(4)getTypePart方法:得到指定目录下所有以某后缀命名的所有文件名。(5)搜索文件SearchFile方法:搜索给定目录下的指定文件,支持模糊查询和深度搜索。如

5、:test.*,则返回所有以test.开头的文件名。(6)拷贝文件Copy方法:参数1:fromFileName 源文件名;参数2:toFileName 目标文件名;参数3: override 目标文件存在时是否覆盖,返回类型为boolean。二、 实验目的与要求: 1 理解数据流的概念2 理解Java流的层次结构3 理解文件的概念三、 实验步骤与源程序:(1) 实验步骤a)编辑源文件b)编译c)运行(2) 源代码【写出解决本次上机题目的源代码,并提供语句的注释;如程序过长则给出主要程序段并写出注释】题目1import java.io.BufferedReader;import java.io

6、.IOException;import java.io.InputStreamReader;import java.io.*;public class Aa public static void main(String args) throws IOException BufferedReader in = new BufferedReader(new InputStreamReader(System.in); String s = in.readLine(); byte buffer = s.getBytes(); System.out.println(buffer); FileWriter

7、 f = new FileWriter(D:1.txt); BufferedWriter writer = new BufferedWriter(f); writer.write(s); writer.close(); 题目2:import java.io.*;public class Aa public static void main(String args) throws IOException FileInputStream in=new FileInputStream(D:/test.txt); byte b=new byte1024; int len1=in.read(b); in

8、.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=new char1024; 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 ja

9、va.io.InputStream;public class Io public static void main(String args) throws Exception / TODO Auto-generated method stub File fis=new File(d:+File.separator+test.txt); InputStream input=null; input=new FileInputStream(fis); byte b=new byte8; int len=input.read(b,3,5); input.close(); for(int i=3;ib.

10、length;i+) System.out.println(char)bi); 题目4:import java.io.*;import java.util.Scanner;public class Io public static void main(String args) throws Exception / TODO Auto-generated method stub boolean test = true; while (test) System.out.println(请输入文件名); Scanner in = new Scanner(System.in); String s =

11、in.next(); File f = new File(d:+s); if (f.exists() test = false; try InputStream input = null; input = new FileInputStream(f); byte b = new byte1024; int len = input.read(b); System.out.println(内容为: + new String(b, 0, len); catch (Exception e) System.out.println(e); 题目5:import java.io.BufferedOutput

12、Stream;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 = ; Sc

13、anner 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 char1024; int n = 0; while(n = f

14、r.read(chars) != -1) String string = new String(chars, 0, n); content = content + string; else System.out.println(文件不存在); catch (Exception e) e.printStackTrace(); finally if (fr != null) try fr.close(); catch (IOException e) e.printStackTrace(); return content; public void writeFile(String path) Fil

15、e file = new File(path); FileOutputStream fos = null; mkDirs(path); try fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); PrintWriter pw = new PrintWriter(bos, true); pw.print(content); pw.flush(); catch (FileNotFoundException e) e.printStackTrace(); finally

16、 if (fos != null) try fos.close(); catch (IOException e) e.printStackTrace(); public void writeFile() if (isNull(filePath) System.out.println(文件存储路径:); filePath = sc.nextLine(); File file = new File(filePath); FileOutputStream fos = null; mkDirs(filePath); try fos = new FileOutputStream(file); Buffe

17、redOutputStream bos = new BufferedOutputStream(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(); public void mkDirs(String filepat

18、h) if (filepath.indexOf() != -1) filepath = filepath.replaceAll(, /); int n = filepath.indexOf(/); String path = filepath.substring(0, n) + /; filepath = filepath.substring(filepath.indexOf(/) + 1, filepath.length(); String files = filepath.split(/); for (int i = 0; i files.length - 1; i+) path = pa

19、th + filesi; File file = new File(path); if (!file.exists() file.mkdir(); public void addImfor() System.out.println(-增加记录-); String name = ; String tel = ; String email = ; content = readFile(); while(true) System.out.println(姓名:); name = sc.next(); System.out.println(电话:); tel = sc.next(); System.o

20、ut.println(Email:); email = sc.next(); content = content + name + + tel + + email +; System.out.println(0、Exit 1、继续); int i = sc.nextInt(); if (i = 0) break; writeFile(); public void deleteImfor() System.out.println(-删除记录-); String name = ; String imfors = null; content = readFile(); while(true) Sys

21、tem.out.println(你要删除的姓名是:); name = sc.next(); if (content.indexOf(name) != -1) imfors = content.split(); for (int i = 0; i imfors.length; i+) if (imforsi.indexOf(name) != -1) imforsi = ; content = ; for (int i = 0; i imfors.length; i+) if (!isNull(imforsi) content = content + imforsi + ; writeFile()

22、; System.out.println(删除成功); else System.out.println(此人不存在); System.out.println(0、Exit 1、继续); int i = sc.nextInt(); if (i = 0) break; public void viewAll() System.out.println(-显示所有-); content = readFile(); if (!isNull(content) String imfors = content.split(); System.out.println(姓名t电话tEmail); for (int

23、 i = 0; i imfors.length; i+) String imfor = imforsi.split(); for (int j = 0; j imfor.length; j+) System.out.print(imforj + t); System.out.println(); else System.out.println(暂时还没有记录); public void queryImfor() System.out.println(-查找记录-); content = readFile(); if (!isNull(content) String result = ; Str

24、ing imfors = null; String imfor = null; String name = ; boolean bool = false; while(true) result = ; System.out.println(请输入关键字(按姓名查找):); name = sc.next(); bool = false; if (content.indexOf(name) != -1) imfors = content.split(); for (int i = 0; i imfors.length; i+) if (imforsi.indexOf(name) != -1) im

25、for = imforsi.split(); if (imfor0.equals(name) bool = true; result = result + imforsi + ; if (bool) imfors = result.split(); System.out.println(姓名t电话tEmail); for (int i = 0; i imfors.length; i+) imfor = imforsi.split(); for (int j = 0; j imfor.length; j+) System.out.print(imforj + t); System.out.pri

26、ntln(); else System.out.println(无此人信息); else System.out.println(无此人信息); System.out.println(0、Exit 1、继续); int i = sc.nextInt(); if (i = 0) break; else System.out.println(文件还没有记录); public void copy() System.out.println(-备份-); content = readFile(); if (isNull(bakPath) System.out.println(备份全路径:); bakPat

27、h = sc.next(); writeFile(bakPath); System.out.println(备份成功); public boolean isNull(String string) if (null = string | = string | 0 = string.length() return true; else return false; public static void main(String args) AddList add = new AddList(); Scanner sc = new Scanner(System.in); int operater = 0; while(true) System.out.println(选择功能:n1、增加记录 2、删除记录 3、显示所有 4、查询记录 5、备份 6、退出); operater = sc.nextInt(); if (1 = operater) add.addImfor(); else if (2 = operater) add.deleteImfor(); else if (3 = operater) add.viewAll(); else if (4 = operater) ad

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2