FileChannel Buffer LECTURE
Bonjour, dans la cadre d'exercices sur les Buffer, je teste le programmme suivant trouvé dans la JAVADOCEXAMPLES
Le problème est que "il propose l'écriture du fichier mais pas la lecture"
C'est malheureusement la lecture que je ne parviens pas à maîtriser
Voici la tentative de code (AJOUT en vert dans la bas ) pour la lecture
Pouvez-vous me donner quelques suggestions, Merci
Code:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
package tampons_jeter;
import static java.lang.Math.ceil;
import static java.lang.Math.sqrt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.nio.DoubleBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.stream.FileImageInputStream;
public class PrimesToFile2 {
public static void main(String[] args) throws IOException {
int primesRequired = 100; // Default count
if (args.length > 0) {
try {
primesRequired = Integer.valueOf(args[0]).intValue();
} catch (NumberFormatException e) {
System.out.println("Prime count value invalid. Using default of "
+ primesRequired);
}
}
long[] primes = new long[primesRequired]; // Array to store primes
primes[0] = 2; // Seed the first prime
primes[1] = 3; // and the second
int count = 2; // Count of primes found - up to now, which is also the array index
long number = 5; // Next integer to be tested
outer:
for (; count < primesRequired; number += 2) {
// The maximum divisor we need to try is square root of number
long limit = (long)ceil(sqrt((double)number));
// Divide by all the primes we have up to limit
for (int i = 1; i < count && primes[i] <= limit; i++)
if (number % primes[i] == 0) // Is it an exact divisor?
continue outer; // yes, try the next number
primes[count++] = number; // We got one!
}
File aFile = new File("primes.txt");
FileOutputStream outputFile = null;
try {
outputFile = new FileOutputStream(aFile);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}
FileChannel file = outputFile.getChannel();
final int BUFFERSIZE = 100; // Buffer size in bytes
ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
DoubleBuffer doubleBuf = buf.asDoubleBuffer();
buf.position(8);
CharBuffer charBuf = buf.asCharBuffer();
LongBuffer longBuf = null;
String primeStr = null;
for (long prime : primes) {
primeStr = "prime = " + prime; // Create the string
doubleBuf.put(0,(double)primeStr.length()); // Store the string length
charBuf.put(primeStr); // Store the string
buf.position(2*charBuf.position() + 8); // Position for 3rd buffer
longBuf = buf.asLongBuffer(); // Create the buffer
longBuf.put(prime); // Store the binary long value
buf.position(buf.position() + 8); // Set position after last value
buf.flip(); // and flip
try {
file.write(buf); // Write the buffer as before.
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
buf.clear();
doubleBuf.clear();
charBuf.clear();
}
try {
System.out.println("File written is " + file.size() + " bytes.");
outputFile.close(); // Close the file and its channel
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
//AJOUT pour lire mais NOT OK
System.out.println();
System.out.println("Essai lecture ");
File aFile2 = new File("primes.txt");
FileInputStream inputFile = new FileInputStream(aFile2);
FileChannel fci = inputFile.getChannel();
buf.position(8);
buf.limit(35);
//for (long prime : primes) {
int nb = 0;
while ((nb = fci.read(buf)) != -1){
//fci.read(buf);
System.out.println(doubleBuf.get(0));
System.out.println(charBuf.get());
buf.position(2*charBuf.position() + 8);
System.out.println(longBuf.get());
buf.position(buf.position() + 8);
buf.flip();
}
System.exit(0);
}
} |