Skip to content

Commit 6398ede

Browse files
io相关测试
1 parent 91a7da3 commit 6398ede

13 files changed

Lines changed: 409 additions & 0 deletions

src/main/java/basic/collection/testMap/HashMapTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class HashMapTest {
1515

1616
public static void main(String[] args) {
1717
HashMap<Integer, String> hashMap = new HashMap<>();
18+
hashMap.put(null, "hello");
1819
for (int i = 0; i < 10; i++) {
1920
hashMap.put(i, Integer.toString(i));
2021
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package basic.nio;
2+
3+
import java.nio.charset.Charset;
4+
import java.util.Iterator;
5+
import java.util.SortedMap;
6+
7+
public class AvailableCharSets {
8+
public static void main(String[] args) {
9+
SortedMap<String, Charset> charSets = Charset.availableCharsets();
10+
Iterator<String> it = charSets.keySet().iterator();
11+
while (it.hasNext()) {
12+
String csName = it.next();
13+
System.out.print(csName);
14+
Iterator aliases = charSets.get(csName).aliases().iterator();
15+
if (aliases.hasNext())
16+
System.out.print(": ");
17+
while (aliases.hasNext()) {
18+
System.out.print(aliases.next());
19+
if (aliases.hasNext())
20+
System.out.print(", ");
21+
}
22+
System.out.println();
23+
}
24+
}
25+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package basic.nio;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.FileChannel;
8+
import java.nio.charset.Charset;
9+
10+
public class BufferToNext {
11+
private static final int BSIZE = 1024;
12+
13+
public static void main(String[] args) throws IOException {
14+
FileChannel fc = new FileOutputStream("data2.txt").getChannel();
15+
fc.write(ByteBuffer.wrap("Some text".getBytes()));
16+
fc = new FileInputStream("data2.txt").getChannel();
17+
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
18+
fc.read(buff);
19+
buff.flip();
20+
System.out.println(buff.asCharBuffer());
21+
buff.rewind();
22+
String encoding = System.getProperty("file.encoding");
23+
System.out.println("Decoded using " + encoding + ": "
24+
+ Charset.forName(encoding).decode(buff));
25+
fc = new FileOutputStream("data2.txt").getChannel();
26+
fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-8")));
27+
fc.close();
28+
fc = new FileInputStream("data2.txt").getChannel();
29+
buff.clear();
30+
fc.read(buff);
31+
buff.flip();
32+
System.out.println(buff.asCharBuffer());
33+
fc = new FileOutputStream("data2.txt").getChannel();
34+
buff = ByteBuffer.allocate(24);
35+
buff.asCharBuffer().put("Some text");
36+
fc.write(buff);
37+
fc.close();
38+
fc = new FileInputStream("data2.txt").getChannel();
39+
buff.clear();
40+
fc.read(buff);
41+
buff.flip();
42+
System.out.println(buff.asCharBuffer());
43+
}
44+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package basic.nio;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.ByteOrder;
5+
import java.util.Arrays;
6+
7+
public class Endians {
8+
public static void main(String[] args) {
9+
ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
10+
bb.asCharBuffer().put("abcdef"); //ÿ��charռ�����ֽ�
11+
System.out.println(Arrays.toString(bb.array()));
12+
bb.rewind();
13+
bb.order(ByteOrder.BIG_ENDIAN); //��λ���ȣ���Ҫ���ֽڷ��ڵ�λ
14+
bb.asCharBuffer().put("abcdef");
15+
System.out.println(Arrays.toString(bb.array()));
16+
bb.rewind();
17+
bb.order(ByteOrder.LITTLE_ENDIAN);
18+
bb.asCharBuffer().put("abcdef"); //��λ���ȣ���Ҫ���ֽڷ��ڸ�λ
19+
System.out.println(Arrays.toString(bb.array()));
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package basic.nio;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.RandomAccessFile;
7+
import java.nio.ByteBuffer;
8+
import java.nio.channels.FileChannel;
9+
/**
10+
* ��IO�򵥲���
11+
* FileOutputStream FileInputStream RandomAccessFile ���Ի�ȡͨ��
12+
* ����Reader��Writer���ܻ�ȡͨ��
13+
* ��Ϊͨ���ײ�Ͼ�Ҳ���ֽ���
14+
* @author yangqc
15+
* 2016/6/1
16+
*/
17+
public class GetChannel {
18+
private static final int BSIZE=1024;
19+
public static void main(String[] args) throws IOException {
20+
FileChannel fc=new FileOutputStream("data.txt").getChannel(); //��ȡͨ��
21+
fc.write(ByteBuffer.wrap("Some text \n".getBytes())); //�
22+
fc.close();
23+
fc=new RandomAccessFile("data.txt","rw").getChannel();
24+
fc.position(fc.size());
25+
fc.write(ByteBuffer.wrap("Some more".getBytes()));
26+
fc.close();
27+
fc=new FileInputStream("data.txt").getChannel();
28+
ByteBuffer buff=ByteBuffer.allocate(BSIZE);
29+
fc.read(buff);
30+
buff.flip(); //һ������read�ͱ������flip�û����������ñ��˶�ȡ���ݵ�׼��
31+
while(buff.hasRemaining()){
32+
System.out.print((char)buff.get());
33+
}
34+
}
35+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package basic.nio;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.CharBuffer;
5+
/**
6+
* ��ȡ��������
7+
* @author yangqc
8+
* 2016.6.2
9+
*/
10+
public class GetData {
11+
private static final int BSIZE = 1024;
12+
13+
public static void main(String[] args) {
14+
ByteBuffer bb = ByteBuffer.allocate(BSIZE); //�����ֽ���
15+
int i = 0;
16+
while (i++ < bb.limit()) //�ж���ʼÿλ�Ƿ�Ϊ0
17+
if (bb.get() != 0)
18+
System.out.println("nonzero");
19+
System.out.println("i=" + i);
20+
bb.rewind();
21+
bb.asCharBuffer().put("Howdy!");
22+
char c;
23+
bb.rewind();
24+
while ((c = bb.getChar()) != 0)
25+
System.out.print(c + " ");
26+
// System.out.println(bb.position());
27+
// bb.rewind();
28+
CharBuffer cb=((ByteBuffer)bb.rewind()).asCharBuffer();
29+
System.out.println(cb.remaining());
30+
for(int j=0;j<cb.remaining();j++){
31+
cb.put(j,'a');
32+
System.out.println(bb);
33+
}
34+
while (cb.hasRemaining())
35+
System.out.print((char)cb.get());
36+
// System.out.println(bb.position());
37+
System.out.println();
38+
bb.rewind();
39+
bb.asShortBuffer().put((short) -32769);
40+
System.out.println(bb.getShort());
41+
bb.rewind();
42+
bb.asIntBuffer().put(89798798);
43+
System.out.println(bb.getInt());
44+
bb.rewind();
45+
bb.asLongBuffer().put(9809L);
46+
System.out.println(bb.getLong());
47+
bb.rewind();
48+
bb.asFloatBuffer().put(9879798f);
49+
bb.rewind();
50+
bb.asDoubleBuffer().put(2112);
51+
System.out.println(bb.getDouble());
52+
bb.rewind();
53+
}
54+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package basic.nio;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.IntBuffer;
5+
6+
public class IntBufferDemo {
7+
private static final int BSIZE=1024;
8+
public static void main(String[] args) {
9+
ByteBuffer bb=ByteBuffer.allocate(BSIZE);
10+
IntBuffer ib=bb.asIntBuffer();
11+
System.out.println(ib.capacity()); //����256����Ϊһ��int����ռ4���ֽڣ�1024���ֽ�����Ϊ256��int����
12+
ib.put(new int[]{11,43,47,99,143,811,1016,1212});
13+
System.out.println(ib.capacity()+"(");
14+
System.out.println(ib.get(3));
15+
ib.put(3,181);
16+
ib.put(new int[]{21,21,4,4});
17+
ib.flip();
18+
while(ib.hasRemaining()){
19+
int i=ib.get();
20+
System.out.println(i);
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package basic.nio;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.IOException;
5+
import java.io.RandomAccessFile;
6+
import java.nio.MappedByteBuffer;
7+
import java.nio.channels.FileChannel;
8+
/**
9+
* MappedByteBuffer�Ǵ�ByteBuffer�̳ж���
10+
* @author yangqc
11+
* 2016.6.2
12+
* ���ļ������������ڵײ�����ܴ���ļ�
13+
*/
14+
public class LargeMappedFiles {
15+
static int length=0x8FFFFFF; //128MB
16+
public static void main(String[] args) throws FileNotFoundException, IOException {
17+
MappedByteBuffer out=new RandomAccessFile("text.dat","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);//����ζ�ſ���ӳ����ļ���С�IJ���
18+
for(int i=0;i<length;i++)
19+
out.put((byte)'x');
20+
for(int i=length/2;i<length/2+6;i++)
21+
System.out.println((char)out.get(i));
22+
}
23+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package basic.nio;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.RandomAccessFile;
9+
import java.nio.channels.FileChannel;
10+
import java.nio.IntBuffer;
11+
12+
public class MappedIO {
13+
private static int numOfInts = 4000000;
14+
private static int numOfUbuffInts = 200000;
15+
16+
private abstract static class Testter {
17+
private String name;
18+
19+
public Testter(String name) {
20+
this.name = name;
21+
}
22+
23+
public void runTest() {
24+
try {
25+
long start = System.nanoTime();
26+
test();
27+
double duration = System.nanoTime() - start;
28+
System.out.format("%.2f\n", duration / 1.0e9);
29+
} catch (IOException e) {
30+
throw new RuntimeException(e);
31+
}
32+
}
33+
34+
public abstract void test() throws IOException;
35+
}
36+
private static Testter[] tests={
37+
new Testter("Stream Write"){
38+
@Override
39+
public void test() throws IOException {
40+
DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("temp.tmp"))));
41+
for(int i=0;i<numOfInts;i++){
42+
dos.writeInt(1);
43+
}
44+
dos.close();
45+
}
46+
},
47+
new Testter("Mapped Writer"){
48+
@Override
49+
public void test() throws IOException {
50+
FileChannel fc=new RandomAccessFile("temp.tmp","rw").getChannel();
51+
IntBuffer ib=fc.map(FileChannel.MapMode.READ_WRITE,0,fc.size()).asIntBuffer();
52+
for(int i=0;i<numOfInts;i++)
53+
ib.put(i);
54+
fc.close();
55+
}
56+
}
57+
};
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package basic.nio;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.nio.channels.FileChannel;
7+
8+
/**
9+
* ͨ��֮�����Ĵ���
10+
*
11+
* @author yangqc 2016.6.2
12+
*/
13+
public class TransferTo {
14+
15+
public static void main(String[] args) throws IOException {
16+
if (args.length != 2) {
17+
System.out.println("arguments:sourcefile destfile");
18+
System.exit(1);
19+
}
20+
FileChannel in = new FileInputStream(args[0]).getChannel(), out = new FileOutputStream("args[1]").getChannel();
21+
in.transferTo(0, in.size(), out);
22+
}
23+
}

0 commit comments

Comments
 (0)