java应用技术
3(6)
一、网络编程
1.输出主机地址,输出主机名称
案例:
package cn.java.java7;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NetDemo1 {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getByName("127.0.0.1");
System.out.println(ip.getHostAddress());//输出主机地址
System.out.println(ip.getHostName());//输出主机名称
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
2.UDP发送端,接收端
案例:
package cn.java.java7;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class UDPSendDemo {
public static void main(String[] args) {
Send sd = new Send();
sd.send();
}
}
class Send{
public void send(){
System.out.println("发送端");
Scanner sc = new Scanner(System.in);
String say = sc.next();
byte[] b = say.getBytes();
try {
DatagramSocket ds = new DatagramSocket();
DatagramPacket dp = new DatagramPacket(b,b.length,InetAddress.getByName("127.0.0.1"),8888);
ds.send(dp);
ds.close();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive(){
System.out.println("接收端");
try {
DatagramSocket dc = new DatagramSocket(8888);
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b,b.length);
dc.receive(dp);
System.out.println(new String(b,0,b.length));
dc.close();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.TCP服务器端
案例:
package cn.java.java7;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemo {
public static void main(String[] args) {
System.out.println("服务器端");
try {
ServerSocket ss = new ServerSocket(9090);//Server服务器插座
Socket sk = ss.accept();
InputStream in = sk.getInputStream();
byte[] b = new byte[1024];
in.read(b);
System.out.println(new String(b,0,b.length));
OutputStream ops = sk.getOutputStream();
ops.write("我是服务器".getBytes());
in.close();
sk.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4.TCP客户端
案例:
package cn.java.java7;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPDemo {
public static void main(String[] args) {
System.out.println("客户端:");
try {
Socket sk = new Socket("127.0.0.1",9090);
//发送
OutputStream os = sk.getOutputStream();
os.write("我是好人".getBytes());
//接收
InputStream in = sk.getInputStream();
byte[] b = new byte[1024];
in.read(b);
System.out.println(new String(b,0,b.length));
in.close();
os.close();
sk.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.TCP服务器端多线程传文件
案例:
package cn.java.java7;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ThreadServerDemo {
public static void main(String[] args){
try {
System.out.println("服务器端");
ServerSocket ss = new ServerSocket(7080);//Server服务器插座
while(true){
Socket sk = ss.accept();
new Thread(new Server(sk)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Server implements Runnable{
private Socket sk;
private static int i = 0;
public Server(Socket sk) {
this.sk = sk;
}
@Override
public void run() {
InputStream in = null;
try {
in = sk.getInputStream();
String path = "F:"+File.separator+"bb"+File.separator+"帅哥"+(++i)+".zip";
OutputStream ops = new FileOutputStream(path);
byte[] b1 = new byte[(1024*1024)*100];
int len = 0;
while((len=in.read(b1))!=-1){
ops.write(b1, 0,len);
}
in.close();
ops.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6.TCP客户端多线程传文件
案例:
package cn.java.java7;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class SendFileDemo {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("客户端:");
Socket s = new Socket("127.0.0.1", 7080);
OutputStream out = s.getOutputStream();
String path = "F:"+File.separator+"aa"+File.separator+"ImagePacks2.zip";
InputStream in = new FileInputStream(path);
byte[] b = new byte[(1024*1024)*100];
int len = 0;
while((len=in.read(b))!=-1){
out.write(b, 0,len);
}
System.out.println("发送完毕");
in.close();
out.close();
s.close();
}
}
二、枚举类
1.一些方法在运行时,它需要的数据不能是任意的,而必须是一定范围内的值,比如成绩Grade只能为ABCDE。
此类问题在JDK5以前采用自定义带有枚举功能的类解决,Java5以后可以直接使用枚举予以解决。
2.枚举类也是一种特殊形式的Java类。
3.枚举类中声明的每一个枚举值代表枚举类的一个实例对象。
4.若枚举类只有一个枚举值,则可以当作单态设计模式使用。
5.枚举类也可以实现接口、或继承抽象类。
6.与java中的普通类一样,在声明枚举类时,也可以声明属性、方法和构造函数,但枚举类的构造函数必须为私有的(这点不难理解)。
案例:
package cn.java6;
public class EnumDemo {
public static void main(String[] args) {
Grade A= Grade.A;
System.out.println(Grade.A);
System.out.println(Grade.A.getInfo());
Grade.A.out();
System.out.println(A.name());
System.out.println(A.ordinal());
Grade d = Grade.valueOf("D");
d.out();
Grade[] g = Grade.values();
for (int i = 0; i < g.length; i++) {
System.out.println(g[i]);
}
}
}
//枚举是一种特殊的类;
//枚举中的元素就是枚举这个特殊类的实例。
//枚举可以有构造方法,但必须是私有的。
enum Grade{
A("90-100"){
public void out(){
System.out.println("优秀");
}
},B("80-89"){
public void out(){
System.out.println("良好");
}
},C("70-79"){
public void out(){
System.out.println("一般");
}
},D("60-69"){
public void out(){
System.out.println("及格");
}
},E("0-59"){
public void out(){
System.out.println("不及格");
}
};
private String info;
private Grade(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public abstract void out();
}
3(6)
一、网络编程
1.输出主机地址,输出主机名称
案例:
package cn.java.java7;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NetDemo1 {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getByName("127.0.0.1");
System.out.println(ip.getHostAddress());//输出主机地址
System.out.println(ip.getHostName());//输出主机名称
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
2.UDP发送端,接收端
案例:
package cn.java.java7;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class UDPSendDemo {
public static void main(String[] args) {
Send sd = new Send();
sd.send();
}
}
class Send{
public void send(){
System.out.println("发送端");
Scanner sc = new Scanner(System.in);
String say = sc.next();
byte[] b = say.getBytes();
try {
DatagramSocket ds = new DatagramSocket();
DatagramPacket dp = new DatagramPacket(b,b.length,InetAddress.getByName("127.0.0.1"),8888);
ds.send(dp);
ds.close();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive(){
System.out.println("接收端");
try {
DatagramSocket dc = new DatagramSocket(8888);
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b,b.length);
dc.receive(dp);
System.out.println(new String(b,0,b.length));
dc.close();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.TCP服务器端
案例:
package cn.java.java7;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemo {
public static void main(String[] args) {
System.out.println("服务器端");
try {
ServerSocket ss = new ServerSocket(9090);//Server服务器插座
Socket sk = ss.accept();
InputStream in = sk.getInputStream();
byte[] b = new byte[1024];
in.read(b);
System.out.println(new String(b,0,b.length));
OutputStream ops = sk.getOutputStream();
ops.write("我是服务器".getBytes());
in.close();
sk.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4.TCP客户端
案例:
package cn.java.java7;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPDemo {
public static void main(String[] args) {
System.out.println("客户端:");
try {
Socket sk = new Socket("127.0.0.1",9090);
//发送
OutputStream os = sk.getOutputStream();
os.write("我是好人".getBytes());
//接收
InputStream in = sk.getInputStream();
byte[] b = new byte[1024];
in.read(b);
System.out.println(new String(b,0,b.length));
in.close();
os.close();
sk.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.TCP服务器端多线程传文件
案例:
package cn.java.java7;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ThreadServerDemo {
public static void main(String[] args){
try {
System.out.println("服务器端");
ServerSocket ss = new ServerSocket(7080);//Server服务器插座
while(true){
Socket sk = ss.accept();
new Thread(new Server(sk)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Server implements Runnable{
private Socket sk;
private static int i = 0;
public Server(Socket sk) {
this.sk = sk;
}
@Override
public void run() {
InputStream in = null;
try {
in = sk.getInputStream();
String path = "F:"+File.separator+"bb"+File.separator+"帅哥"+(++i)+".zip";
OutputStream ops = new FileOutputStream(path);
byte[] b1 = new byte[(1024*1024)*100];
int len = 0;
while((len=in.read(b1))!=-1){
ops.write(b1, 0,len);
}
in.close();
ops.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6.TCP客户端多线程传文件
案例:
package cn.java.java7;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class SendFileDemo {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("客户端:");
Socket s = new Socket("127.0.0.1", 7080);
OutputStream out = s.getOutputStream();
String path = "F:"+File.separator+"aa"+File.separator+"ImagePacks2.zip";
InputStream in = new FileInputStream(path);
byte[] b = new byte[(1024*1024)*100];
int len = 0;
while((len=in.read(b))!=-1){
out.write(b, 0,len);
}
System.out.println("发送完毕");
in.close();
out.close();
s.close();
}
}
二、枚举类
1.一些方法在运行时,它需要的数据不能是任意的,而必须是一定范围内的值,比如成绩Grade只能为ABCDE。
此类问题在JDK5以前采用自定义带有枚举功能的类解决,Java5以后可以直接使用枚举予以解决。
2.枚举类也是一种特殊形式的Java类。
3.枚举类中声明的每一个枚举值代表枚举类的一个实例对象。
4.若枚举类只有一个枚举值,则可以当作单态设计模式使用。
5.枚举类也可以实现接口、或继承抽象类。
6.与java中的普通类一样,在声明枚举类时,也可以声明属性、方法和构造函数,但枚举类的构造函数必须为私有的(这点不难理解)。
案例:
package cn.java6;
public class EnumDemo {
public static void main(String[] args) {
Grade A= Grade.A;
System.out.println(Grade.A);
System.out.println(Grade.A.getInfo());
Grade.A.out();
System.out.println(A.name());
System.out.println(A.ordinal());
Grade d = Grade.valueOf("D");
d.out();
Grade[] g = Grade.values();
for (int i = 0; i < g.length; i++) {
System.out.println(g[i]);
}
}
}
//枚举是一种特殊的类;
//枚举中的元素就是枚举这个特殊类的实例。
//枚举可以有构造方法,但必须是私有的。
enum Grade{
A("90-100"){
public void out(){
System.out.println("优秀");
}
},B("80-89"){
public void out(){
System.out.println("良好");
}
},C("70-79"){
public void out(){
System.out.println("一般");
}
},D("60-69"){
public void out(){
System.out.println("及格");
}
},E("0-59"){
public void out(){
System.out.println("不及格");
}
};
private String info;
private Grade(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public abstract void out();
}