java.nio : utilisation des Channel
Bonjour,
Dans le but d'optimiser l'écriture dans les fichiers, je peux utiliser le package java.nio.
J'ai donc développé la petite fonction suivante, mais qui ne fonctionne pas ; je ne comprends pas pourquoi.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static void write() throws IOException
{
File file = new File("C:/temp/test.txt");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
String st = "tata\n yoyo";
FileChannel channel = fos.getChannel();
ByteBuffer bb = ByteBuffer.allocate(st.length() * 2);
// CAUTION: allocated size is nb_chars x size of a char (in bytes)
for (int i = 0; i < st.length(); i++)
{
bb.putChar(st.charAt(i));
}
bb.flip();
channel.write(bb);
fos.close();
} |
Une idée sur la raison pour laquelle mon fichier reste vide ?