提交 3abb6823 编写于 作者: 武汉红喜's avatar 武汉红喜

java

上级 87697d97
package org.hongxi.java.io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
/**
* Created by shenhongxi on 2018/10/25.
*/
public class FileTest {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("/Users/javahongxi/Documents/white_list.txt"));
PrintWriter pw = new PrintWriter("/Users/javahongxi/Documents/white_list02.txt");
String line = null;
while ((line = br.readLine()) != null) {
pw.println(line);
}
pw.flush();
pw.close();
br.close();
}
}
package org.hongxi.java.util.lang;
package org.hongxi.java.lang;
/**
* Created by shenhongxi on 2018/6/1.
......
package org.hongxi.java.util.lang.oop;
package org.hongxi.java.lang.oop;
/**
* @author shenhongxi 2019/8/11
......
package org.hongxi.java.util.lang.oop;
package org.hongxi.java.lang.oop;
/**
* @author shenhongxi 2019/8/11
......
package org.hongxi.java.net;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClient {
public static void main(String[] args) {
new TCPClient().connect();
}
public void connect() {
@SuppressWarnings("unused")
boolean started = false;
Socket s = null;
DataOutputStream dos = null;
try {
s = new Socket("127.0.0.1", 5555);
dos = new DataOutputStream(s.getOutputStream());
started = true;
System.out.println("Yeah, I connected");
Thread.sleep(3000);
dos.writeUTF("Happy");
dos.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (dos != null) {
dos.close();
}
if (s != null) {
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package org.hongxi.java.net;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
boolean started;
ServerSocket ss;
public static void main(String[] args) {
new TCPServer().start();
}
private void start() {
try {
ss = new ServerSocket(5555);
started = true;
System.out.println("Server started");
} catch (BindException e) {
e.printStackTrace();
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ss != null) ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class Client extends Thread {
private Socket s;
private DataInputStream dis;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
System.out.println(dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
if (s != null) {
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package org.hongxi.java.net;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UDPClient {
public static void main(String[] args) {
try {
long n = 1000L;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(n);
//byte[] buf = new String("Hello").getBytes();
byte[] buf = baos.toByteArray();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
new InetSocketAddress("localhost", 5678));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package org.hongxi.java.net;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer {
public static void main(String[] args) {
try {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket ds = new DatagramSocket(5678);
while (true) {
ds.receive(dp);
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readLong());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package org.hongxi.java.nio;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MappedFileTest {
static private final int start = 0;
static private final int size = 1024;
static public void main(String args[]) throws Exception {
RandomAccessFile raf = new RandomAccessFile("mappedfile.txt", "rw");
FileChannel fc = raf.getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE,
start, size);
mbb.put(0, (byte) 97);
mbb.put(1023, (byte) 122);
raf.close();
}
}
package org.hongxi.java.nio;// $Id$
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class MultiPortEcho {
private int ports[];
private ByteBuffer echoBuffer = ByteBuffer.allocate(1024);
public MultiPortEcho(int ports[]) throws IOException {
this.ports = ports;
go();
}
private void go() throws IOException {
// Create a new selector
Selector selector = Selector.open();
// Open a listener on each port, and register each one
// with the selector
for (int i = 0; i < ports.length; ++i) {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ServerSocket ss = ssc.socket();
InetSocketAddress address = new InetSocketAddress(ports[i]);
ss.bind(address);
SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("Going to listen on " + ports[i]);
}
while (true) {
int num = selector.select();
Set selectedKeys = selector.selectedKeys();
Iterator it = selectedKeys.iterator();
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
if ((key.readyOps() & SelectionKey.OP_ACCEPT)
== SelectionKey.OP_ACCEPT) {
// Accept the new connection
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
// Add the new connection to the selector
SelectionKey newKey = sc.register(selector, SelectionKey.OP_READ);
it.remove();
System.out.println("Got connection from " + sc);
} else if ((key.readyOps() & SelectionKey.OP_READ)
== SelectionKey.OP_READ) {
// Read the data
SocketChannel sc = (SocketChannel) key.channel();
// Echo data
int bytesEchoed = 0;
while (true) {
echoBuffer.clear();
int r = sc.read(echoBuffer);
if (r <= 0) {
break;
}
echoBuffer.flip();
sc.write(echoBuffer);
bytesEchoed += r;
}
System.out.println("Echoed " + bytesEchoed + " from " + sc);
it.remove();
}
}
}
}
static public void main(String args[]) throws Exception {
if (args.length <= 0) {
System.err.println("Usage: java MultiPortEcho port [port port ...]");
System.exit(1);
}
int ports[] = new int[args.length];
for (int i = 0; i < args.length; ++i) {
ports[i] = Integer.parseInt(args[i]);
}
new MultiPortEcho(ports);
}
}
package org.hongxi.java.rmi;
import java.rmi.Naming;
/**
* Created by shenhongxi on 2016/4/18.
*/
public class Client {
public static void main(String[] args) throws Exception {
UserService userService = (UserService) Naming.lookup("rmi://127.0.0.1:8899/userService");
User user = new User();
user.setName("Mars");
user.setAge(27);
String hi = userService.hi(user);
System.out.println(hi);
}
}
package org.hongxi.java.rmi;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
/**
* Created by shenhongxi on 2016/4/18.
* java RMI 的局限性是只能在java环境使用,另外依赖ip/port
*/
public class Server {
public static void main(String[] args) throws Exception {
UserService userService = new UserServiceImpl();
LocateRegistry.createRegistry(8899);
Naming.rebind("rmi://127.0.0.1:8899/userService", userService);
}
}
package org.hongxi.java.rmi;
import java.io.Serializable;
/**
* Created by shenhongxi on 2016/4/18.
*/
public class User implements Serializable {
private static final long serialVersionUID = 7105466693678286106L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package org.hongxi.java.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* Created by shenhongxi on 2016/4/18.
*/
public interface UserService extends Remote {
String hi(User user) throws RemoteException;
}
package org.hongxi.java.rmi;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* Created by shenhongxi on 2016/4/18.
*/
public class UserServiceImpl extends UnicastRemoteObject implements UserService {
public UserServiceImpl() throws RemoteException {
super();
}
@Override
public String hi(User user) throws RemoteException {
return "Hi, " + user.getName() + ", your age is " + user.getAge() + "?";
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册