Olá,
Estou criando um RandomAccessFile, mas não estou conseguindo. Já li tutoriais e APIs.
Por favor ...public void insere(Usuario umUsuario){
RandomAccessFile file;
try {
file = new RandomAccessFile(nomeDoArquivo,"rw");
FileChannel fc = file.getChannel( );
fc.position(fc.size());
ByteBuffer bb = ByteBuffer.allocateDirect (20);
byte[] userName = umUsuario.getUserName().getBytes();
bb.put(userName);
fc.write(bb);
bb = ByteBuffer.allocate(10);
byte[] password = umUsuario.getPassword().getBytes();
bb.put(userName);
fc.write(bb);
fc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Usuario busca(long pos){
RandomAccessFile file;
pos--;
Usuario result = new Usuario();
try {
file = new RandomAccessFile(nomeDoArquivo,"rw");
FileChannel fc = file.getChannel( );
fc.position(1);
ByteBuffer bb = ByteBuffer.allocateDirect (20);
fc.read(bb);
bb.rewind();
byte[] userName = new byte[20];
bb.put(userName);
result.setUserName(new String(userName));
bb = ByteBuffer.allocateDirect (10);
fc.read(bb);
bb.rewind();
byte[] password = new byte[10];
bb.get(password);
result.setUserName(new String(password));
fc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
return result;
}
O que está faltando ?
Obrigado,
Márcio