Parceiro vai ai uma CLasse de Registro para colcoar os registros dentro de um arquvo .Dat ou .txt o que for... E um arquivo binário e a classe registro sabe como ler e gravar a si propria em um arquivo.
A classe Arquivo_Java manipula o arquivo em si, seu contrutor recebe um nome de arquivo("meuArquivo.dat") e manipula os seus registros.
Nesta classe há uma busca binária e ua busca binária adaptada para o metodo de ordenação por inserção binária em arquivo.
Divirta-se.
PS: POstarei no meu material a classe com todos os métodos de ordenação em arquivo que fiz para um trabalho de Pesquisa e Ordenação deste semestre.
Classe registro
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package arquivo;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
*
* @author joao
*/
public class Registro {
public final int tf = 20;//variavel do tipo "final" é constante
private int codigo, tl = tf, idade;
private char nome[] = new char[tf];
public Registro() {
}
public Registro(int codigo){
this.codigo=codigo;
this.idade=codigo*2-1;
for (int i = 0; i < tf; i++)
nome[i] = 'X';
}
public Registro(int codigo, String Snome, int idade) {
this.codigo = codigo; //this é variavel de estancia
this.idade = idade;
this.tl = Snome.length();
for (int i = 0; i < Snome.length(); i++) {
nome[i] = Snome.charAt(i);
}
}
public int getCodigo() {
return (codigo);
}
public String getNome() {
String Snome = new String(nome);
return (Snome);
}
public void gravaNoArq(RandomAccessFile arquivo) {
try {
arquivo.writeInt(codigo);
arquivo.writeInt(idade);
arquivo.writeInt(tl);
for (int i = 0; i < tf; i++) {
arquivo.writeChar(nome[i]);
}
} catch (IOException e) {
}
}
public void leDoArq(RandomAccessFile arquivo) {
try {
this.codigo = arquivo.readInt();
this.idade = arquivo.readInt();
this.tl = arquivo.readInt();
for (int i = 0; i < this.tf; i++) {
this.nome[i] = arquivo.readChar();
}
for (int i = tl; i < tf; i++) {
this.nome[i] = ' ';
}
} catch (IOException e) {
}
}
public void exibirReg() {
int i;
System.out.print("codigo....." + this.codigo);
System.out.print(" nome.......");
String Snome = new String(nome);
System.out.print(Snome);
System.out.println(" idade......." + this.idade);
System.out.println("----------------------------------");
}
static int length() {
return (52); //int codigo, tl=20, idade; ------------> 12 bytes
//private char nome[] = new char[20]; --> 40 bytes
//------------------------------------------------- +
// Total : 40 + 12 = 52 bytes
}
}
//--------------------------------------------------------------------------------------------------
classe Arquivo_Java
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package arquivo;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
*
* @author joao
*/
public class Arquivo_Java {
private String nomearquivo;
private RandomAccessFile arquivo;
private final int TF = 256;
public Arquivo_Java(String nomearquivo) {
try {
arquivo = new RandomAccessFile(nomearquivo, "rw");
} catch (IOException e) {
}
}
public void truncate(long pos) //desloca eof
{
try {
arquivo.setLength(pos * Registro.length());
} catch (IOException exc) {
}
}
//semelhante ao feof() da linguagem C
//verifica se o ponteiro esta no <EOF> do arquivo
public boolean eof() {
boolean retorno = false;
try {
if (arquivo.getFilePointer() == arquivo.length()) {
retorno = true;
}
} catch (IOException e) {
}
return (retorno);
}
//insere um Registro no final do arquivo, passado por parâmetro
public void inserirRegNoFinal(Registro reg) {
seekArq(filesize());//ultimo byte
reg.gravaNoArq(arquivo);
}
public void exibirArq() {
int i;
Registro aux = new Registro();
seekArq(0);
i = 0;
while (!this.eof()) {
System.out.println("Posicao " + i);
aux.leDoArq(arquivo);
aux.exibirReg();
i++;
}
}
//..........................................................................
public void exibirUmRegistro(int pos) {
Registro aux = new Registro();
seekArq(pos);
System.out.println("Posicao " + pos);
aux.leDoArq(arquivo);
aux.exibirReg();
}
//..........................................................................
public void seekArq(int pos) {
try {
arquivo.seek(pos * Registro.length());
} catch (IOException e) {
}
}
//..........................................................................
public void leArq() throws IOException {
int codigo, idade;
String nome;
codigo = Entrada.leInteger("Digite o código");
while (codigo != 0) {
nome = Entrada.leString("Digite o nome");
idade = Entrada.leInteger("Digite a idade");
inserirRegNoFinal(new Registro(codigo, nome, idade));
codigo = Entrada.leInteger("Digite o código");
}
}
//..........................................................................
public int filesize() {
try {
return (int) arquivo.length() / Registro.length();
} catch (IOException e) {
}
return 0;
}
//------------------------------------------------------------------------------
public int buscaBinaria(int chave) {
Registro meio = new Registro();
int pIni = 0, pFim = filesize() - 1, pMeio = pFim / 2;
seekArq(pMeio);
meio.leDoArq(arquivo);
while (pIni < pFim && chave != meio.getCodigo()) {
if (chave < meio.getCodigo()) {
pFim = pMeio;
} else {
pIni = pMeio + 1;
}
pMeio = (pIni + pFim) / 2;
seekArq(pMeio);
meio.leDoArq(arquivo);
}
seekArq(pMeio);
meio.leDoArq(arquivo);
if (chave == meio.getCodigo()) {
return pMeio;
}
if (chave > meio.getCodigo()) {
return pMeio + filesize() + 1;
}
return pMeio + filesize();
}
//------------------------------------------------------------------------------
public int buscaBinariaA(int chave, int TL) {
int pos = 0, fim = TL - 1, ini = 0, meio = fim / 2;
Registro reg = new Registro();
seekArq(meio);
reg.leDoArq(arquivo);
while (chave != reg.getCodigo() && fim > ini) {
if (reg.getCodigo() > chave) {
fim = meio;
} else {
ini = meio + 1;
}
meio = (ini + fim) / 2;
seekArq(meio);
reg.leDoArq(arquivo);
}
return meio;
}
//------------------------------------------------------------------------------
public void insercaoBin() {
int i = 1, j, pos, tl = filesize() - 1;
Registro auxI = new Registro(), aux = new Registro();
while (i < tl) {
seekArq(i);
auxI.leDoArq(arquivo);
pos = buscaBinariaA(auxI.getCodigo(), i);
j = i;
while (j > pos) {
seekArq(pos - 1);
aux.leDoArq(arquivo);
aux.gravaNoArq(arquivo);
j--;
}
seekArq(pos);
auxI.gravaNoArq(arquivo);
i++;
}
}
//------------------------------------------------------------------------------
}
Para escrever sua resposta aqui, entre ou crie uma conta.
Compartilhar