(Created page with "=== Exemplo: Leitura e Escrita de Acesso Aleatório === <java5> public class LeituraEscritaAleatória { public static void main(String[] args) throws IOException { Rando...") |
(→Exemplo: Leitura e Escrita de Acesso Aleatório) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
=== Exemplo: Leitura e Escrita de Acesso Aleatório === | === Exemplo: Leitura e Escrita de Acesso Aleatório === | ||
− | < | + | <source lang="java"> |
− | public class LeituraEscritaAleatória { | + | public class LeituraEscritaAleatória { |
− | + | public static void main(String[] args) { | |
− | + | try (RandomAccessFile rf = new RandomAccessFile("raw.dat", "rw")) { | |
− | + | for (int i = 0; i < 10; i++) | |
− | + | rf.writeDouble(i * 1.414); | |
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
− | + | try (RandomAccessFile rf = new RandomAccessFile("raw.dat", "rw")) { | |
− | + | rf.seek(5 * 8); | |
− | + | rf.writeDouble(47.0001); | |
− | + | } catch (IOException e) { | |
+ | e.printStackTrace(); | ||
+ | } | ||
− | + | try (RandomAccessFile rf = new RandomAccessFile("raw.dat", "r")) { | |
− | + | for (int i = 0; i < 10; i++) | |
− | + | System.out.println("Valor " + i + ": " + rf.readDouble()); | |
− | + | } catch (IOException e) { | |
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
} | } | ||
− | + | </source> | |
− | |||
− | </ | ||
[[category:Ensino]] | [[category:Ensino]] |
public class LeituraEscritaAleatória {
public static void main(String[] args) {
try (RandomAccessFile rf = new RandomAccessFile("raw.dat", "rw")) {
for (int i = 0; i < 10; i++)
rf.writeDouble(i * 1.414);
} catch (IOException e) {
e.printStackTrace();
}
try (RandomAccessFile rf = new RandomAccessFile("raw.dat", "rw")) {
rf.seek(5 * 8);
rf.writeDouble(47.0001);
} catch (IOException e) {
e.printStackTrace();
}
try (RandomAccessFile rf = new RandomAccessFile("raw.dat", "r")) {
for (int i = 0; i < 10; i++)
System.out.println("Valor " + i + ": " + rf.readDouble());
} catch (IOException e) {
e.printStackTrace();
}
}
}