OOP with Java 通知 Project 6: 5 月 30 日晚 9 点

Similar documents
OOP with Java 通知 Project 6: 5 月 24 日晚 9 点

OOP with Java 通知 Project 6: 6 月 6 日晚 9 点

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

OOP with Java 通知 Project 7, 6 月 14 日晚 9 点 本周四上机课正常进行 答疑 本周三, 周五下午 1:00 3:00, 理科楼 B911 6 月 12 日考试 ( 随堂 ) 下午 1:00-3:00 教书院 218

JavaIO.PDF

OOP with Java 通知 Project 7 :6 月 14 日晚 9 点 6 月 5 日复习 6 月 12 日考试

while ((ch = fr.read())!= -1) { System.out.print((char) ch); fr.close(); 例 3: 用 BufferedReader 读 TXT 文件 public class FileReaderDemo3 { public static v

OOP with Java 通知 Project 7 :6 月 27 日晚 9 点 6 月 14 日复习 6 月 21 日考试 答疑 6 月 20 日 13 : : 00 理科楼 B911

untitled

chp6.ppt

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

Guava学习之Resources

EJB-Programming-4-cn.doc

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

Microsoft Word - newJavaA4.doc


SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

目 录 实 验 一 : 熟 悉 Java 开 发 环 境... 1 实 验 二 : 类 与 面 向 对 象... 7 实 验 三 : 继 承 和 多 态 实 验 四 : 异 常 处 理 实 验 五 : 流 文 件 及 基 于 文 本 的 应 用... 23

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

2009年9月全国计算机等级考试二级Java真题及答案

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Learning Java

Microsoft PowerPoint - ch7_1 DA class

Microsoft Word - JAVA Programming Language Homework VI_ans.doc

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

untitled

untitled

x MapReduce A Italic URL Constant width Constant width bold Constant width italic 這個圖示代表提示或建議 這個圖示代表一般註解

untitled

chp7.ppt

使用MapReduce读取XML文件

Swing-02.pdf

EJB-Programming-3.PDF

untitled

View:Struts 提 供 了 action form 创 建 form bean, 用 于 在 controller 和 view 间 传 输 数 据 此 外,Struts 提 供 了 自 定 义 JSP 标 签 库, 辅 助 开 发 者 用 JSP 创 建 交 互 式 的 以 表 单 为 基

《大话设计模式》第一章

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

处理基本数据类型 Buffer 内幕 内存映像文件 性能 文件锁闭 压缩 生成 GZIP 压缩文件 对象串行化 恢复对象 正则表达式 生成正则表达式... 41

RUN_PC連載_12_.doc

FileMaker 16 ODBC 和 JDBC 指南

Microsoft Word - 投影片ch14

java2d-4.PDF

Chapter 9: Objects and Classes

Microsoft Word - 01.DOC

FileMaker 15 ODBC 和 JDBC 指南

Java

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

软件工程文档编制

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

Microsoft Word - 投影片ch13

3.1 num = 3 ch = 'C' 2

Java 1 Java String Date

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

Chapter 9: Objects and Classes

2009年3月全国计算机等级考试二级Java语言程序设计笔试试题

[Short Essay:] 1. (5 points) Use state-of-memory diagram to describe the differences between x and y declared as follows: double[] x = new double[4];

Microsoft PowerPoint - 06.ppt

CC213

2 Java 语 言 程 序 设 计 教 程 简 单 性 Java 语 言 的 语 法 与 C 语 言 和 C++ 语 言 很 接 近, 使 得 大 多 数 程 序 员 很 容 易 学 习 和 使 用 Java 另 一 方 面,Java 丢 弃 了 C++ 中 很 少 使 用 的 很 难

OOP with Java 通知 Project 2 提交时间 : 3 月 14 日晚 9 点 另一名助教 : 王桢 学习使用文本编辑器 学习使用 cmd: Power shell 阅读参考资料

JAVA 单元 2.1 四则运算机 ( 一 ) 单元教学进度设计 教学环节 教学内容 教师学生活动活动 反馈 反馈课前作业完成情况 反馈加分 1. 下面哪些是合法的变量名? ( ) A.2variable 答案 :DEG B..variable2 解答 : C.._whatavariable A:/

内 容 提 要 将 JAVA 开 发 环 境 迁 移 到 Linux 系 统 上 是 现 在 很 多 公 司 的 现 实 想 法, 而 在 Linux 上 配 置 JAVA 开 发 环 境 是 步 入 Linux 下 JAVA 程 序 开 发 的 第 一 步, 本 文 图 文 并 茂 地 全 程 指

Microsoft PowerPoint - course2.ppt

Microsoft PowerPoint - string_kruse [兼容模式]

untitled

Microsoft Word - sy09_实验09_字符串和文本IO.docx

OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点

《计算概论》课程 第十九讲 C 程序设计语言应用

Microsoft Word - 11.doc

Android 开发教程

D C 93 2

Functions and Methods 函数和方法 agentzh ( 章亦春 )

Microsoft Word - Learn Objective-C.doc

Microsoft PowerPoint ppt

CC213

Microsoft PowerPoint - course6.ppt

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

ebook

mvc

C H A P T E R 7 Windows Vista Windows Vista Windows Vista FAT16 FAT32 NTFS NTFS New Technology File System NTFS

/ / (FC 3)...

4.1 AMI MQSeries API AMI MQI AMI / / AMI JavaC C++ AMI / AMI AMI - / /

<4D F736F F D20BAF4BADEA457BDD2B1D0A7F75F4A415641B57BA6A1B2D52E646F63>

《面向对象程序设计A》课程教学大纲Ⅱ4

全国计算机技术与软件专业技术资格(水平)考试

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

内容介绍 6.1 任务预览 6.2 异常 6.3 异常种类与层次结构 6.4 异常处理代码块 try-catch-finally 6.5 throw 语句与 throws 子句 6.6 自定义异常类 6.7 异常处理代码块嵌套 6.8 错误与断言 6.9 本章小结 6.10 实训 6: 除法运算程序

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票

untitled

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

第1章

Transcription:

OOP with Java Yuanbin Wu cs@ecnu

OOP with Java 通知 Project 6: 5 月 30 日晚 9 点

复习 异常处理 语法 抛出异常 : throw 处理异常 : try, catch 异常对象 : Exception 类的子类 从方法中抛出异常 方法的异常说明 :throws 中断当前方法的执行, 返回抛出的异常对象, 在该方法的调用路径上寻找合适的 catch.

class SimpleException extends Exception { public class InheritingExceptions { public static void main(string[] args) { try { System.out.println("Throw SimpleException from f()"); throw new SimpleException(); catch(exception e) { System.out.println("Caught it!"); System.out.println(e); System.out.println(e.printStackTrace(System.out));

bar() throws Type1Exception, Type2Exception{ // throw (Type1Exception e); // throw (Type2Exception e); foo() { try{ bar(); catch (Type1Exception e){... catch (Type2Exception e){...

I/O Introduction Path Object InputStream/OutputStream Reader/Writer Typical usage of I/O streams Object Serialization

Introduction I/O Input/output 哪些行为有 I/O 操作? I/O 操作是复杂的 多种交互的设备

Introduction I/O 过程的抽象 流 (Stream) Input Output

Introduction I/O 流 抽象 : 数据的来源 (In), 数据的目的地 (Out) 字节 (01 串 ): ByteArrayStream 文件 : FileStream 字符串 : StringStream 对象 : ObjectStream

Introduction 最重要的 I/O Stream 操作 InputStream: read() OutputStream: write() close() 为生活带来便利的 I/O Stream 操作 从文件读取一行 : readline() 读取一个基本类型 : readint(), readdouble() 读取对象

public static void fileoutput() throws IOException{ String str = "hello world!"; File file = new File("d:\\test2.txt"); // 创建文件 if(!file.exists()){ file.createnewfile(); // 如果文件不存在, 则进行创建 FileOutputStream foutput = new FileOutputStream(file); BufferedOutputStream boutput = new BufferedOutputStream(fOutput); byte[] buffer = str.getbytes(); // 将字符串文本转换成字节数组 boutput.write(buffer); boutput.close(); foutput.close(); public static void fileinput() throws IOException{ File file = new File("d:\\test2.txt"); // 创建文件 FileInputStream fiutput = new FileInputStream(file); BufferedInputStream biutput = new BufferedInputStream(fIutput); int temp = 0; while((temp = biutput.read())!= -1){ // 当 temp 为 -1 时, 数据读取完毕 System.out.print((char)temp); biutput.close(); fiutput.close();

Path 接口 File 类 Java 7 之前 Path 接口 Java 7 引入, 用于替代 File 类 File 类对象可通过 topath() 得到对应的 Path 对象

Path 接口 文件路径 绝对路径 C:/Documents/tmp/Hello.java 相对路径../../tmp/Hello.java

Path 接口 Path 接口 java.nio.file 提供对文件路径字符串的操作

Path 接口 创建 Path 类型的对象 Path p1 = Paths.get("C:/Document/tmp/Hello.java"); Path p2 = FileSystems.getDefault().getPath("C:/Document/tmp/Hello.java");

Path 接口 // Microsoft Windows syntax Path path = Paths.get("C:\\home\\joe\\foo"); // Solaris syntax // Path path = Paths.get("/home/joe/foo"); System.out.format("toString: %s%n", path.tostring()); System.out.format("getFileName: %s%n", path.getfilename()); System.out.format("getName(0): %s%n", path.getname(0)); System.out.format("getNameCount: %d%n", path.getnamecount()); System.out.format("subpath(0,2): %s%n", path.subpath(0,2)); System.out.format("getParent: %s%n", path.getparent()); System.out.format("getRoot: %s%n", path.getroot()); // C:\home\joe\foo // foo // home // 3 // home\joe // home\joe\ // C:\

Files 类 包含文件操作的静态方法 文件是否存在 创建文件? 删除文件 拷贝 / 移动 重命名 列出目录下所有文件...

Files 类 判断文件是否存在 Path p = Paths.get("C:/Document/tmp/Hello.java"); System.out.println(Files.exists(p)); 判断文件是否可读 / 可写 / 可执行 Path p = Paths.get("C:/Document/tmp/Hello.java"); System.out.println(Files.isReadable(p)); System.out.println(Files.isWritable(p)); System.out.println(Files.isExecutable(p));

Files 类 删除文件 try { Files.delete(path); catch (NoSuchFileException x) { System.out.format("%s: no such" + " file or directory%n", path); catch (DirectoryNotEmptyException x) { System.out.format("%s not empty%n", path); catch (IOException x) { // File permission problems are caught here. System.out.println(x);

Files 类 获得文件相关的信息 size(path) isdirectory(path, LinkOption) isregularfile(path, LinkOption...) issymboliclink(path) getlastmodifiedtime(path, LinkOption...) setlastmodifiedtime(path, FileTime) getowner(path, LinkOption...) setowner(path, UserPrincipal)

Stream I/O 流 InputStream 类 read(), close() OutputStream 类 write(), close() 每次读入 / 写出一个字节

从不同的源头产生输入 Byte arrays String objects files InputStream 对应于不同的 InputStream 子类

InputStream ByteArrayInputStream StringBufferInputStream FileInputStream...

OutputStream 输出到不同的目的地 Byte arrays String objects files 对应于不同的 OutputStream 子类

OutputStream ByteArrayOutputStream FileOutputStream...

Stream InputStream ByteArrayInputStream StringBufferInputStream FileInputStream... OutputStream ByteArrayOutputStream FileOutputStream...

import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; public class CopyBytes { public static void main(string[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("outagain.txt"); int c; while ((c = in.read())!= -1) out.write(c); finally { if (in!= null) in.close(); if (out!= null) out.close();

Stream 是否有效 InputStream/OutputStream 每次读 / 写一个字节 缓冲区 (buffer) 可以帮助提高效率 是否易用 有时需要读入 / 写出一个基本类型, 而不是字节 readint(), readdouble() 是否易读 需要格式化的输出 println(), print(), printf(),..

Stream 希望 : 对每一种输入输出流都可以 选择是否带缓冲 选择读入字节还是基本类型 提供格式化的输出 How?

Stream 如何实现带缓冲的输入 / 输出流? 方案 1: read() 方法增加参数 read(byte[ ], int off, int len) 缺点 buffer 长度需要用户指定 用户需要知道更多实现细节

Stream 如何实现带缓冲的输入 / 输出流? 方案 2 继承 BufferedByteArrayInputStream, BufferedByteArrayOutputStream BufferedStringBufferInputStream, BufferedStringBufferOutputStream BufferedFileInputStream, BufferedFileOutputStream 缺点 类过多 无法动态加载

Stream 如何实现带缓冲的输入 / 输出流? 方案 3 组合 定义 BufferedInputStream 类 包含一个 InputStream 对象作为成员 调用该 InputStream 对象的 read(byte[], int off, int len) 实现缓冲 该调用在不同系统上有不同的优化, 并且被封装

FileInputStream fin = new FileInputStream("xanadu.txt"); BufferedInputStream bf = new BufferedInputStream(fin); bf.read(); // buffered read FileOutputStream fout = new FileOutputStream("xanadu.txt"); BufferedOutputStream bf = new BufferedOutputStream(fout); bf.write(1); // buffered write ByteArrayInputStream bin = new ByteArrayInputStream("xanadu.txt".getBytes()); BufferedInputStream bf = new BufferedInputStream(bin); bf.read(); // buffered read ByteArrayOutputStream bout = new ByteArrayOutputStream("xanadu.txt".getBytes()); BufferedOutputStream bf = new BufferedOutputStream(bout); bf.write(1); // buffered read

Stream 添加缓冲功能 BufferedInputStream 添加读取基本类型功能 DataInputStream readint(), readdouble(), readfloat(),...

FileInputStream fin = new FileInputStream("xanadu.txt"); DataInputStream din = new DataInputStream(fin); din.read(); din.readint(); din.readdouble(); FileOutputStream fout = new FileOutputStream("xanadu.txt"); DataOutputStream dout = new DataOutputStream(fout); dout.write(1); dout.writeint(10), dout.writedouble(3.14); ByteArrayInputStream bin = new ByteArrayInputStream("xanadu.txt".getBytes()); DataInputStream din = new DataInputStream(bin); din.read(); din.readint(); din.readdouble(); ByteArrayOutputStream bout = new ByteArrayOutputStream("xanadu.txt".getBytes()); DataOutputStream bout = new DataOutputStream(bout); bout.write(1); bout.writeint(10), bout.writedouble(3.14);

Stream 添加缓冲功能 BufferedInputStream 添加读取基本类型功能 DataInputStream readint(), readdouble(), readfloat(),... 添加格式化输出功能 PrintStream println(), print(),

FileOutputStream fout = new FileOutputStream("xanadu.txt"); PrintStream ps = new PrintStream(fout); ps.write(1); ps.println( hello ); ps.print( world ); ByteArrayOutputStream bout = new ByteArrayOutputStream("xanadu.txt".getBytes()); PrintStream ps = new PrintStream(bout); ps.write(1); ps.println( hello ); ps.print( world );

FilterInputStream 特点 BufferedInputStream, DataInputStream 为 InputStream 的子类 具有 InputStream 所有的类的子类接口

FilterInputStream InputStream FilterInputStream BufferdInputStream DataInputStream

FilterOutputStream OutputStream FilterOutputStream BufferdOutputStream DataOutputStream PrintStream

FilterInputStream 特点 BufferedInputStream, DataInputStream 为 InputStream 的子类 具有 InputStream 所有的类的子类接口 FilterInputStream 的对象可以作为 FilterInputStream 的参数!

FileInputStream fin = new FileInputStream("xanadu.txt"); BufferedInputStream bf = new BufferedInputStream(fin); DataInputStream din = new DataInputStream(bf); din.read(); din.readint(); din.readdouble(); FileOutputStream fout = new FileOutputStream("xanadu.txt"); BufferedOutputStream bf = new BufferedOutputStream(fout); DataOutputStream dout = new DataOutputStream(bf); dout.write(1); dout.writeint(10); dout.writedouble(3.14);

Decorator pattern InputStream ByteArrayInputStream FileInputStream StringBufferInputStream FilterInputStream BufferdInputStream DataInputStream 装饰器

总结 Stream InputStream, OutputStream 根据数据源的不同, 分为 FileInputStream, ByteArrayInputStream FileOutputStream, ByteArrayOutputStream 可以有不同的装饰器 BufferedInputStream, DataInputStream PrintStream

public static void fileoutput() throws IOException{ String str = "hello world!"; File file = new File("d:\\test2.txt"); // 创建文件 if(!file.exists()){ file.createnewfile(); // 如果文件不存在, 则进行创建 FileOutputStream foutput = new FileOutputStream(file); BufferedOutputStream boutput = new BufferedOutputStream(fOutput); byte[] buffer = str.getbytes(); // 将字符串文本转换成字节数组 boutput.write(buffer); boutput.close(); foutput.close(); public static void fileinput() throws IOException{ File file = new File("d:\\test2.txt"); // 创建文件 FileInputStream fiutput = new FileInputStream(file); BufferedInputStream biutput = new BufferedInputStream(fIutput); int temp = 0; while((temp = biutput.read())!= -1){ // 当 temp 为 -1 时, 数据读取完毕 System.out.print((char)temp); biutput.close(); fiutput.close();

Reader / Writer InputStream, OutputStream 每次读入 / 写出一个字节 Reader, Writer 每次读入 / 写出一个字符 Utf-16 每次读入 / 写出 16bit, 或者 32bit

Reader / Writer 读写字节 InputStream OutputStream FileInputStream FileOutputStream StringBufferInputStrea m (no corresponding class) ByteArrayInputStream ByteArrayOutputStrea m 读写字符 Reader Writer FileReader FileWriter StringReader StringWriter CharArrayReader CharArrayWriter

Reader / Writer decorators 读写字节 FilterInputStream FilterOutputStream BufferedInputStream BufferedOutputStream DataInputStream 读写字符 FilterReader FilterWriter BufferedReader BufferedWriter DataInputStream DataOutputStream PrintStream DataOutputStream PrintWriter PrintWriter 可以用 OutputStream 作为参数

例子 Typical uses of I/O streams

import java.io.*; public class BufferedInputFile { // Throw exceptions to console: public static String read(string filename) throws IOException { // Reading input by lines: BufferedReader in = new BufferedReader(new FileReader(filename)); String s; StringBuilder sb = new StringBuilder(); while((s = in.readline())!= null) sb.append(s + "\n"); in.close(); return sb.tostring(); public static void main(string[] args) throws IOException { System.out.print(read("BufferedInputFile.java"));

import java.io.*; public class MemoryInput { public static void main(string[] args) throws IOException { StringReader in = new StringReader( BufferedInputFile.read("MemoryInput.java")); int c; while((c = in.read())!= -1) System.out.print((char)c);

import java.io.*; public class FormattedMemoryInput { public static void main(string[] args) throws IOException { try { DataInputStream in = new DataInputStream( new ByteArrayInputStream( BufferedInputFile.read( "FormattedMemoryInput.java").getBytes())); while(true) System.out.print((char)in.readByte()); catch(eofexception e) { System.err.println("End of stream");

import java.io.*; public class TestEOF { public static void main(string[] args) throws IOException { DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("TestEOF.java"))); while(in.available()!= 0) System.out.print((char)in.readByte());

import java.io.*; public class BasicFileOutput { static String file = "BasicFileOutput.out"; public static void main(string[] args) throws IOException { BufferedReader in = new BufferedReader( new StringReader( BufferedInputFile.read("BasicFileOutput.java"))); PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter(file))); int linecount = 1; Short cut: String s; PrintWriter out = new PrinterWriter(file); while((s = in.readline())!= null ) out.println(linecount++ + ": " + s); out.close(); // Show the stored file: System.out.println(BufferedInputFile.read(file));

import java.io.*; public class StoringAndRecoveringData { public static void main(string[] args) throws IOException { DataOutputStream out = new DataOutputStream( new BufferedOutputStream(new FileOutputStream("Data.txt"))); out.writedouble(3.14159); out.writeutf("that was pi"); out.writedouble(1.41413); out.writeutf("square root of 2"); out.close(); DataInputStream in = new DataInputStream( new BufferedInputStream(new FileInputStream("Data.txt"))); System.out.println(in.readDouble()); // Only readutf() will recover the // Java-UTF String properly: System.out.println(in.readUTF()); System.out.println(in.readDouble()); System.out.println(in.readUTF());

Standard I/O System.out PrintStream System.err PrintStream System.in 需要一些预处理

import java.io.*; public class Echo { public static void main(string[] args) throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); String s; while((s = stdin.readline())!= null && s.length()!= 0) System.out.println(s);