You are on page 1of 7

/*

* To change this template, choose Tools | Templates


* and open the template in the editor.
*/
package ArchivosDeEnteros;
/**
*
* @author AdaLL
*/
import java.io.*;
public class Archivo
{
/*SE CREA EL ARCHIVO*/
public static void Agregar()
{
String leyendocadena;
FileWriter escribir;

BufferedReader entrada= new BufferedReader( new InputStreamReader(Sy


stem.in));
//creamos el archivo para escribir , el FileWriter
try{
escribir = new FileWriter("Archivo.txt");
}catch(IOException exc){
System.out.println("no se puede abrir archivo");
return ;
}

System.out.println("introduce los nombres a ordenar, escriba INICIAR


para salir");
do{
System.out.println(":");
leyendocadena= escribir.readLine();
if (leyendocadena.compareTo("INICIAR")==0) break;
escribir.write(leyendocadena);

}while(leyendocadena.compareTo("INICIAR")!=0);
escribir.close();
}

/*SE LEE EL ARCHIVO CREADO*/


public static void Leer()
{
int a=0;
DataInputStream salida=null;
try
{
salida= new DataInputStream( new FileInputStream
("Archivo"));
System.out.print("\nArchivo:\n");
while(salida.available()!=0)
{
a=salida.readInt();
System.out.println(""+a);
}
salida.close();
}
catch(IOException e)
{
System.out.println("NO SE ABRE REVISA SI EL ARCHIVO EXIS
TE");
}
}
/* EN ESTE METODO SE HACE LA PARTICION DEL ARCHIVO PRINCIPAL Y SE VA ASIGNANDO
* A LOS ARCHIVOS DE A O BQUE EN DONDE ES ESCRIBE LOS VALORES YA SEAN MENORES
*O MAYORES */
public static void particionar()
{
int aux=0,dato=0,pos=1;
DataInputStream Archivo1= null;
DataOutputStream A=null;
DataOutputStream B=null;
try
{
Archivo1=new DataInputStream(new FileInputStream("Archiv
o"));
A = new DataOutputStream( new FileOutputStream("AuxN1"))
;
B = new DataOutputStream( new FileOutputStream("AuxN2"))
;
}
catch(IOException e)
{
System.out.println("ERROR AL PARTICIONAR EL ARCHIVO");
}
try
{
while (Archivo1.available()!=0)
{
dato=Archivo1.readInt();
if(pos==1)
if(aux<=dato)
{
A.writeInt(dato);
aux=dato;
}
else
{
pos=2;
B.writeInt(dato);
aux=dato;
}
else
if(pos==2)
if(aux<=dato)
{
B.writeInt(dato);
aux=dato;
}
else
{
pos=1;
A.writeInt(dato);
aux=dato;
}
}
Archivo1.close();
A.close();
B.close();
}
catch(IOException e)
{
System.out.println("ERROR AL PARTICIONAR EL ARCHIVO");
}
}
/*EN ESTE METODO SE MUESTRAN LOS ARCHIVOS YA PARTICIONADOS QUE SON C Y D
*Y QUE CONTIENEN A AUXN1 Y AUXN2 RESPECTIVAMENTE*/
public static void mostrar()
{
int a=0;int b=0;
DataInputStream C = null;
DataInputStream D = null;
try
{
C = new DataInputStream( new FileInputStream("AuxN1"));
D = new DataInputStream( new FileInputStream("AuxN2"));
//SE LEEN LOS VALORES DEL ARCHIVO C Y SE ASIGNA A LA VARIABLE a
// Y DESPUES SON DESPLEGADOS EN PANTALLA
System.out.print("AuxN1:");
while (C.available()!=0){
a=C.readInt();
System.out.print (" "+a);
}
C.close();
System.out.println("");
//SE LEEN LOS VALORES DEL ARCHIVO D Y SE ASIGNA A LA VARIABLE b
// Y DESPUES SON DESPLEGADOS EN PANTALLA
System.out.print("AuxN2:");
while (D.available()!=0){
b=D.readInt();
System.out.print (" "+b);
}
D.close();
}
catch(IOException e)
{
System.out.println("ERROR AL MOSTRAR EL ARCHIVO");
}

}/*EN ESTE METODO SE HACE LA FUSION DE A CON B SE VA COMPARANDO LOS


DATOS DEL ARCHIVO A CON LOS DATOS DEL ARCHIVO B QUE CONTIENE
LOS DATOS AUXN1 Y AUXN2 RESPECTIVAMENTE Y DESPUES SON ESCRITOS EN OTR
O
ARCHIVO LLAMADO E*/
public static void fusion()
{
int dato2=0,auxa=0,auxb=0,y=0;
int dato=0;
boolean f=false;

DataInputStream lecturaA=null;
DataInputStream lecturaB=null;
DataOutputStream E=null;
try
{
lecturaA = new DataInputStream( new FileInputStream("Au
xN1"));
lecturaB = new DataInputStream( new FileInputStream("Au
xN2"));
E = new DataOutputStream( new FileOutputStream("Archivo"
,false));
/*SI EL ARCHIVO LECTURAA ES IGUAL A 0
* MIENTRAS EL ARCHIVO LECTURAA QUE CONTIENE LOS DATOS DE AUXN1
SEA DIFERENTE DE 0 SE LEE EL DATO DE TIPO ENTERO Y SE ASIGNA A
DATO Y DESPUES SE ESCRIBE EN EL ARCHIVO E */
if(lecturaB.available()==0)
while(lecturaA.available()!=0)
{
dato=lecturaA.readInt();
E.writeInt(dato);
}
else
{
dato=lecturaA.readInt();
auxa=dato;
dato2=lecturaB.readInt();
auxb=dato2;
//SE COMPARARN LOS DATOS ASIGNADOS A AUXA Y AUXB
//SI AUXA ES MENOR QUE AUXB SE ESCRIBE EL VALOR DE AUXA
EN E Y LE
//ASIGNA Y=1 QUE ES UNA BANDERA
if(auxa<auxb)
{
E.writeInt(auxa);
y=1;
}
//SI AUXA ES MAYOR QUE AUXB SE ESCRIBE EL VALOR DE AUXB EN E
Y SE
// LE ASIGNA Y=2 QUE ES UNA BANDERA
else
{
E.writeInt(auxb);
y=2;
}
//AQUI SE HACE LA COMPARACION DE A Y B
do
{
if(y==1)
{
if(lecturaA.available()==0&&lecturaB.available()==0)
{
if(auxa<auxb)
{
E.writeInt(auxb);
break;
}
else
{
E.writeInt(auxb);
E.writeInt(auxa);
break;
}
}
if(lecturaA.available()==0)
{
E.writeInt(auxb);
//AQUI LEE EL DATO DE LECTURAB Y LO ASIGNA A DATO2
//Y SE ESCRIBE EN EL ARCHIVO E
while(lecturaB.available()!=0)
{
dato2=lecturaB.readInt();
E.writeInt(dato2);
f=true;
}
}
if(lecturaA.available()!=0)
{
dato=lecturaA.readInt();
auxa=dato;
if(auxa<auxb)
{
E.writeInt(auxa);
y=1;
}
else
{
E.writeInt(auxb);
y=2;
}
}
}
if(y==2)
{
if(lecturaA.available()==0&&lecturaB.available()==0)
{
if(auxa<auxb)
{
E.writeInt(auxb);
break;
}
else
{
E.writeInt(auxa);
break;
}
}
if(lecturaB.available()==0)
{
E.writeInt(auxa);
//AQUI SE LEE EL DATO EN LECTURAA SE LE ASIGNA A DATO
//Y S ESCRIBE EN EL ARCHIVO E
while(lecturaA.available()!=0)
{
dato=lecturaA.readInt();
E.writeInt(dato);
f=true;
}
}
if(lecturaB.available()!=0)
{
dato2=lecturaB.readInt();
auxb=dato2;
if(auxa<auxb)
{
E.writeInt(auxa);
y=1;
}
else
{
E.writeInt(auxb);
y=2;
}
}
}
}while(!f);
}
lecturaA.close();
lecturaB.close();
E.close();
}
catch(IOException e)
{
System.out.println("ERROR AL FUSIONAR EL ARCHIVO");
}
}
/*ESTE METODO MANDA LLAMAR A TODOS LOS METODOS QUE SON LEER(),PARTICIONA
R()
*MOSTRAR(),FUSION() MIENTRAS AUXN2 SEA DIFERENTE DE 0*/
public static void fusionar()
{
DataInputStream Aux2=null;
try
{
Aux2 = new DataInputStream( new FileInputStream("AuxN2")
);

Leer();
System.out.println(" ");
particionar();
int cont=1;
while(Aux2.available()!=0)
{
System.out.println("\nPARTICION\t"+cont);
particionar();
mostrar();
fusion();
cont++;
}

}
catch(IOException e)
{
System.out.println("ERROR EN FLUJO DE ARCHIVO");
}
}
}

You might also like