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

Skip to content

Commit 1e9dc2a

Browse files
committed
nio学习
1 parent 221c706 commit 1e9dc2a

25 files changed

+121
-14
lines changed

.classpath

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6-
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0"/>
7-
<classpathentry kind="output" path="bin"/>
8-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0"/>
7+
<classpathentry kind="lib" path="E:/maven/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar"/>
8+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/httpclient-4.5.4.jar"/>
9+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/commons-codec-1.10.jar"/>
10+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/commons-logging-1.2.jar"/>
11+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/fluent-hc-4.5.4.jar"/>
12+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/httpclient-cache-4.5.4.jar"/>
13+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/httpclient-win-4.5.4.jar"/>
14+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/httpcore-4.4.7.jar"/>
15+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/httpmime-4.5.4.jar"/>
16+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/jna-4.4.0.jar"/>
17+
<classpathentry kind="lib" path="E:/files/jar/httpcomponents-client-4.5.4-bin/httpcomponents-client-4.5.4/lib/jna-platform-4.4.0.jar"/>
18+
<classpathentry kind="output" path="bin"/>
19+
</classpath>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
eclipse.preferences.version=1
2-
encoding//src/TwoChapter1StreamFile=UTF-8
3-
encoding/<project>=UTF-8
1+
eclipse.preferences.version=1
2+
encoding//src/StreamFile=UTF-8
3+
encoding/<project>=UTF-8

bin/BasicAndDontKnow/Test111.class

734 Bytes
Binary file not shown.

bin/BasicAndDontKnow/Test1111.class

644 Bytes
Binary file not shown.

bin/Http/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/HttpClientTest.class

bin/NIO/SimpleUseOfBuffer.class

1.78 KB
Binary file not shown.

bin/NIO/SimpleUserOfChannel.class

1.79 KB
Binary file not shown.

bin/NIO/Test.class

2.3 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

bin/StreamFile/Test2.class

647 Bytes
Binary file not shown.
File renamed without changes.

bin/TwoChapter1StreamFile/Test2.class

-691 Bytes
Binary file not shown.

bin/jdbc/JDBC.class

971 Bytes
Binary file not shown.

java_pid14280.hprof

-26.7 MB
Binary file not shown.

java_pid4076.hprof

-26.7 MB
Binary file not shown.

readme.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/NIO/SimpleUseOfBuffer.java

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,85 @@
11
package NIO;
22

3-
public class SimpleUseOfBuffer {
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;
48

9+
/*
10+
* 使用Buffer的四个步骤:
11+
* (声明buffer,绑定到通道)
12+
* 1.写数据到Buffer
13+
* ---a. int bytesRead=inChannel.read(buf)//read into buf
14+
* ---b. buf.read(123);
15+
* 2.调用buffer.flip()//从写模式切换到度模式
16+
* --- 即将limit=position position=0
17+
* 3.从buffer中读数据
18+
* ---a. int bytesWrite=inChannel.write(buf)//从buf中读数据到channel
19+
* ---b. buf.get()
20+
* 4.调用buffer.clear()或者buffer.compact()
21+
* ---buf.clear():positon=0,limit=capacity “忘记”哪些数据读过,哪些没有
22+
* ---buf.compact():将未读过的数据都放在开头,position=最后一个数据后面
23+
*
24+
* buf.rewind():position=0,limit不变,你可以重读buf中的所有数据
25+
*
26+
* buf.mark():mark=positon:标记position
27+
* buf.reset():position=mark:重新回到之前的position
28+
*
29+
*
30+
*
31+
* 其他几个基本函数:
32+
* buf.equals():比较剩余的值是否相等
33+
* ------ByteBuffer.equals()
34+
* p=postion;
35+
* for(int i=this.limit-1,int j=that.limit-1;i>=p;i--,j--)
36+
* buf.compare():
37+
* ------ByteBuffer.compareTo():
38+
* n=positon+Math.min(this.remain,that.remain)
39+
* //在剩余的值中
40+
* for(i=this.position,j=that.position;i<n;i++)
41+
* //如果谁的值大,谁就大
42+
* int temp=if(compare(this.get(i),that.get(i)))
43+
* if(temp!=0)
44+
* return temp;
45+
* //如果剩余值都相同,谁数据多谁就大
46+
* return this.remain-that.remain;
47+
* */
48+
public class SimpleUseOfBuffer {
49+
public static void main(String[] args){
50+
try {
51+
RandomAccessFile randomFile;
52+
randomFile = new RandomAccessFile("src\\employee.txt", "rw");
53+
FileChannel inChannel= randomFile.getChannel();
54+
55+
ByteBuffer byteBuffer=ByteBuffer.allocate(10);
56+
57+
//从通道中读一组字节序列到缓存中
58+
int bytesRead=inChannel.read(byteBuffer);
59+
60+
while(bytesRead!=-1) {
61+
System.out.println("Read"+bytesRead);
62+
63+
//从buffer中读取数据
64+
byteBuffer.flip();
65+
66+
while(byteBuffer.hasRemaining()) {
67+
System.out.println("buf:"+(char)byteBuffer.get());//从buf中取数据
68+
}
69+
70+
byteBuffer.clear();
71+
bytesRead=inChannel.read(byteBuffer);
72+
}
73+
74+
randomFile.close();
75+
} catch (FileNotFoundException e) {
76+
// TODO Auto-generated catch block
77+
e.printStackTrace();
78+
} catch (IOException e) {
79+
// TODO Auto-generated catch block
80+
e.printStackTrace();
81+
}
82+
83+
84+
}
585
}
File renamed without changes.

src/StreamFile/Test2.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package StreamFile;
2+
3+
public class Test2 {
4+
public static void main(String[] args){
5+
System.out.println("Test2");
6+
Test test= new Test();
7+
String[] a=null;
8+
test.main(a);
9+
}
10+
}
11+
12+
class Test1{
13+
public static void main(String[] args) {
14+
System.out.println("Test1");
15+
System.out.println(args.length);
16+
}
17+
}
File renamed without changes.

0 commit comments

Comments
 (0)