En lisant ça, http://www.cs.brown.edu/courses/cs16.../j-nio-ltr.pdf , j'avais cru comprendre que les entrées sorties nio était plus performentes que les entrées sorties io.

J'ai fais des tests (enfin je pense) et je trouve systématiquement io deux fois plus rapide que nio.

Est-ce que j'utilise mal nio, n'est-ce pas si rapide ?

Voici les deux codes :

IO

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
public static void print(String S, String filename, boolean append) {
		try {
			PrintWriter writer = new PrintWriter(new FileOutputStream(filename,
					append));
			writer.println(S);
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
 
	}

NIO
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public static void printNIO(String S, String filename, boolean append) {
 
 
		int transfBytes = 0;
		boolean nend = true;
 
		byte[] T = S.getBytes();
		int bufSize = Math.max(T.length, 32768);
 
		try {
 
 
			FileOutputStream fout = new FileOutputStream(filename, append);
			FileChannel fc = fout.getChannel();
 
			ByteBuffer buffer = ByteBuffer.allocate(bufSize);
 
			do{
 
				if(transfBytes+buffer.capacity() < T.length){
 
					buffer.put(T,transfBytes,buffer.capacity());
					transfBytes=transfBytes+buffer.capacity();
				}
				else{
 
					buffer.put(T,transfBytes,T.length-transfBytes);
					nend = false;
				}
 
				buffer.flip();
				fc.write(buffer);
				buffer.clear();
			}while(nend);
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
 
			e.printStackTrace();
			System.exit(-1);
		}
	}