java 常用API 方法

java 常用API 方法
 1
Reader类是处理所有字符流输入类的父类。 
读取字符 
  public int read() throws IOException; //读取一个字符,返回值为读取的字符 
  public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/ 
  public abstract int read(char cbuf[],int off,int len) throws IOException; 
  /*读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现*/ 
标记流 
  public boolean markSupported(); //判断当前流是否支持做标记 
  public void mark(int readAheadLimit) throws IOException; 
   //给当前流作标记,最多支持readAheadLimit个字符的回溯。 
  public void reset() throws IOException; //将当前流重置到做标记处 
关闭流 
  public abstract void close() throws IOException; 
2
Writer类是处理所有字符流输出类的父类。 
向输出流写入字符 
  public void write(int c) throws IOException 
  //将整型值c的低16位写入输出流 
  public void write(char cbuf[]) throws IOException 
  //将字符数组cbuf[]写入输出流 
  public abstract void write(char cbuf[],int off,int len) throws IOException 
  //将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流 
  public void write(String str) throws IOException 
  //将字符串str中的字符写入输出流 
  public void write(String str,int off,int len) throws IOException 
  //将字符串str 中从索引off开始处的len个字符写入输出流 
flush( ) 
  刷空输出流,并输出所有被缓存的字节。 
关闭流 
  public abstract void close() throws IOException 
生成流对象 
  public InputStreamReader(InputStream in); 
  /*in是字节流,而InputStreamReader是字符流,但是其来源是字节流in 
  因此InputStreamReader就可以把字节流in转换成字符流处理。/* 
  public InputStreamReader(InputStream in,String enc) throws UnsupportedEncodingException; 
  /*enc是编码方式,就是从字节流到字符流进行转换时所采用的编码方式, 
   例如 ISO8859-1UTF-8UTF-16等等*/ 
  public OutputStreamWriter(OutputStream out); 
  /*out是字节流,而OutputStreamReader是字符流 */ 
  public OutputStreamWriter(OutputStream out,String enc) throws UnsupportedEncodingException; 
  //enc
是编码方式 
  InputStreamReaderOutputStreamWriter的方法: 
读入和写出字符 
  基本同ReaderWriter 
获取当前编码方式 
  public String getEncoding(); 
关闭流 
  public void close() throws IOException; 

生成流对象 
  public BufferedReader(Reader in); //使用缺省的缓冲区大小 
  public BufferedReader(Reader in, int sz); //sz为缓冲区的大小 
  public BufferedWriter(Writer out); 
  public BufferedWriter(Writer out, int sz); 
读入/写出字符 
  除了ReaderWriter中提供的基本的读写方法外,增加对整行字符的处理。 
  public String readLine() throws IOException; //读一行字符 
  public void newLine() throws IOException; //写一行字符 
内存的读/ 
1.ByteArrayInputStream
ByteArrayOutputStream 
  ByteArrayInputStream //从字节数组中读取以字节为单位的数据 
  ByteArrayOutputStream //向字节数组中写入以字节为单位的数据 
2
StringBufferInputStreamStringBufferOutputStream 
  StringBufferInputStream 

    //从字符串缓冲区StringBuffer中读取以字符为单位的数据 
  StringBufferOutputStream 
  //向字符串缓冲区StringBuffer中写入以字符为单位的数据 

AWT图形用户界面设计 
AWT
事件及其相应的监听器接口 
事件类别 描述信息接口名 方法 
 ActionEvent 激活组件   ActionListener  actionPerformed(ActionEvent) 
 ItemEvent 选择了某些项目   ItemListener  itemStateChanged(ItemEvent) 
 MouseEvent 鼠标移动   MouseMotionListener  mouseDragged(MouseEvent) 
 mouseMoved(MouseEvent) 
鼠标点击等   MouseListener  mousePressed(MouseEvent) 
 mouseReleased(MouseEvent) 
 mouseEntered(MouseEvent) 
 mouseExited(MouseEvent) 
 mouseClicked(MouseEvent) 
 KeyEvent 键盘输入   KeyListener  keyPressed(KeyEvent) 
 keyReleased(KeyEvent) 
 keyTyped(KeyEvent) 
 FocusEvent 组件收到或失去焦点   FocusListener  focusGained(FocusEvent) 
 focusLost(FocusEvent) 
 AdjustmentEvent 移动了滚动条等组件   AdjustmentListener  adjustmentValueChanged(AdjustmentEvent) 
 ComponentEvent 对象移动缩放显示隐藏等   ComponentListener  componentMoved(ComponentEvent) 
 componentHidden(ComponentEvent) 
 componentResized(ComponentEvent) 
 componentShown(ComponentEvent) 
 WindowEvent 窗口收到窗口级事件   WindowListener  windowClosing(WindowEvent) 
 windowOpened(WindowEvent) 
 windowIconified(WindowEvent) 
 windowDeiconified(WindowEvent) 
 windowClosed(WindowEvent) 
 windowActivated(WindowEvent) 
 windowDeactivated(WindowEvent) 
 ContainerEvent 容器中增加删除了组件   ContainerListener  componentAdded(ContainerEvent) 
 componentRemoved(ContainerEvent) 
 TextEvent 文本字段或文本区发生改变   TextListener  textValueChanged(TextEvent) 
其余略过 

Java的线程和Java Applet 
Applet
和浏览器间的通信 
getDocumentBase( ) //
返回当前网页所在的URL 
  getCodeBase( ) //返回当前applet所在的URL 
  getImage(URL base,String target) //返回网址URL中名为target的图像 
  getAudioClip(URL base,String target) 
                 //返回网址URL中名为target的声音对象 
  getParameter(String target ) //提取HTML文件中名为target的参数的值 
同页Applet间的通信 
  在java.applet.Applet类中提供了如下方法得到当前运行页的环境上下文AppletContext对象。 
  public AppletContext getAppletContext(); 
通过AppletContext对象,可以得到当前小应用程序运行环境的信息。AppletContext是一个接口, 
其中定义了一些方法可以得到当前页的其它小应用程序,进而实现同页小应用程序之间的通信。 
1.
得到当前运行页的环境上下文AppletContext对象 
     public AppletContext getAppletContext(); 
2.
取得名为nameApplet对象 
     public abstract Applet getApplet(String name); 
3.
得到当前页中所有Applet对象 
     public abstract Enumeration getApplets(); 

Swing用户界面设计 
Swing
 
         描述 
Com.sum.swing.plaf.motif
用户界面代表类,它们实现Motif界面样式   
Com.sum.java.swing.plaf.windows
用户界面代表类,它们实现Windows界面样式 
Javax.swing
  Swing组件和使用工具 
Javax.swing.border
    Swing轻量组件的边框 
Javax.swing.colorchooser
  JcolorChooser的支持类/接口  

3 java
常用API 
 Javax.swing.event
  事件和侦听器类 
Javax.swing.filechooser
  JFileChooser的支持类/接口 
Javax.swing.pending
  未完全实现的Swing组件 
Javax.swing.plaf
  抽象类,定义UI代表的行为 
Javax.swing.plaf.basic
  实现所有标准界面样式公共功能的基类 
Javax.swing.plaf.metal
用户界面代表类,它们实现Metal界面样式 
Javax.swing.table
  Jtable组件 
Javax.swing.text
  支持文档的显示和编辑 
Javax.swing.text.html
  支持显示和编辑HTML文档 
Javax.swing.text.html.parser
  Html文档的分析器 
Javax.swing.text.rtf
  支持显示和编辑RTF文件 
Javax.swing.tree
  Jtree组件的支持类 
Javax.swing.undo
  支持取消操作  

Java网络编程 
为了表示URL java.net中实现了类URL。我们可以通过下面的构造方法来初始化一个URL对象: 
public URL (String spec); 
     通过一个表示URL地址的字符串可以构造一个URL对象。 
     URL urlBase=new URL("http://www. 263.net/") 
public URL(URL context, String spec); 
     通过基URL和相对URL构造一个URL对象。 
     URL net263=new URL ("http://www.263.net/"); 
     URL index263=new URL(net263, "index.html") 
public URL(String protocol, String host, String file); 
     new URL("http", "www.gamelan.com", "/pages/Gamelan.net. html"); 
public URL(String protocol, String host, int port, String file); 
     URL gamelan=new URL("http", "www.gamelan.com", 80, "Pages/Gamelan.network.html"); 
注意:类URL的构造方法都声明抛弃非运行时例外(MalformedURLException),因此生成URL对象时, 
我们必须要对这一例外进行处理,通常是用try-catch语句进行捕获。格式如下: 

try{ 
URL myURL= new URL(…) 
}catch (MalformedURLException e){ 
  … 
   (放置例外的编码的) 
  … 

这个应该很熟悉的,哈哈,前天还有朋友在邪恶八进制的回帖里面问这一句什么意思。 
一个URL对象生成后,其属性是不能被改变的,但是我们可以通过类URL所提供的方法来获取这些属性: 
public String getProtocol()
获取该URL的协议名。 
public String getHost()
获取该URL的主机名。 
public int getPort()
获取该URL的端口号,如果没有设置端口,返回-1 
public String getFile()
获取该URL的文件名。 
public String getRef()
获取该URL在文件中的相对位置。 
public String getQuery()
获取该URL的查询信息。 
public String getPath()
获取该URL的路径 
public String getAuthority()
获取该URL的权限信息 
public String getUserInfo()
获得使用者的信息 
public String getRef()
获得该URL的锚 
当我们得到一个URL对象后,就可以通过它读取指定的WWW资源。这时我们将使用URL的方法openStream(),其定义为: 
         InputStream openStream(); 
方法openSteam()与指定的URL建立连接并返回InputStream类的对象以从这一连接中读取数据。 
public class URLReader { 
public static void main(String[] args) throws Exception{ (
声明抛出所有例外) 
URL tirc = new URL("http://www.eviloctal.com/forum/");
(构建一URL对象)                     
BufferedReader in = new BufferedReader(new InputStreamReader(tirc.openStream())); 
 (使用openStream得到一输入流并由此构造一个BufferedReader对象) 
String inputLine; 
while ((inputLine = in.readLine()) != null)
(一直读,读完为止) 
  System.out.println(inputLine); (输出) 
in.close();
关闭输入流) 
 

例子  
下面是一个典型的创建客户端 Socket 的过程。  
try{ 
  Socket socket=new Socket("127.0.0.1",4700);        
}catch(IOException e){ 
  System.out.println("Error:"+e); 

这是最简单的在客户端创建一个 Socket 的一个小程序段,也是使用 Socket 进行网络通讯的  
第一步,程序相当简单,在这里不作过多解释了。  
java
这种语言因为能跨平台,还有就是安全性高,关键是后者,估计在网络上的应用会越来越多  
,至少目前来说好像重要性还在日益提高,前天我去交上网的费用,偶尔看了一眼铁通的内线的机器  
恰好操作员在操作,我发现服务端就是这个东西写的。   

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值