微信自助解封需要多久申请解封要多久

JAVA输入输出流--字节流篇(什么时候用哪个) 三亿文库
JAVA输入输出流--字节流篇(什么时候用哪个)
stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数据源可以是文件,内存,或是网络连接。类似的,当程序需要写入数据的时候,就会开启一个通向目的地的流。这时候你就可以想象数据好像在这其中“流”动一样,如下图:
在Java的IO中,所有的stream(包括Inputstream和Out stream)都包括两种类型:
表示以字节为单位从stream中读取或往stream中写入信息,即io包中的inputstream类和outputstream类的派生类。通常用来读取二进制数据,如图象和声音。
以Unicode字符为导向的stream,表示以Unicode字符为单位从stream中读取或往stream中写入信息。
Reader和Writer要解决的,最主要的问题就是国际化。原先的I/O类库只支持8位的字节流,因此不可能很好地处理16位的Unicode字符流。Unicode是国际化的字符集(更何况Java内置的char就是16位的Unicode字符),这样加了Reader和Writer之后,所有的I/O就都支持Unicode了。此外新类库的性能也比旧的好。
但是,Read和Write并不是取代InputStream和OutputStream,有时,你还必须同时使用&基于byte的类&和&基于字符的类&。为此,它还提供了两个&适配器(adapter)&类。InputStreamReader负责将InputStream转化成Reader,而OutputStreamWriter则将OutputStream转化成Writer。实际上是通过byte[]和String来关联。在实际开发中出现的汉字问题实际上都是在字符流和字节流之间转化不统一而造成的。
以字符为导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,只是在操作时的导向不同。
如 CharArrayReader和ByteArrayInputStream的作用都是把内存中的一个缓冲区作为InputStream使用,所不同的是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。
字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串;而字节流处理单元为1个字节,操作字节和字节数组,可用于任何类型的对象,包括二进制对象,但是不能直接处理Unicode字符。字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好。如果是音频文件、图片、歌曲,就用字节流好点,如果是关系到中文(文本)的,用字符流好点。
2流的层次结构
java将读取数据对象称为输入流,能向其写入的对象叫输出流。
基于字节的输入流
FileInputStream:把一个文件作为InputStream,从本地文件系统中
读取数据字节,实现对文件的读取操作
ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使
用,从内存数组中读取数据字节
ObjectInputStream:对象输入流。从文件中把对象读出来重新建立。
对象必须要实现Serializable接口。对象中的transient和static类型的成员变量不会被读取和写入。
PipedInputStream:实现了pipe的概念,从线程管道中读取数据字节,
主要在线程中使用。管道输入流是指一个通讯管道的接收端。一个线程通过管道输出流发送数据,而另一个线程通过管道输入流读取数据,这样可实现两个线程间的通讯
SequenceInputStream:把多个InputStream合并为一个
InputStream,当到达流的末尾时从一个流转到另一个流,“序列输入流”类允许应用程序把几个输入流连续地合并起来,并且使它们像单个输入流一样出现。
StringBufferInputStream:把一个String对象作为InputStream,
从字符串中读取数据字节
FilterInputStream:过滤器流java.io.FilterInputStream,过滤器流
即能把基本流包裹起来,提供更多方便的用法。类的构造方法为
FilterInputStream(InputStream),在指定的输入流之上,创建一个输入流过滤器。常用的子类如下:
BufferedInputStream:缓冲区对数据的访问,以提高效率 ?
DataInputStream:从输入流中读取基本数据类型,如int、float、
double或者甚至一行文本
LineNumberInputStream:在翻译行结束符的基础上,维护一个
计数器,该计数器表明正在读取的是哪一行。
PushbackInputStream:允许把数据字节向后推到流的首部 ?
从用户控制台读取数据字节
在System类中, in是InputStream类的静态对象,因此,out和err可以引用PrintStream类的成员方法。如:System.in.read()。 2)
基于字节的输出流
FileOutputStream:把信息存入文件中
ByteArrayOutputStream:把信息存入内存中的一个缓冲区中,该类实
现一个以字节数组形式写入数据的输出流
PipedOutputStream:实现了pipe的概念,主要在线程中使用。管道
输出流是指一个通讯管道的发送端。 一个线程通过管道输出流发送数据, 而另一个线程通过管道输入流读取数据,这样可实现两个线程间的通讯。
SequenceOutputStream:把多个OutStream合并为一个
FilterOutputStream:类似于FilterInputStream,OutputStream也
提供了过滤器输出流。
ObjectOutputStream:对象输出流。对象必须要实现Serializable接
口。对象中的transient和static类型的成员变量不会被读取和写入。 ?
System.out
输出数据字节到用户控制台
在System类中,out和err是PrintStream类的静态对象,因此,out和err可以引用PrintStream类的成员方法。如:System.out.write (int a)。
基于字符的输入流
CharArrayReader:与ByteArrayInputStream对应,从字符数组中
StringReader:与StringBufferInputStream对应,从字符数组中读
FileReader:与FileInputStream对应,从本地文件系统中读取字符序
PipedReader:与PipedInputStream对应,从线程管道中读取字符序
InputStreamReader:InputStreamReader是从输入流中读取数据,
连接输入流于读取器。如: new InputStreamReader(System.in) ?
BufferedReader:缓冲数据的访问,以提高效率
LineNumberReader(BufferedReader的子类):维护一个计数
器,该计数器表明正在读取的是哪一行。
FilterReader(抽象类):提供一个类创建过滤器时可以扩展这个类
PushbackReader(FilterReader的子类):允许把文本数据推回
到读取器的流中。
这些过滤器读取器都可以传入一个Reader作为构造方法的参数。 4)
基于字符的输出流
CharArrayWrite:与ByteArrayOutputStream对应
StringWrite:无与之对应的以字节为导向的stream
FileWrite:与FileOutputStream对应
联系客服:cand57</君,已阅读到文档的结尾了呢~~
广告剩余8秒
文档加载中
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
输入输出流
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口1流的概念;stream代表的是任何有能力产出数据的数据源,;在Java的IO中,所有的stream(包括In;(1)字节流;表示以字节为单位从stream中读取或往stre;(2)字符流;以Unicode字符为导向的stream,表示以;区别:;Reader和Writer要解决的,最主要的问题;但是,Read和Write并不是取代InputS;以字符为导向
stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数据源可以是文件,内存,或是网络连接。类似的,当程序需要写入数据的时候,就会开启一个通向目的地的流。这时候你就可以想象数据好像在这其中“流”动一样,如下图:
在Java的IO中,所有的stream(包括Inputstream和Out stream)都包括两种类型:
表示以字节为单位从stream中读取或往stream中写入信息,即io包中的inputstream类和outputstream类的派生类。通常用来读取二进制数据,如图象和声音。
以Unicode字符为导向的stream,表示以Unicode字符为单位从stream中读取或往stream中写入信息。
Reader和Writer要解决的,最主要的问题就是国际化。原先的I/O类库只支持8位的字节流,因此不可能很好地处理16位的Unicode字符流。Unicode是国际化的字符集(更何况Java内置的char就是16位的Unicode字符),这样加了Reader和Writer之后,所有的I/O就都支持Unicode了。此外新类库的性能也比旧的好。
但是,Read和Write并不是取代InputStream和OutputStream,有时,你还必须同时使用&基于byte的类&和&基于字符的类&。为此,它还提供了两个&适配器(adapter)&类。InputStreamReader负责将InputStream转化成Reader,而OutputStreamWriter则将OutputStream转化成Writer。实际上是通过byte[]和String来关联。在实际开发中出现的汉字问题实际上都是在字符流和字节流之间转化不统一而造成的。
以字符为导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,只是在操作时的导向不同。
如 CharArrayReader和ByteArrayInputStream的作用都是把内存中的一个缓冲区作为InputStream使用,所不同的是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。
字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串;而字节流处理单元为1个字节,操作字节和字节数组,可用于任何类型的对象,包括二进制对象,但是不能直接处理Unicode字符。字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好。如果是音频文件、图片、歌曲,就用字节流好点,如果是关系到中文(文本)的,用字符流好点。
2流的层次结构
java将读取数据对象称为输入流,能向其写入的对象叫输出流。
基于字节的输入流
FileInputStream:把一个文件作为InputStream,从本地文件系统中
读取数据字节,实现对文件的读取操作
ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使
用,从内存数组中读取数据字节
ObjectInputStream:对象输入流。从文件中把对象读出来重新建立。
对象必须要实现Serializable接口。对象中的transient和static类型的成员变量不会被读取和写入。
PipedInputStream:实现了pipe的概念,从线程管道中读取数据字节,
主要在线程中使用。管道输入流是指一个通讯管道的接收端。一个线程通过管道输出流发送数据,而另一个线程通过管道输入流读取数据,这样可实现两个线程间的通讯
SequenceInputStream:把多个InputStream合并为一个
InputStream,当到达流的末尾时从一个流转到另一个流,“序列输入流”类允许应用程序把几个输入流连续地合并起来,并且使它们像单个输入流一样出现。
StringBufferInputStream:把一个String对象作为InputStream,
从字符串中读取数据字节
FilterInputStream:过滤器流java.io.FilterInputStream,过滤器流
即能把基本流包裹起来,提供更多方便的用法。类的构造方法为
FilterInputStream(InputStream),在指定的输入流之上,创建一个输入流过滤器。常用的子类如下:
BufferedInputStream:缓冲区对数据的访问,以提高效率
DataInputStream:从输入流中读取基本数据类型,如int、float、
double或者甚至一行文本
LineNumberInputStream:在翻译行结束符的基础上,维护一个
计数器,该计数器表明正在读取的是哪一行。
PushbackInputStream:允许把数据字节向后推到流的首部
从用户控制台读取数据字节
在System类中, in是InputStream类的静态对象,因此,out和err可以引用PrintStream类的成员方法。如:System.in.read()。
基于字节的输出流
FileOutputStream:把信息存入文件中
ByteArrayOutputStream:把信息存入内存中的一个缓冲区中,该类实
现一个以字节数组形式写入数据的输出流
PipedOutputStream:实现了pipe的概念,主要在线程中使用。管道
输出流是指一个通讯管道的发送端。 一个线程通过管道输出流发送数据, 而另一个线程通过管道输入流读取数据,这样可实现两个线程间的通讯。
SequenceOutputStream:把多个OutStream合并为一个
FilterOutputStream:类似于FilterInputStream,OutputStream也
提供了过滤器输出流。
ObjectOutputStream:对象输出流。对象必须要实现Serializable接
口。对象中的transient和static类型的成员变量不会被读取和写入。 ?
System.out
输出数据字节到用户控制台
在System类中,out和err是PrintStream类的静态对象,因此,out和err可以引用PrintStream类的成员方法。如:System.out.write (int a)。
基于字符的输入流
CharArrayReader:与ByteArrayInputStream对应,从字符数组中
StringReader:与StringBufferInputStream对应,从字符数组中读
FileReader:与FileInputStream对应,从本地文件系统中读取字符序
PipedReader:与PipedInputStream对应,从线程管道中读取字符序
InputStreamReader:InputStreamReader是从输入流中读取数据,
连接输入流于读取器。如: new InputStreamReader(System.in) ?
BufferedReader:缓冲数据的访问,以提高效率
LineNumberReader(BufferedReader的子类):维护一个计数
器,该计数器表明正在读取的是哪一行。
FilterReader(抽象类):提供一个类创建过滤器时可以扩展这个类
PushbackReader(FilterReader的子类):允许把文本数据推回
到读取器的流中。
这些过滤器读取器都可以传入一个Reader作为构造方法的参数。
基于字符的输出流
CharArrayWrite:与ByteArrayOutputStream对应
StringWrite:无与之对应的以字节为导向的stream
FileWrite:与FileOutputStream对应
PipedWrite:与PipedOutputStream对应
3InputStream类
Inputstream类和Outputstream类都为抽象类,不能创建对象,可以通过子类来实例化。
InputStream是输入字节数据用的类,所以InputStream类提供了3种重载的read方法。Reader也有完全相同的3个read接口。Inputstream类中的常用方法:
public abstract int read( ):读取一个byte的数据,返回读到的数据
(高位补0的int类型值),如果返回-1,表示读到了输入流的末尾。
public int read(byte
b[ ]):读取b.length个字节的数据放到b数
组中。返回值是读取的字节数, 如果返回-1,表示读到了输入流的末尾。该方法实际上是调用下一个方法实现的。
public int read(byte
len):从输入流中最多读取
len个字节的数据,存放到数组b中,返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。off指定在 数组b中存放数据的起始偏移位置。
public int available( ):返回输入流中可以读取的字节数。注意:若输
入阻塞,当前线程将被挂起,如果InputStream对象调用这个方法的话,它只会返回0,这个方法必须由继承InputStream类的子类对象调用才有用。
public long skip(long
n):忽略输入流中的n个字节,返回值是实际
忽略的字节数, 跳过一些字节来读取。
public int close( ) :我们在使用完后,必须对我们打开的流进行关闭。 7)
void mark(int readlimit) :在输入流的当前位置放置一个 标记,如果
读取的字节数多于readlimit设置的值,则流忽略这个标记。在
IutputStream类中实际是一个空实现。
void reset() :返回到上一个标记。
boolean markSupported() :测试当前流是否支持mark和reset方
法。如果支持,返回true,否则返回false。在IutputStream类中实际是一个空实现。
4OutputSteam类
OutputStream提供了3个write方法来做数据的输出,这个是和
InputStream是相对应的,Writer同样提供了相同的三个write方法。 1)
public abstract void write(int
b) :先将int转换为byte类型,把
低字节写入到输出流中。
public void write(byte
b[ ]):将参数b中的字节写到输出流。 3)
public void write(byte
len) :将参数b的从偏
移量off开始的len个字节写到输出流。
三亿文库包含各类专业文献、幼儿教育、小学教育、专业论文、文学作品欣赏、各类资格考试、高等教育、生活休闲娱乐、外语学习资料、JAVA输入输出流--字节流篇(什么时候用哪个)93等内容。 
 java IO文件输入输出流总结_计算机软件及应用_IT/计算机_专业资料。java I/O文件...则只能用字节流 File f1=new File(“c:\\a.jpg”); FileInputStream fis=...  Java 中面向字节的输入输出流字节流以字节为传输单位,用来读写 8 位的数据,除了能够处理纯文本文件之外,还能用 来处理二进制文件的数据。InputStream 类和 ...  实验6-java输入输出流操作_计算机软件及应用_IT/计算机_专业资料。实验 6:java 输入输出流操作 实验目的: 1. 2. 了解字节流和字符流 了解流的分类 实验步骤:...  java输入输出流报告_计算机软件及应用_IT/计算机_专业资料。Java 简单的文件输入...(); } } } 实验四:用字符流形式将将5个学生的学号,姓名,三门成绩和总府...  输入输出流一、实验目的: 熟悉 Java 的文件读写机制,练习输入输出流的使用。 二、实验内容: 1、键盘输入 10 个整数,从小到大进行排序。 2、接收键盘输入的字符...  下面关于 java 中输入/输出流的说法错误的是( )。 A) FileInputStream 与 FileOutputStream 类用读、写字节流。 B) Reader 与 Writer 类用来读、写字符流。 ...  Java 输入输出流及文件读写详解_IT/计算机_专业资料。I/O 类体系在 JDK API 中,基础的 IO 类都位于 java.io 包,而新实现的 IO 类则位于一系 列以 java...  java 输入输出流实验_计算机软件及应用_IT/计算机_专业...到磁盘时,就可以使用输入/输 出流,简称 I/O 流...实验指导 (1)ZipInputStream 流必须指向一个字节流...  Java 输入输出流 汇总_计算机软件及应用_IT/计算机_专业资料。Java 输入输出流 ...(写) 结论:只要是处理纯文本数据,就要优先考虑使用字符流,除此之外都用字节流...I/O操作,字节流,字符流,字符集编码 - holoblog - ITeye技术网站
I/O操作,字节流,字符流,字符集编码
是放在java.io包中的;
一个File类的对象,表示了磁盘上的文件或目录。
File类提供了与平台无关的方法来对磁盘上的文件或目录进行操作。
import java.io.*;
class FileTest
public static void main(String[] args) throws Exception //IO操作会经常抛出异常,我们交给JVM来处理
//File f=new File("1.txt");
// f.createNewFile(); //创建一个文件1.txt
//f.mkdir(); //创建一个目录:1.txt
/*指定绝对路径:\在JAVA是特殊的转义字符;所以输出\用“\\” */
//File f=new File("E:\\JavaLesson\\Lesson7\\1.txt"); //--但它只能在当前windows系统有效;在Linux下无效
File fDir = new File(File.separator); //定义父目录为根目录;在windows下表示当前运行盘符的根目录!
String strFile="JavaLesson"+File.separator+
"Lesson7"+File.separator+"1.txt"; //win和Linux系统中编译成不同符合;
File f = new File(fDir,strFile); //根目录+当前目录和文件;可移植到Linux;
f.createNewFile();
属性和方法:
a.separator
separator属性用来代表路径斜杠;通用的!
构造函数可以采用父目录+子文件名的方式:
File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
b.删除文件:delete()
boolean delete()
Deletes the file or directory denoted by this abstract pathname.
f.delete() //将f文件删除
c.deleteOnExit
deleteOnExit(); 在程序退出的时候删除文件!
可以用来删除垃圾文件;临时文件等;
d.createTempFile 创建临时文件到临时目录(返回File对象);
static File createTempFile(String prefix, String suffix)
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
for(int i=0;i&5;i++)
File f = File.createTempFile("winsun",".tmp"); //会创建到系统默认的临时文件夹中;
f.deleteOnExit(); //必须手动删除
Thread.sleep(3000);
e.List()方法:
String[] list() ---返回字符串数组; 列出所有文件和子目录;
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
File fDir=new File(File.separator);
String strFile="JavaLesson"+File.separator+"Lesson6";
File f=new File(fDir,strFile);
String[] names=f.list(); //返回一个数组
for(int i=0;i&names.i++)
System.out.println(names[i]); //打印内容
e.2 增加过滤器: -- 增加条件
String[] list(FilenameFilter filter) --增加一个文件过滤器,选择文件
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
其中,FilenameFilter 是一个接口;包含一个方法accept:
boolean accept(File dir, String name) ---- Tests if a specified file should be included in a file list.
File fDir=new File(File.separator);
String strFile="JavaLesson"+File.separator+"Lesson6";
File f=new File(fDir,strFile);
//利用内部类来实现FilenameFilter接口,accept方法使用了一个Sting的indexOf方法:
String[] names=f.list(new FilenameFilter()
public boolean accept(File dir,String name)
return name.indexOf(".java")!=-1;
} //有.java子串时返回真,选所有java文件!
}); //作为方法参数传递
for(int i=0;i&names.i++)
System.out.println(names[i]);
e.3: String.indexOf()
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
功能: 返回子串的索引值,当没有该子串时返回-1
在File类中,没有给我们提供对文件进行读写操作的方法!
3.输入流,输出流 --- 流式I/O
流(Stream)是字节的源或目的。
两种基本的流是:输入流(Input Stream)和输出流(Output Stream)。可从中读出一系列字节的对象称为输入流。而能向其中写入一系列字节的对象称为输出流。
流的分类:
节点流:从特定的地方读写的流类,例如:磁盘或一块内存区域。
过滤流:使用节点流作为输入或输出。过滤流是使用一个已经存在的输入流或输出流连接创建的。 --- 能够获得一些额外的功能!
4.InputStream -- 抽象类!
三个基本的读方法
abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。 -- 返回一个整型,所以int中第一个字节有效,后三个无效!
int read(byte[] b) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。
int read(byte[] b, int off, int len) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。
long skip(long n) :在输入流中跳过n个字节,并返回实际跳过的字节数。
int available() :返回在不发生阻塞的情况下,可读取的字节数。
void close() :关闭输入流,释放和这个流相关的系统资源。-- 创建一个输入流时,OS会分配一些资源;
void mark(int readlimit) :在输入流的当前位置放置一个标记,如果读取的字节数多于readlimit设置的值,则流忽略这个标记。 -- 在InputStream中是个空实现!
void reset() :返回到上一个标记。-- 在InputStream中是个空实现!
boolean markSupported() :测试当前流是否支持mark和reset方法。如果支持,返回true,否则返回false。-- 然后再实现
5. OutputStream
三个基本的写方法
abstract void write(int b) :往输出流中写入一个字节。
void write(byte[] b) :往输出流中写入数组b中的所有字节。
void write(byte[] b, int off, int len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。
void flush() :刷新输出流,强制缓冲区中的输出字节被写出。-- 为了加快输出设备的出入速度,一般先写到缓冲区,然后一次性与I/O设备交互进行输出,但是为了立即输出,则采用此方法!
void close() :关闭输出流,释放和这个流相关的系统资源。
System.out 就是一个PrintStream类型!
System还有两个对象: in, out 从标准输入设备读,从标准输出设备写
6.基本的流类
FileInputStream和FileOutputStream
节点流,用于从文件中读取或往文件中写入字节流。如果在构造FileOutputStream时,文件已经存在,则覆盖这个文件。
import java.io.*;
class StreamTest
public static void main(String[] args) throws Exception
FileOutputStream fos=new FileOutputStream("1.txt");
fos.write(".cn".getBytes()); //write(byte[] b) -- 需要个字节数组,利用String的方法得到!神奇!
fos.close(); //关闭资源
FileInputStream fis=new FileInputStream("1.txt"); //用已存在的文件构造文件输入流对象;
byte[] buf=new byte[100];
int len=fis.read(buf); //从文件中读取数据流到buf数组
System.out.println(new String(buf,0,len)); //用字节数组构造String串,用另一个构造函数
fis.close();
BufferedInputStream和BufferedOutputStream
过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。
BufferedInputStream将mark和reset方法实现了!BufferedOutputStream实现了flush方法
需要用已经存在的FileInputStream和FileOutputStream 来构建它们!
import java.io.*;
class StreamTest
public static void main(String[] args) throws Exception
FileOutputStream fos=new FileOutputStream("1.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos); //用fos来构建bos
bos.write(".cn".getBytes()); //将字符串写入bos对象
bos.flush(); //或者用 bos.close(); 也能将缓冲区的数据写入磁盘! 关了它就无需关闭fos了!-- 连锁
FileInputStream fis=new FileInputStream("1.txt");
BufferedInputStream bis=new BufferedInputStream(fis); //用fis构建bis
byte[] buf=new byte[100];
int len=bis.read(buf); //读出对象内容到buf中
System.out.println(new String(buf,0,len));
bis.close(); //无需再fis.
DataInputStream和DataOutputStream
过滤流,需要使用已经存在的节点流来构造,提供了读写Java中的基本数据类型的功能。
是由FilterInputStream继承而来,实现了DataInput接口(基本类型的读取);Output类似!
Constructor:
DataOutputStream(OutputStream out)
import java.io.*;
class StreamTest
public static void main(String[] args) throws Exception
FileOutputStream fos=new FileOutputStream("1.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
DataOutputStream dos=new DataOutputStream(bos); //也可以用fos!
char ch='a';
float f=4.5f;
dos.writeByte(b); //dos中实现的方法
dos.writeInt(i);
dos.writeChar(ch);
dos.writeFloat(f);
dos.close(); //由于我们链接了一个bos,所以有buffer的功能!关闭才能写入!
FileInputStream fis=new FileInputStream("1.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
DataInputStream dis=new DataInputStream(bis);
System.out.println(dis.readByte());
System.out.println(dis.readInt());
System.out.println(dis.readChar());
System.out.println(dis.readFloat());
dis.close();
PipedInputStream和PipedOutputStream
管道流,用于线程间的通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须同时构造管道输入流和管道输出流。
可用无参构造,然后用connect()方法进行连接;
也可在构造的时候直接指定管道输出流进行连接!
PipedInputStream(PipedOutputStream src)
Creates a PipedInputStream so that it is connected to the piped output stream src.
同样,PipedOutputStream也有类似的方法!
注意管道流是由输入输出流继承而来的而不是过滤流FilterInput(Output)Stream继承而来的!
import java.io.*;
class PipedStreamTest
public static void main(String[] args) throws Exception
PipedOutputStream pos=new PipedOutputStream();
PipedInputStream pis=new PipedInputStream();
pos.connect(pis); //也可以反过来连接,或者构造的时候连接!
new Producer(pos).start();
new Consumer(pis).start();
catch(Exception e)
e.printStackTrace();
class Producer extends Thread //用管道输出流定义生产者
private PipedOutputS
public Producer(PipedOutputStream pos)
public void run()
pos.write("Hello,welcome you!".getBytes());
pos.close();
catch(Exception e)
e.printStackTrace();
class Consumer extends Thread //用管道输入流定义消费者
private PipedInputS
public Consumer(PipedInputStream pis)
public void run()
byte[] buf=new byte[100];
int len=pis.read(buf); //无实体文件,直接从输入流中读(得到的是对应输出流中的数据)
System.out.println(new String(buf,0,len)); //字节数组,偏移量,长度
pis.close();
catch(Exception e)
e.printStackTrace();
7.Java I/O库的设计原则
Java的I/O库提供了一个称做链接的机制,可以将一个流与另一个流首尾相接,形成一个流管道的链接。这种机制实际上是一种被称为Decorator(装饰)设计模式的应用。
通过流的链接,可以动态的增加流的功能,而这种功能的增加是通过组合一些流的基本功能而动态获取的。
我们要获取一个I/O对象,往往需要产生多个I/O对象,(如buffer流产生就需要先构造基本输入输出流)这也是Java I/O库不太容易掌握的原因,但在I/O库中Decorator模式的运用,给我们提供了实现上的灵活性。
之前的都是字节流;下面介绍字符流:
8.Reader和Writer
Java程序语言使用Unicode来表示字符串和字符。
Reader和Writer这两个抽象类主要用来读写字符流。
BufferedRead并没有从FilterReader中派生而来(不同于字节流中的层次),是sun的一个bug!
InputStreamReader起到了一个桥梁的作用,用来进行字符流与字节流之间的相互转换!
BufferedWriter以及OutputStreamWriter与上面对应!
BufferedReader,BufferedWriter提供了对字符的快速读取
构造函数:
OutputStreamWriter(OutputStream out)
Creates an OutputStreamWriter that uses the default character encoding.
import java.io.*;
class StreamTest
public static void main(String[] args) throws Exception
FileOutputStream fos=new FileOutputStream("1.txt");
OutputStreamWriter sw=new OutputStreamWriter(fos); //字节流到字符流的桥梁!
BufferedWriter bw=new BufferedWriter(osw); //写操作一般用这个而不用OSW,更高效!
/*Constructor:BufferedWriter(Writer out) Creates a buffered character-output stream
that uses a default-sized output buffer. -- OSW是Writer的子类,所以也可以用来构造*/
bw.write(".cn");
bw.close();
FileInputStream fis=new FileInputStream("1.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br =new BufferedReader(isr);
System.out.println(br.readlLine());
br.close();
从键盘接收多行输入,然后输出到屏幕:
InputStreamReader isr=new InputStreamReader(System.in); //不用文件输入流(fis)构造了,而直接用in
BufferedReader br=new BufferedReader(isr);
String strL
while((strLine=br.readLine())!=null) //按行读取
System.out.println(strLine);
br.close();
9.字符集的编码
ASCII(American Standard Code for Information Interchange,美国信息互换标准代码),是基于常用的英文字符的一套电脑编码系统。我们知道英文中经常使用的字符、数字符号被计算机处理时都是以二进制码的形式出现的。这种二进制码的集合就是所谓的ASCII码。每一个ASCII码与一个8位(bit)二进制数对应。其最高位是0,相应的十进制数是0-127。如,数字?°0?±的编码用十进制数表示就是48。另有128个扩展的ASCII码,最高位都是1,由一些制表符和其它符号组成。ASCII是现今最通用的单字节编码系统。
GB2312:GB2312码是中华人民共和国国家汉字信息交换用编码,全称《信息交换用汉字编码字符集-基本集》。主要用于给每一个中文字符指定相应的数字,也就是进行编码。一个中文字符用两个字节的数字来表示,为了和ASCII码有所区别,将中文字符每一个字节的最高位置都用1来表示。
GBK:为了对更多的字符进行编码,国家又发布了新的编码系统GBK(GBK的K是?°扩展?±的汉语拼音第一个字母)。在新的编码系统里,除了完全兼容GB2312 外,还对繁体中文、一些不常用的汉字和许多符号进行了编码。
ISO-8859-1:是西方国家所使用的字符编码集,是一种单字节的字符集 ,而英文实际上只用了其中数字小于128的部分。
Unicode:这是一种通用的字符集,对所有语言的文字进行了统一编码,对每一个字符都用2个字节来表示,对于英文字符采取前面加“0”字节的策略实现等长兼容。如 “a” 的ASCII码为0x61,UNICODE就为0x00,0x61。
UTF-8:Eight-bit UCS Transformation Format,(UCS,Universal Character Set,通用字符集,UCS 是所有其他字符集标准的一个超集)。一个7位的ASCII码值,对应的UTF码是一个字节。如果字符是0x0000,或在0xf之间,对应的UTF码是两个字节,如果字符在0x0800与0xffff之间,对应的UTF码是三个字节。
10.CharsetTest
Charset类有个方法能列出可以使用的字符集:
static SortedMap&String,Charset& availableCharsets()
Constructs a sorted map from canonical charset names to charset objects.
import java.util.*;
import java.nio.charset.*;
class CharsetTest
public static void main(String[] args) throws Exception
Map m=Charset.availableCharsets(); //调用静态方法
Set names=m.keySet(); //map接口的keyset方法返回key的集合
Iterator it=names.iterator(); //集合没有get方法,用迭代器
while(it.hasNext())
System.out.println(it.next());
java.lang.System.有个getProperty方法
Properties pps=System.getProperties(); //属性类
pps.list(System.out); //流出所有系统属性 file.encoding
在java中,字符,字符串都是用unicode来构成的;所以得到unicode的过程就是解码,用unicode转化成字节的过程就是编码!
import java.util.*;
import java.nio.charset.*; //new io
class CharsetTest
public static void main(String[] args) throws Exception
Properties pps=System.getProperties(); //属性类
pps.list(System.out); //流出所有系统属性 file.encoding
pps.put("file.encoding","ISO-8859-1"); //设置属性的方法!
byte[] buf=new byte[100];
while((data=System.in.read())!='q') //从键盘读取中文字符,q退出;读取使用OS默认charset:中文GBK!
//在标准设备读取时,依赖于本地操作系统字符集
buf[i]=(byte) //中文会以GBK码值存储;默认charset
String str=new String(buf,0,i); //获取字符 --- 解码!解码使用JVM指定的编码:ISO...
System.out.println(str); //打印出乱码! -- Java 1.6中正常打印, Why?
String strGBK=new String (str.getBytes("ISO-8859-1"),"GBK"); //用ISO先编码回去,然后再用GBK重新解码!!!!
System.out.println(strGBK);
11. RandomAccessFile
RandomAccessFile类同时实现了DataInput和DataOutput接口(-- 所以实现了对基本数据类型的读写!),提供了对文件随机存取的功能,利用这个类可以在文件的任何位置读取或写入数据。
RandomAccessFile类提供了一个文件指针,用来标志要进行读写操作的下一数据的位置。
Constructor:
RandomAccessFile(String name, String mode)
Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
--- mode:Value Meaning:
"r" Open for reading only. Invoking any of the write methods of the resulting object will cause an IOException to be thrown.
"rw" Open for reading and writing. If the file does not already exist then an attempt will be made to create it.
"rws" Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be written synchronously to the underlying storage device.
"rwd" Open for reading and writing, as with "rw", and also require that every update to the file's content be written synchronously to the underlying storage device.
writeBytes(String str) //中文字符会
writeChars(String str)
writeUTF(String str) //在前两个字节中记录了字符的长度! 常用这个!
11.1: long length()
Returns the length of this file.
11.2: long getFilePointer()
Returns the current offset in this file. offset:偏移量!
11.3: void seek(long pos)
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
import java.io.*;
class RandomFileTest
public static void main(String[] args) throws Exception
Student s1=new Student(1, "zhangsan", 98.5);
Student s2=new Student(2, "sdsan", 74.5);
Student s3=new Student(3, "adsfn", 81.5);
RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Student s=new Student();
raf.seek(0);
for(long i=0;i&raf.length();i=raf.getFilePointer()) //因为length返回值为long故i也定义为long!
//getFilePointer() 获得文件中当前的偏移量!
s.readStudent(raf);
System.out.println(s.num+":"+s.name+":"+s.score);
raf.close();
class Student
public Student()
public Student(int num, String name, double score)
this.name=
this.score=
public void writeStudent(RandomAccessFile raf) throws IOException
raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);
public void readStudent(RandomAccessFile raf) throws IOException
num=raf.readInt();
name=raf.readUTF();
score=raf.readDouble();
} //随时注意文件指针!
12.对象序列化
将对象转换为字节流保存起来,并在日后还原这个对象,这种机制叫做对象序列化。
将一个对象保存到永久存储设备上称为持续性。
一个对象要想能够实现序列化,必须实现Serializable接口或Externalizable接口。--IO包内; Serializable接口中没有方法,是个空接口!标示接口!Externalizable是从Serializable继承而来的!
要想实现对象的序列化,我们可以利用两个类:ObjectOutputStream/ObjectInputStream 它们实现了DataOutput/DataInput对象,可以读写基本数据类型!
其中有个方法writeObject(),写入对象成员!
ObjectOutputStream(OutputStream out) -- 将对象信息转化成字节流写入文件中保存!
Creates an ObjectOutputStream that writes to the specified OutputStream.
import java.io.*;
class ObjectSerialTest
public static void main(String[] args) throws Exception
Employee e1=new Employee("zhangsan",25,3000.50);
Employee e2=new Employee("lisi",24,3200.40);
Employee e3=new Employee("wangwu",27,3800.55);
FileOutputStream fos=new FileOutputStream("employee.txt");
ObjectOutputStream os=new ObjectOutputStream(fos);
oos.writeObject(e1);
oos.writeObject(e2);
oos.writeObject(e3);
oos.close();
FileInputStream fis=new FileInputStream("employee.txt");
ObjectInputStream is=new ObjectInputStream(fis);
for(int i=0;i&3;i++)
e=(Employee)ois.readObject(); // 反序列化的时候并不会调用构造函数!
System.out.println(e.name+":"+e.age+":"+e.salary);
ois.close();
class Employee implements Serializable
transient Thread t=new Thread();
public Employee(String name,int age,double salary)
this.name=
this.salary=
private void writeObject(java.io.ObjectOutputStream oos) throws IOException //重新定义写方法,实现自己的控制,如薪水加密后再保存,可无
oos.writeInt(age);
oos.writeUTF(name);
System.out.println("Write Object");
private void readObject(java.io.ObjectInputStream ois) throws IOException //重新定义读方法,可无!
age=ois.readInt();
name=ois.readUTF();
System.out.println("Read Object");
当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量。
如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存。
如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作将会失败,并且会抛出一个NotSerializableException。我们可以将这个引用标记为transient,那么对象仍然可以序列化。
InputStream和OutputStream:字节流的输入输出。--抽象基类!
FileInputStream和FileOutputStream: 用来对文件进行读写操作;
BufferedInputStream和BufferedOutputStream: 提供了带缓冲的读写;
DataInputStream和DataOutputStream: 提供了读写JAVA基本数据类型的功能;
PipedInputStream和PipedOutputStream:在两个线程中间通信
ObjectInputStream和ObjectOutputStream:完成对象的序列化和反序列化;
Reader和Writer:字符流的输入输出。-- 抽象基类!:1.0中只提供字节流操作;JDK1.1以后新出现字符流;
-- 派生了:
InputStreamRead/OutputStreamWriter:提供了字节流到字符流转换的桥梁!
BufferedReader/Writer: 提高了读写字符流的效率!比较常用
流的链接(Java I/O库的设计原则) -- 功能组合在一起形成功能更加强大的机制!
八. Java图形界面编程
Java图形界面编程,AWT、布局管理器、事件模型,JFC、Swing编程。应用JBuilder快速开发图形界面程序。
AWT(Abstract Window Toolkit),抽象窗口工具包,SUN公司提供的用于图形界面编程(GUI)的类库。基本的AWT库处理用户界面元素的方法是把这些元素的创建和行为委托给每个目标平台上(Windows、Unix、Macintosh等)的本地GUI工具进行处理。例如:如果我们使用AWT在一个Java窗口中放置一个按钮,那么实际上使用的是一个具有本地外观和感觉的按钮。这样,从理论上来说,我们所编写的图形界面程序能运行在任何平台上,做到了图形界面程序的跨平台运行。
Class Component --JAVA中所有图形界面组件的抽象基类
=》派生:Container -- 容器对象是可以容纳其它组件的组件
=》派生: Window -- 顶层窗口,无边框无菜单
=》派生: Frame. -- 顶层窗口,有一个边框和一个标题
Component.setVisible(boolean b) //为真则显示
Window.show() //显示窗口并带到前端!
public static void main(String[] args)
Frame. f = new Frame("mybole");
f.setSize(600,400);
f.setLocation(100,100);
f.setBackground(Color.CYAN);
Button btn = new Button("winsun");
f.add(btn);
2.布局管理器
容器里组件的位置和大小是由布局管理器来决定的。容器对布局管理器的特定实例保持一个引用。当容器需要定位一个组件时,它将调用布局管理器来完成。当决定一个组件的大小时,也是如此。
在AWT中,给我们提供了五种布局管理器:
a. BorderLayout ---- Frame. 的缺省布局管理器;分为东西南北中五个方向!
BorderLayout (int 水平间隙, int 水平间隙)
package lesson8;
import java.awt.*;
public class MyFrame
public static void main(String[] args)
Frame. f = new Frame("mybole");
f.setSize(600,400);
f.setLocation(100,100);
f.setBackground(Color.BLUE);
f.setLayout(new BorderLayout(10,10)); //设置Layout:BorderLayout,设置间隙!
Button btn1 = new Button("North");
Button btn2 = new Button("South");
Button btn3 = new Button("West");
Button btn4 = new Button("East");
Button btn5 = new Button("Center");
f.add(btn1,"North");
f.add(btn2,"South");
f.add(btn3,"West");
f.add(btn4,"East");
f.add(btn5,"Center");
b. FlowLayout //依次摆放每个组件;
f.setLayout(new FlowLayout(FlowLayout.LEFT)); //添加参数,靠左开始;
c. GridLayout -- 网格状摆放
构造函数可无参;
GridLayout(int rows, int cols) -- 参数指定几行几列
Creates a grid layout with the specified number of rows and columns.
CardLayout
e. GridBagLayout -- 五种里最复杂的;平时很少使用;(很少直接使用)
我们可以通过设置空布局管理器,来控制组件的大小和位置。调用setLayout(null)。
在设置空布局管理器后,必须对所有的组件调用setLocation(),setSize()或setBounds(),将它们定位在容器中。
3.如何关闭窗口;
AWT事件模型
Events(事件):描述发生了什么的对象。
Event source(事件源):事件的产生器。
Event handlers(事件处理器):接收事件对象、解释事件对象并处理用户交互的方法。
JDK1.0的事件模型:层次模型;
Frame-&Panel-&Button -- 产生事件:可以传递给它的容器,返回给上一层;
JDK1.1的事件模型:委托模型 -- 将事件的执行分开
事件监听器:实现了监听器接口的类。一个监听器对象是一个实现了专门的监听器接口的类的实例。-- awt.envent.windowListener
窗口事件必须委托给窗口监听器!
f.addWindowListener(new MyWindowListener());
class MyWindowListener implements WindowListener
public void windowOpened(WindowEvent e)
public void windowClosing(WindowEvent e)
System.exit(0);
public void windowClosed(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
法2:JBuilder提供了一个接口实现向导可以直接实现所有方法;
法3:JAVA 提供了一个抽象类WindowAdapter(实现了监听器WindowListener--空实现!) -- 继承它并实现需要的方法即可:
class HisWindowListener extends WindowAdapter
public void windowClosing(WindowEvent e)
System.exit(0);
法4:匿名内部类 -- 用Window适配器类实现;
//使用了一个匿名的内部类对象
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
CTRL+G --- JBuilder提供的代码生成功能!!
空白处按得到全部列表;
out CTRL+g 得到一个println语句!
想用四种布局管理器,用新的容器Panel--它是Frame的子类;
package lesson8;
import java.awt.*;
import java.awt.event.*;
public class YourFrame. extends Frame
private Panel borderP
private Panel flowP
private Panel gridP
private Panel cardP
public YourFrame(String title) //Constructor
super(title); //传递基类标题
setSize(600,400);
setLocation(100,100);
setBorderLayoutPanel(); //对4个panel初始化
setFlowLayoutPanel();
setGridLayoutPanel();
setCardLayoutPanel();
setLayout(new GridLayout(2,2)); //框架窗口初始化,两行两列,刚好4个panel
add(borderPanel);
add(flowPanel);
add(gridPanel);
add(cardPanel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
public void setBorderLayoutPanel()
borderPanel=new Panel();
borderPanel.setLayout(new BorderLayout());
Button btn1=new Button("North");
Button btn2=new Button("South");
Button btn3=new Button("West");
Button btn4=new Button("East");
Button btn5=new Button("Center");
borderPanel.add(btn1,BorderLayout.NORTH);
borderPanel.add(btn2,BorderLayout.SOUTH);
borderPanel.add(btn3,BorderLayout.WEST);
borderPanel.add(btn4,BorderLayout.EAST);
borderPanel.add(btn5,BorderLayout.CENTER);
public void setFlowLayoutPanel()
flowPanel=new Panel();
Button btn1=new Button("mybole");
btn1.addActionListener(new ActionListener() { //增加按钮响应
public void actionPerformed(ActionEvent e) {
((Button)e.getSource()).setLabel("weixin"); //改变button上的文本!
Button btn2=new Button("winsun");
flowPanel.add(btn1);
flowPanel.add(btn2);
public void setGridLayoutPanel()
gridPanel=new Panel();
gridPanel.setLayout(new GridLayout(2,2));
Button btn1=new Button("Button1");
Button btn2=new Button("Button2");
Button btn3=new Button("Button3");
Button btn4=new Button("Button4");
gridPanel.add(btn1);
gridPanel.add(btn2);
gridPanel.add(btn3);
gridPanel.add(btn4);
public void setCardLayoutPanel()
final CardLayout cl=new CardLayout();
cardPanel=new Panel();
cardPanel.setLayout(cl);
Button btn1=new Button("黑桃A");
Button btn2=new Button("红桃K");
ActionListener al=new ActionListener() //ActionListener是个接口,所以不能实例化,直接实现
public void actionPerformed(ActionEvent e)
cl.next(cardPanel); //内部类访问cl变量,它必须定义为final的!
btn1.addActionListener(al); //注册监听器
btn2.addActionListener(al); //可以使用同一个
cardPanel.add(btn1,"1");
cardPanel.add(btn2,"2");
public static void main(String[] args) throws HeadlessException
YourFrame. yf = new YourFrame(".cn");
yf.show();
4.在AWT中创建一个菜单需要创建:
MenuBar-》Menu-》MenuItem
SetMenubar()
package lesson8;
import java.awt.*;
public class HisFrame
public static void main(String[] args)
Frame. f=new Frame(".cn");
f.setSize(600,400);
f.setLocation(100,100);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
MenuBar mb=new MenuBar();
Menu m1=new Menu("File");
Menu m2=new Menu("Edit");
MenuItem mi1=new MenuItem("New");
MenuItem mi2=new MenuItem("Open");
MenuItem mi3=new MenuItem("Save");
MenuItem mi4=new MenuItem("Exit");
MenuItem mi5=new MenuItem("Copy");
MenuItem mi6=new MenuItem("Paste");
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m2.add(mi5);
m2.add(mi6);
mb.add(m1);
mb.add(m2);
f.setMenuBar(mb);
a.实现功能打开并显示一个文件:
MenuItem mi2=new MenuItem("Open");
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileDialog fd=new FileDialog(f,"Weixin Open File Dialog",
FileDialog.LOAD); //(父Frame,title,model)
fd.show();
String strFile=fd.getDirectory()+fd.getFile(); //路径+文件名
if(strFile!=null)
FileInputStream fis=new FileInputStream(strFile);
byte[] buf=new byte[10*1024];
int len=fis.read(buf);
tf.append(new String(buf,0,len));
fis.close();
catch (Exception ex) {
ex.printStackTrace();
创建对话框用FileDialog类,需指定一个父Frame;
显示文本的方法:
1.TextField --- 可以指定初始文本,但是只能显示单行文本
2.TextArea -- 多行文本
MenuItem mi4=new MenuItem("Exit");
mi4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
其它常用组件,看例子代码即可:
Chois -- 下拉列表框
Checkbox -- 复选框
Label -- 标签
JAVA基础类:
JFC(Java Foundation Classes):Java基础类,是关于GUI组件和服务的完整集合,主要包含5个API:AWT、Java2D、Accessibility、Drag & Drop、Swing。JFC提供了帮助开发人员设计复杂应用程序的一整套应用程序开发包。
Java2D是一套图形API,它为Java应用程序提供了一套高级的有关二维(2D)图形图像处理的类。Java2D API扩展了java.awt和java.awt.image类,并提供了丰富的绘图风格,定义了复杂图形的机制和精心调节绘制过程的方法和类。这些API使得独立于平台的图形应用程序的开发更加简便。
Accessibility API提供了一套高级工具,用以辅助开发使用非传统输入和输出的应用程序。它提供了一个辅助的技术接口,如:屏幕阅读器,屏幕放大器,听觉文本阅读器(语音处理)等等。
Drag & Drop技术提供了Java和本地应用程序之间的互操作性,用来在Java应用程序和不支持Java技术的应用程序之间交换数据。
JFC模块的重点在Swing。Swing用来进行基于窗口的应用程序开发,它提供了一套丰富的组件和工作框架,以指定GUI如何独立于平台地展现其视觉效果。
Swing的所有元素都是基于JComponent的; -- 由它派生!
包javax.swing
所有swing元素都是J开头:JButton,JCheckBox
6.可以用图形界面生成器生成一个框架;(Swing)
用Application向导生成appli并在图形界面手动设计界面
AWT.event.*; 事件
MouseEvent类的方法:
isPopupTrigger()是否是弹出事件的触发事件;
getComponent()获取事件发生的父组件;
getX(),getY() 获取鼠标当前位置;
a.鼠标右键弹出菜单:
void contentPane_mouseReleased(MouseEvent e) //鼠标键松开事件
if(e.isPopupTrigger()) //判断是否为右键;
jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());
b.右键菜单show增加事件响应:
void jMenuItem3_actionPerformed(ActionEvent e) //弹出消息框
JOptionPane.showMessageDialog(null,"show"); //Swing类方法 -- JOptionPane.showMessageDialog
static void showMessageDialog(Component parentComponent, Object message)
Brings up an information-message dialog titled "Message".
图形界面的编程不是java的强项;而且运行速度较慢!
awt和swing用法不用强记,会用就
5行,使用时利用帮助文档和相关书籍现学现卖就可以了!
浏览: 465780 次
浏览量:9041
我这是一直点击Table标签,最后点提交就变成这样了..你删掉 ...
&a href=&http://www.bai ...
http://blog.csdn.net/crazyjixia ...
转载需要请写下 转载地址http://blog.csdn.ne ...

我要回帖

更多关于 微信被投诉要多久解封 的文章

 

随机推荐