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