Coder une méthode peek() ?
Bonjour,
J'essaie de coder une méthode peek() [voir la dernière méthode dans le code ci-bas]. L'idée étant de faire en sorte que "public SAMLine peek()" me renseigne que sera la prochaine ligne SAM (vide en fin de fichier), mais sans avancer dans le fichier. J'ai beau essayer mais je ne vois pas comment y arriver.
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
| public class SAMFile implements Iterator<SAMLine> {
private BufferedReader in = null;
private String line = null;
private SAMLine string = null;
private FileReader reader;
/**
* Constructs ...
* @param file
* @throws IOException
*/
public SAMFile(File file) throws IOException {
try {
reader = new FileReader(file);
in = new BufferedReader(reader);
line = in.readLine();
if (!line.startsWith("@")) {
string = new SAMLine(line);
}
if (string == null) {
in.close();
in = null;
}
} catch (IOException e) {
string = null;
if (in != null) {
try {
in.close();
} catch (IOException e2) {}
}
in = null;
throw e;
}
}
@Override
public boolean hasNext() {
return string != null;
}
@Override
public SAMLine next() throws NoSuchElementException {
SAMLine returnString = string;
try {
if (string == null) {
throw new NoSuchElementException("Next line is not available");
} else {
if (!line.startsWith("@")) {
string = new SAMLine(line);
}
if ((string == null) && (in != null)) {
in.close();
in = null;
}
}
} catch (NoSuchElementException | IOException ex) {
throw new NoSuchElementException("Exception caught in SAMLineIterator.next() " + ex);
}
return returnString;
}
@Override
public void remove() {
throw new UnsupportedOperationException("FileLineIterator.remove() is not supported");
}
@Override
protected void finalize() throws Throwable {
try {
string = null;
if (in != null) {
try {
in.close();
} catch (Exception e) {}
}
in = null;
} finally {
super.finalize();
}
}
public SAMLine peek() {
// TO DO
return null;
}
} |