Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 221c706

Browse files
committed
初学nio
1 parent 27c07e0 commit 221c706

File tree

15 files changed

+572
-394
lines changed

15 files changed

+572
-394
lines changed
314 Bytes
Binary file not shown.

employee.txt

Whitespace-only changes.

employee1.txt

Whitespace-only changes.

src/BasicAndDontKnow/Test111.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package BasicAndDontKnow;
2+
3+
public class Test111 {
4+
public static void main(String[] args) {
5+
Integer a = 128, b = 128;
6+
System.out.println(a == b);
7+
}
8+
}

src/BasicAndDontKnow/Test1111.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package BasicAndDontKnow;
2+
3+
public class Test1111 {
4+
public static int a = 5;
5+
static {
6+
a = a * 3;
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println(a);
11+
}
12+
13+
static {
14+
a = a / 3;
15+
}
16+
}

src/Http/HttpClientTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Http;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.apache.http.HttpEntity;
7+
import org.apache.http.NameValuePair;
8+
import org.apache.http.client.entity.UrlEncodedFormEntity;
9+
import org.apache.http.client.methods.CloseableHttpResponse;
10+
import org.apache.http.client.methods.HttpGet;
11+
import org.apache.http.client.methods.HttpPost;
12+
import org.apache.http.impl.client.CloseableHttpClient;
13+
import org.apache.http.impl.client.HttpClients;
14+
import org.apache.http.message.BasicNameValuePair;
15+
import org.apache.http.util.EntityUtils;
16+
17+
public class HttpClientTest {
18+
19+
public static void main(String[] args) throws Exception {
20+
CloseableHttpClient httpclient = HttpClients.createDefault();
21+
try {
22+
HttpGet httpGet = new HttpGet("http://httpbin.org/get");
23+
CloseableHttpResponse response1 = httpclient.execute(httpGet);
24+
// The underlying HTTP connection is still held by the response object
25+
// to allow the response content to be streamed directly from the network socket.
26+
// In order to ensure correct deallocation of system resources
27+
// the user MUST call CloseableHttpResponse#close() from a finally clause.
28+
// Please note that if response content is not fully consumed the underlying
29+
// connection cannot be safely re-used and will be shut down and discarded
30+
// by the connection manager.
31+
try {
32+
System.out.println(response1.getStatusLine());
33+
HttpEntity entity1 = response1.getEntity();
34+
// do something useful with the response body
35+
// and ensure it is fully consumed
36+
EntityUtils.consume(entity1);
37+
} finally {
38+
response1.close();
39+
}
40+
41+
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
42+
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
43+
nvps.add(new BasicNameValuePair("username", "vip"));
44+
nvps.add(new BasicNameValuePair("password", "secret"));
45+
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
46+
CloseableHttpResponse response2 = httpclient.execute(httpPost);
47+
48+
try {
49+
System.out.println(response2.getStatusLine());
50+
HttpEntity entity2 = response2.getEntity();
51+
// do something useful with the response body
52+
// and ensure it is fully consumed
53+
EntityUtils.consume(entity2);
54+
} finally {
55+
response2.close();
56+
}
57+
} finally {
58+
httpclient.close();
59+
}
60+
}
61+
62+
}

src/NIO/SimpleUseOfBuffer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package NIO;
2+
3+
public class SimpleUseOfBuffer {
4+
5+
}

src/NIO/SimpleUserOfChannel.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package NIO;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.IOException;
5+
import java.io.RandomAccessFile;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.FileChannel;
8+
9+
10+
public class SimpleUserOfChannel {
11+
public static void main(String[] args){
12+
try {
13+
RandomAccessFile randomFile;
14+
randomFile = new RandomAccessFile("src\\employee.txt", "rw");
15+
FileChannel inChannel= randomFile.getChannel();
16+
17+
ByteBuffer byteBuffer=ByteBuffer.allocate(10);
18+
19+
//从通道中读一组字节序列到缓存中
20+
int bytesRead=inChannel.read(byteBuffer);
21+
22+
while(bytesRead!=-1) {
23+
System.out.println("Read"+bytesRead);
24+
25+
//从buffer中读取数据
26+
byteBuffer.flip();
27+
28+
while(byteBuffer.hasRemaining()) {
29+
System.out.println("buf:"+(char)byteBuffer.get());
30+
}
31+
32+
byteBuffer.clear();
33+
bytesRead=inChannel.read(byteBuffer);
34+
}
35+
36+
randomFile.close();
37+
} catch (FileNotFoundException e) {
38+
// TODO Auto-generated catch block
39+
e.printStackTrace();
40+
} catch (IOException e) {
41+
// TODO Auto-generated catch block
42+
e.printStackTrace();
43+
}
44+
45+
46+
}
47+
}

src/NIO/Test.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package NIO;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.nio.ByteBuffer;
6+
import java.nio.channels.SelectionKey;
7+
import java.nio.channels.Selector;
8+
import java.nio.channels.ServerSocketChannel;
9+
import java.nio.channels.SocketChannel;
10+
import java.util.Iterator;
11+
import java.util.Set;
12+
13+
public class Test {
14+
15+
public void selector() throws IOException {
16+
ByteBuffer buffer=ByteBuffer.allocate(1024);
17+
Selector selector=Selector.open();
18+
ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();
19+
serverSocketChannel.configureBlocking(false);//设为非阻塞方式
20+
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
21+
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
22+
23+
while(true) {
24+
Set selectedKeys=selector.selectedKeys();
25+
Iterator iterator=selectedKeys.iterator();
26+
27+
while(iterator.hasNext()) {
28+
SelectionKey key=(SelectionKey) iterator.next();
29+
30+
if((key.readyOps()&SelectionKey.OP_ACCEPT)==SelectionKey.OP_ACCEPT) {
31+
ServerSocketChannel ssChannel=(ServerSocketChannel) key.channel();
32+
33+
SocketChannel sc=ssChannel.accept();
34+
sc.configureBlocking(false);
35+
sc.register(selector, SelectionKey.OP_READ);
36+
iterator.remove();
37+
}else if((key.readyOps() & SelectionKey.OP_ACCEPT)==SelectionKey.OP_READ) {
38+
SocketChannel sc=(SocketChannel) key.channel();
39+
while(true) {
40+
buffer.clear();
41+
int n=sc.read(buffer);
42+
if(n<=0) {
43+
break;
44+
}
45+
buffer.flip();
46+
}
47+
iterator.remove();
48+
}
49+
50+
}
51+
}
52+
53+
}
54+
55+
public static void main(String[] args) {
56+
57+
}
58+
}
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
package TwoChapter1StreamFile;
2-
3-
import java.io.Closeable;
4-
import java.io.FileInputStream;
5-
import java.io.FileNotFoundException;
6-
import java.io.IOException;
7-
import java.io.PrintWriter;
8-
import java.util.Scanner;
9-
10-
public class InputOutputTXT implements Closeable{
11-
12-
/*
13-
* 1.输出文本到文件
14-
* 2.读文本
15-
*
16-
* */
17-
public static void main(String[] args) {
18-
//try-with-catch--语法糖
19-
try( Scanner in=new Scanner(new FileInputStream("src\\readme.txt"));
20-
PrintWriter out= new PrintWriter("src\\readme.txt")){
21-
//PrintWriter out=new PrintWriter(new FileWriter("readme.txt"),true);
22-
//是上面写法的简写,true代表自动刷新
23-
//打印到写出器
24-
out.println("name");
25-
out.print("password");
26-
out.println(100);
27-
out.flush();
28-
29-
} catch (FileNotFoundException e) {
30-
// TODO Auto-generated catch block
31-
e.printStackTrace();
32-
}
33-
}
34-
35-
@Override
36-
public void close() throws IOException {
37-
// TODO Auto-generated method stub
38-
System.out.println("关闭输入/输出流");
39-
}
40-
41-
}
1+
package StreamFile;
2+
3+
import java.io.Closeable;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.util.Scanner;
9+
10+
public class InputOutputTXT implements Closeable{
11+
12+
/*
13+
* 1.����ı����ļ�
14+
* 2.���ı�
15+
*
16+
* */
17+
public static void main(String[] args) {
18+
//try-with-catch--�﷨��
19+
try( Scanner in=new Scanner(new FileInputStream("src\\readme.txt"));
20+
PrintWriter out= new PrintWriter("src\\readme.txt")){
21+
//PrintWriter out=new PrintWriter(new FileWriter("readme.txt"),true);
22+
//������д���ļ�д��true�����Զ�ˢ��
23+
//��ӡ��д����
24+
out.println("name");
25+
out.print("password");
26+
out.println(100);
27+
out.flush();
28+
29+
} catch (FileNotFoundException e) {
30+
// TODO Auto-generated catch block
31+
e.printStackTrace();
32+
}
33+
}
34+
35+
@Override
36+
public void close() throws IOException {
37+
// TODO Auto-generated method stub
38+
System.out.println("�ر�����/�����");
39+
}
40+
41+
}

0 commit comments

Comments
 (0)