7. 三、 标准输入输出在System类中有三个静态域System.in、System.out和System.err。
可以在标准位置使用这三个系统流进行I/O操作。
public static InputStream in
读取字符数据的标准输入流。
public static PrintStream out
显示或打印输出信息的标准输出流
public static PrintStream err
输出错误信息的标准错误流。
39. (二) InetAddress类 java.net.InetAddress类是Java的IP地址封装类,它不需要用户了解如何实现地址的细节。
定义:
public final class InetAddress extends object implements Serializable
该类用hostName(String)和address(int)拜师网络地址,即主机名和IP地址。这两个字段是不公开的,不能直接访问。
40. 创建InetAddress对象的方法InetAddress类没有构造方法,要创建该类的实例对象,可以通过该类的静态方法获得该对象。
public static InetAddress getLocalHost()
方法getLocalHost()获得本地机的InetAddress对象,当查找不到本地机器的地址时,触发一个UnknownHostException异常。
48. (3) public URL(String protocol, String host, String file);
new URL("http", "www.gamelan.com", "/pages/Gamelan.net. html");
(4) public URL(String protocol, String host, int port, String file);
URL gamelan=new URL("http", "www.game.com", 80, "Pages/Gamelan.html");
49. 解析一个URLpublic 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的标记
51. 读取html文件public class URLRead {
public static void main(String[] args) throws Exception {
URL web = new URL("http://www.lytu.edu.cn");
BufferedReader in = new BufferedReader(new InputStreamReader(web.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)System.out.println(inputLine);
in.close();
56. try {
URL url = new URL(urlString);
File file = new File(url.getFile());
String filename = file.getName();
//为了得到不含路径的文件名
in = new BufferedInputStream(url.openStream());
out = new BufferedOutputStream(
new FileOutputStream("c:\\temp\\" + url.getFile()));