1 / 32

案例 6 :教师输入学生成绩,并将成绩写入一个文件中。

案例 6 :教师输入学生成绩,并将成绩写入一个文件中。. 浙江工业大学 计算机学院 赵小敏 zxm@zjut.edu.cn. 涉及知识点. 1 、流的基本概念 2 、字节流 3 、字符流 4 、文件类 5 、随机读写文件 6 、对象序列化. 1.1 流的基本概念. 数据流 是从源到目的的字节的有序序列,先进先出。 两种基本流 :InputStream( 输入流 ) 和 OutputStream( 输出流 ). 1.2 字节流. InputStream 和 OutputStream 分别是字节输入流和字节输出流的超类

dawn
Download Presentation

案例 6 :教师输入学生成绩,并将成绩写入一个文件中。

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 案例6:教师输入学生成绩,并将成绩写入一个文件中。案例6:教师输入学生成绩,并将成绩写入一个文件中。 浙江工业大学 计算机学院 赵小敏 zxm@zjut.edu.cn

  2. 涉及知识点 • 1、流的基本概念 • 2、字节流 • 3、字符流 • 4、文件类 • 5、随机读写文件 • 6、对象序列化

  3. 1.1流的基本概念 • 数据流是从源到目的的字节的有序序列,先进先出。 • 两种基本流:InputStream(输入流)和OutputStream(输出流)

  4. 1.2字节流 • InputStream和OutputStream分别是字节输入流和字节输出流的超类 • InputStream和OutputStream提供许多用于字节输入输出的方法,包括: • 数据的读取 • 数据的写入 • 标记位置 • 获取数据量 • 关闭数据流

  5. 字节输入流InputStream类的层次结构

  6. InputStream 方法 • 三个基本read()方法 int read() //读一个字节返回 int read(byte[ ] ) // 将数据读入byte[], 返回读的字节数 int read( byte[], int offset, int length ) • 其它方法 void close( ) //关闭流。自顶向下关闭Filter stream int available() //返回未读的字节数 long skip(long n) // 跳过n个字节 boolean markSupported( ) //测试打开的流是否支持书签 void mark(int) //标记当前流,并建立int大小缓冲区 void reset( ) // 返回标签出

  7. 字节输出流OutputStream类层次

  8. OutputStream方法 • 三个基本的write( )方法 void write( int ) // 写一个字节 void write(byte[ ]) // 写一个字节数组 void write(byte[ ], int offset, int length ) • 其它方法 void close( ) void flush( ) // 强行写

  9. 1、创建文件输入输出流对象并打开文件 (2)创建FileOutputStream的对象,打开要写入数据的文件 FileOutputStream的构造方法是: • public FileOutputStream(String name) throws FileNotFoundException • public FileOutputStream(String name,boolean append) throws FileNotFoundException • public FileOutputStream(File fiel) throws FileNotFoundException • 其中:name是要打开的文件名,file是文件类File的对象。如下面语句可以创建文件的输出流对象,并打开要写入数据的文件D:/java/temp/mytext.txt: • FileOutputStream wf=new FileOutputStream(“D:/java/temp/mytext.txt”);

  10. 2、对文件进行读写的方法 (1)用read方法读取文件的数据 • public int read( ) throws IOException //返回从文件中读取的一个字节。 • public int read(byte[] b) throws IOException • public int read(byte[] b,int off,int len) throws IOException //返回读取的字节数,若b的长度为0,返回0。

  11. 2、对文件进行读写的方法 (2)用write方法将数据写入文件 • public void write(int b) throws IOException //向文件写入一个字节,b是int类型,所以将b的低8位写入 • public void write(byte[] b) throws IOException • public void write(byte[] b,int off,int len) throws IOException //将字节数组写入文件,其中off是b中的起始位置,len是写入的最大长度。

  12. 3、字节文件流的关闭 • 当读写操作完毕时,要关闭输入或输出流,释放相关的系统资源。 • 如果发生I/O错误,抛出IOException异常。 • 关闭数据流的方法是: public void close( ) throws IOException

  13. 数据输入输出流:DataInputStream和DataOutputStream • 数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。 DataInputStream(InputStream in) • 数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。 DataOutputStream (OutputStream out)

  14. 1.3字符流 • 类Reader是字符输入流的抽象超类,其提供的方法与InputStream类似,只是将基于Byte的参数改为基于Char。 • 类Writer是字符输出流的抽象超类,其提供的方法与OutputStream类似,只是将基于Byte的参数改为基于Char。 • Reader和Writer 类实现字节和字符间的自动转换。 • 每一个核心输入、输出流,都有相应的Reader和Writer版本。

  15. Reader的类层次结构

  16. Reader的基本方法 int read();//读单个字符 int read(char cbuf[]);//读字符放入数组中 int read(char cbuf[], int offset, int length);//读字符放入数组的指定位置 void close( ) //关闭流。 long skip(long n) // 跳过n个字符 boolean markSupported( ) //测试打开的流是否支持书签 void mark(int) //标记当前流,并建立int大小缓冲区 void reset( ) // 返回标签出 boolean ready() //测试当前流是否准备好进行读

  17. Writer的类层次结构

  18. Writer的基本方法 int write(int c) ; // 写单个字符 int write(char cbuf[]) ;// 写字符数组 int write(char cbuf[], int offset, int length) ; int write(String str) ; int write(String str, int offset, int length) ; void close( ) void flush( ) // 强行写

  19. 字符文件输入输出流:FileReader和FileWrite • FileReader和Filewriter类用于字符文件的输入和输出 • 读写文件的过程: • 先创建对象打开文件 • 然后用读写方法从文件中读取数据或将数据写入文件 • 最后关闭数据流。

  20. 1、创建字符流文件对象,打开文件 • 创建FileReader或Filewriter对象,打开要读写的文件 • FileReader的构造方法: • public FileReader(String filename) • public FileReader(File file) • FileWriter的构造方法: • public FlieWriter(String filename) • public Filewriter(File file)

  21. 2、字符文件流的读写 • 用从超类继承的read和write方法可以对打开的文件进行读写 • 读取文件数据的方法: • int read( ) throws IOException • int read(char b[ ]) throws IOException • int read(char b[ ],int off,int len) throws IOException • 数据写入到文件的方法: • void write(char b) throws IOException • void write(char b[ ]) throws IOException • void write(char b[ ],int off,int len) throws IOException

  22. 3、字符文件流的关闭 • 对文件操作完毕要用close方法关闭数据流。 public void close( ) throws IOException

  23. 字符缓冲流: BufferedReader和BufferedWriter • BufferedReader和BufferedWriter类以缓冲区方式对数据进行输入输出。 1.BufferedReader用于字符缓冲输入,构造方法如下: • public BufferedReader(Reader in) • public BufferedReader(Reader in,int sz) 其中:in为超类Reader的对象,sz为用户设定的缓冲区大小。

  24. 2. BufferedWriter类 • Bufferedwriter用于字符缓冲流输出,构造方法为: • public BufferedWriter(Writer out) • public Bufferedwriter(Writer out,int sz) 其中:out为超类Writer的对象,sz为用户设定的缓冲区大小。

  25. 字节流与字符流的比较 • Reader 和 InputStream以及Writer 与 OutputStream定义的API类似,但操作的数据类型不同。 • 所有的流——InputStream、 OutputStream 、Reader、 Writer 在创建时自动打开;程序中可以调用close方法关闭流,否则Java运行环境的垃圾收集器将隐含将流关闭。

  26. 1.4文件类 • 创建文件流:常用文件名或File类的对象创建文件流。 • 文件过滤:将符合条件的文件选择出来进行操作,通过接口FileFilter和FilenameFilter来实现。

  27. 文件类File • 提供对文件进行创建目录、创建临时文件、改变文件名、删除文件等操作 • 提供获取文件信息的方法,如文件名、文件路径和文件长度等 • File类的构造方法: • public File(String pathname) • public File(String parent,String child) • public File(File parent,String child) • public File(URI uri)

  28. File类的方法 1 、访问文件对象 • public String getName( ) • public String getPath( ) • public String getAbsolutePath( ) • public String getParent( ) • public File getParentFile( ) 2、文件操作 • public boolean renameTo(File  dest) • public boolean delete()

  29. File类的方法(续) 3、获得文件的属性 public long length() public boolean exists() public long lastMoidfied() 4、目录操作 public boolean mkdir() public String[ ] list() public File[ ] listFiles();

  30. 作业6 • 管理员输入图书信息,并将图书信息写入一个文件中。

  31. 补充作业 2、实现一个简单的学生管理系统,能支持学生数据(Student)的显示、增加、修改和删除等操作,学生信息包括学号、姓名、性别、专业等。具体要求如下: • 学生数据按对象序列化写入student.dat文件 • 程序启动后,从student.dat文件读取学生对象存入ArrayList中; • 增加学生数据时将学生对象Student存入ArrayList,按保存后再写入student.dat; • 删除学生必须输入学生的学号; • 修改学生必须输入学号,然后输入姓名、性别和专业进行修改; • 保存时将当前ArrayList中的所有学生对象写入student.dat; • 退出前也要将当前ArrayList中的所有学生对象写入student.dat; • 设计一个操作菜单,包括“增加”、“修改”、“删除”、“显示”、“保存”、“退出”等6个选项。

  32. Questions?

More Related