1.java中的网络功能提供的四大类:
2.InetAddress类的使用:
public static void main(String[] args) {
try {
// 获取本机的InetAddress实例
InetAddress address = InetAddress.getLocalHost();
System.out.println("计算机名:" + address.getHostName());
System.out.println("IP地址:" + address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
3.URL类的使用:
private static void UrlTest() {
// 1.创建url的实例
try {
URL imooc = new URL("http://www.imooc.com");
// 通过url的openSource方法获取url对象所表示的资源的字节输入流
InputStream is = imooc.openStream();
// 将字节输入流转化为字符数据流
Reader reader = new InputStreamReader(is);
// 添加缓冲
BufferedReader bf = new BufferedReader(reader);
String data = bf.readLine();
while (data != null) {
System.out.println(data);
data = bf.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}