Dobrý večer.
Riešim problém s databázov, kde program číta užívateľov z generátora a pri čítaní nastáva chyba.
Chyba:
Chyba pocas citania suboru:java.io.UTFDataFormatException: malformed input: partial character at end
Chyba pocas citania suboru:java.io.UTFDataFormatException: malformed input around byte 8198
Chyba pocas citania suboru:java.io.UTFDataFormatException: malformed input around byte 8171
Chyba pocas citania suboru:java.io.UTFDataFormatException: malformed input around byte 8144
Chyba pocas citania suboru:java.io.UTFDataFormatException: malformed input: partial character at end
Chyba pocas citania suboru:java.io.UTFDataFormatException: malformed input around byte 0
Chyba pocas citania suboru:java.io.UTFDataFormatException: malformed input around byte 26
Chyba pocas citania suboru:java.io.UTFDataFormatException: malformed input around byte 26
Zoznamka:
import java.util.Scanner;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.io.*;
public class ZoznamkaDB
{
//forma tabulky v dBaze
private static int dlzkaZaznamu=60,//nie je bez 0-tej pozicie
dlzkaPriezviska=10,
dlzkaPriezviskaMena=20,
dlzkaLokality=10,
pocZaznamov=20000;
static int a=7;
byte l;
private static long zac,kon;
private static String menoSub = "reta.dat",menoSubPom="PomocnySubor.dat";
private static int porovnajRetazce(int ii,String xx)
{
String r1="",pomocna4;
int kc;
try
{
RandomAccessFile pom= new RandomAccessFile(menoSubPom,"rw");
pom.seek((long)((ii-1)*(dlzkaPriezviska+10+a)));//nahrada za 8
pomocna4=pom.readUTF();
pomocna4=pomocna4.substring(0,8);
kc=(byte)8;//(pomocna4.charAt(0))-(byte)('0');//nahrada za 8
r1=pomocna4.substring(0,kc);//nahrada za(9)
pom.close();
}
catch(IOException exception)
{
System.out.println("Chyba pocas citania suboru:" +exception);
}
return r1.compareTo(xx);
}
public static void sortHoare(int lavy, int pravy)
{
int kk,i,j,s;
char zn;
String x,m,pomocne,pomocne2;
try
{
RandomAccessFile pom= new RandomAccessFile(menoSubPom,"rw");
i=lavy;
j=pravy;
s=(lavy+pravy)/2;
pom.seek((long)((s-1)*(dlzkaPriezviska+2+a)));//nahrada (2)
pomocne=pom.readUTF();//nahrada (2)//tu mi to zhebne !!!
kk=(byte)8;//(pomocne.charAt(0)-(byte)('0'));//nahrada (2)
x=pomocne.substring(0,kk);//nahrada (3)
do
{
while((porovnajRetazce(i,x)<0)&&(i<=j))
i++;
while((porovnajRetazce(j,x)>0)&&(i<=j))
j--;
if (i<=j)
{
//m=pQs[i];(4)
pom.seek((long)((i-1)*(dlzkaPriezviska+2+a)));//nahrada za (4)
m=pom.readUTF();//nahrada za (4)
//pQs[i]=pQs[j];(5)
pom.seek((long)((j-1)*(dlzkaPriezviska+2+a)));//nahrada za (5)
pomocne2=pom.readUTF();//nahrada za (5)
pom.seek((long)((i-1)*(dlzkaPriezviska+2+a)));//nahrada za (5)
pom.writeUTF(pomocne2);//nahrada za (5)
//pQs[j]=m;(6)
pom.seek((long)((j-1)*(dlzkaPriezviska+2+a)));//nahrada za (6)
pom.writeUTF(m);//nahrada za (6)
i++;
j--;
}
}
while (i<=j);
if (lavy < j)
sortHoare(lavy,j);
if (pravy > i)
sortHoare(i,pravy);
pom.close();
}
catch(IOException exception)
{
System.out.println("Chyba pocas citania suboru:" +exception);
}
}
public static void main(String[]args)
{
long start=System.currentTimeMillis();
String menoSub="reta.dat";
String ss,menoSublv="retaz.idx",
gHIPriezv,
gHIZaluba,
gHILokal="",
gHIPohlavie,
celyZaznam="",
gPriezv,
gLokal;
int i,j,k,pocZobr=0,pr;
StringBuffer bb,bb1;
char kk;
String zapis;
Scanner vst=new Scanner(System.in);
System.out.print("\f");
try
{
RandomAccessFile ff= new RandomAccessFile(menoSub,"rw");
RandomAccessFile pom= new RandomAccessFile(menoSubPom,"rw");
System.out.println("START vyroby pretriedeneho suboru ..");
zac=System.currentTimeMillis();
j=0;
gPriezv="";
for (i=1;i<=(ff.length()/(dlzkaZaznamu+2));i++)
{
ff.seek((long)((i-1)*(dlzkaZaznamu+2)));
ss=ff.readUTF();
kk='8';//(char)((byte)('0')+k);
bb1= new StringBuffer(ss.substring(0,8));
bb1=bb1.append(i);
gPriezv=bb1.toString();
while(gPriezv.length()!=15)//preto, aby mali polozky rovnaku dlzku, kvoli seeku
{
gPriezv=gPriezv+" ";
}
//bb.toString();
if (i<pocZobr)
System.out.println(gPriezv);
//pQs[i]=gPriezv; toto treba nahradiť suborom (1)
pom.writeUTF(gPriezv);//nahradene (1)
}
ff.close();
pom.close();
}
catch(IOException exception)
{
System.out.println("Chyba pocas citania suboru:" +exception);
}
System.out.println();
sortHoare(1,pocZaznamov);
kon=System.currentTimeMillis();
System.out.println("Koniec vyroby indexu vyhladavania ");
System.out.println("cas vypoctu= "+(kon-zac)+" ms");
//tu bol quicksort
System.out.println();
System.out.println("Zadaj priezvisko, ktore chces vyhladat");
gHIPriezv=vst.nextLine();
int la=1,st=0,hidx=0;
pr=pocZaznamov;
boolean nasiel=false;
try
{//tu sa vyuziva indexovy subor na vyhladavanie v databaze bisekciou
RandomAccessFile fiv=new RandomAccessFile(menoSublv,"r");
RandomAccessFile ff=new RandomAccessFile(menoSub,"r");
System.out.println("start vyhladavania cez index...");
zac=System.currentTimeMillis();
while(!nasiel&&(la<=pr))
{
st=(la+pr)/2;
fiv.seek((long)((st-1)*4));
j=fiv.readInt();
ff.seek((long)((j-1)*(dlzkaZaznamu+4)));
ss=ff.readUTF();
bb=new StringBuffer(ss);
k=bb.indexOf(" ");
bb1=new StringBuffer(bb.substring(0,k));
gPriezv=bb1.toString();
k=gPriezv.compareTo(gHIPriezv);
if(k==0)
{
hidx=j;
nasiel=true;
celyZaznam=ss;
}
else
{
if(k>0)
pr=st-1;
else
la=st+1;
}
}
kon=System.currentTimeMillis();
System.out.println("koniec vyhladavania cez index");
System.out.println("cas vypoctu= "+(kon-zac)+"ms");
if(nasiel)
{
System.out.println("Nasiel priezvisko "+gHIPriezv+" na indexe: "+hidx);
System.out.println("Zaznam: "+celyZaznam);
}
else
System.out.println("Priezvisko "+gHIPriezv+" nenasiel");
fiv.close();
ff.close();
}
catch(IOException chyba)
{
System.out.println("Chyba pocas prace so suborom: "+chyba);
}
System.out.println(" ");
// stretnutie ********
Scanner aStretnutie = new Scanner (System.in);
System.out.println("Chcete sa stretnut(ano/nie): ");
String uzivAstretnutie = aStretnutie.next();
System.out.println("Odpovedali ste "+uzivAstretnutie+" !");
// *********************
System.out.println("koniec");
}
}
Generátor:
import java.io.RandomAccessFile;
import java.io.IOException;
import java.util.Scanner;
public class GeneratorDB
{
private static int dlzkaZaznamu=24,//nezaratana nulta pozicia
dlzkaPriezviska=10, dlzkaPriezviskaMena=20,pocZaznamov=2000000,dlzkaZaluba=34,dlzkaLokalita=54,dlzkaPohlavie=60;
byte aDI,i;
public static char typPis(int t)
{
char[] vsp={'B','C','E','D','I','F','G','H','J','K','L','M','N','V','Z'};
char[] vsa={'A','E','I','O','U'};
char[] sp={'b','c','e','d','i','f','g','h','j','k','l','m','n','o','v','z'};
char[] sa={'a','a','e','i','i','o','u'};
char y='.';
if(t==0)
y=vsp[(int)Math.round(Math.random()*14)];
if(t==1)
y=vsa[(int)Math.round(Math.random()*4)];
if(t==2)
y=sp[(int)Math.round(Math.random()*15)];
if(t==3)
y=sa[(int)Math.round(Math.random()*6)];
return y;
}
public static String genPriezv(byte mDI)
{
int pocet=mDI;
char pis[]=new char [pocet];
int pod;
String priezv;
pis[0]=typPis(pod=(int)Math.round(Math.random()));
priezv=""+pis[0];
pod=(pod==0)?2:3;
for(int i=1;i<pocet;i++)
{
if(pod==3)
pis[i]=typPis(2);
if(pod==2)
pis[i]=typPis(3);
priezv=priezv+pis[i];
pod=(pod==2)?3:2;
}
return priezv;
}
public static void main(String[]args)
{
String[] cMena={"Katarina","Milan","Eva","Robo","Laco","Ema","Maria","Dagmar","Adam","Karol","Samuel","Artur","Lucia","Dano"};
String[] cZaluby={"futbal","hokej","skovavaie","golf","skakanie"};
String[] cLokality={"Bratislva","Zilina","Smokovec","Bytca","Hricov","Trnove","Trnava","Presov","Senica","Uganda",};
String[] cPohlavia={"muz","zena"};
String menoSub="reta.dat",ss,gPriezv,gMeno,gVek,gMenoAPriezv,gZaluba,gLokal,gPohlavie;
int i,j,dlp,cG,pointer;
Scanner vst=new Scanner(System.in);
try
{
RandomAccessFile ff=new RandomAccessFile(menoSub,"rw");
cG=1;
System.out.print("\f");
for(i=1;i<=pocZaznamov;i++)
{
gMenoAPriezv="";
//n. g. krstne meno z pola konstant mien
j=(int)(Math.floor(14*Math.random()));
gMeno=cMena[j];
//n. g. dlzku dlzku priezviska, nie mensiu nez 3 znaky a nie vacsiu nez 15
dlp=(int)(3+Math.floor((dlzkaPriezviska-4)*Math.random()));
gPriezv=genPriezv((byte)(dlp));
//n. g. lokalita z pola konstant lokalit
j=(int)(Math.floor(2*Math.random()));
gLokal=cLokality[j];
//n. g. zaluba z pola konstant zalub
j=(int)(Math.floor(5*Math.random()));
gZaluba=cZaluby[j];
//n. g. pohlavia z pola konstant pohlavi
j=(int)(Math.floor(2*Math.random()));
gPohlavie=cPohlavia[j];
//n. g. vek osoby 1..89
j=(int)(1+Math.floor(89*Math.random()));
gVek=String.valueOf(j);
gMenoAPriezv=gPriezv;
for(j=gMenoAPriezv.length();j<=10;j++)
gMenoAPriezv=gMenoAPriezv+" ";
gMenoAPriezv=gMenoAPriezv+gMeno;
for(int k=gMenoAPriezv.length();k<=20;k++)
gMenoAPriezv=gMenoAPriezv+" ";
gMenoAPriezv=gMenoAPriezv+gVek;
//zaluby
for(int k=gMenoAPriezv.length();k<=24;k++)
gMenoAPriezv=gMenoAPriezv+" ";
gMenoAPriezv=gMenoAPriezv+gZaluba;
//lokalita
for(int k=gMenoAPriezv.length();k<=34;k++)
gMenoAPriezv=gMenoAPriezv+" ";
gMenoAPriezv=gMenoAPriezv+gLokal;
//pohlavia
for(int k=gMenoAPriezv.length();k<=54;k++)
gMenoAPriezv=gMenoAPriezv+" ";
gMenoAPriezv=gMenoAPriezv+gPohlavie;
for(int k=gMenoAPriezv.length();k<=60;k++)
gMenoAPriezv=gMenoAPriezv+" ";
ff.writeUTF(gMenoAPriezv);
//System.out.printf("%4d",cG);
//System.out.print(" ");
//System.out.printf("%31s",gMenoAPriezv);
//System.out.println();
//cG++;
}
//ak sa modifikoval subor teraz ho skratim
ff.setLength(ff.getFilePointer());
System.out.println();
System.out.println("zadaj poziciu udaju na citanie");
System.out.println("Zadaj menej nez- "+(int)(pocZaznamov +1));
j=vst.nextInt();
pointer=((j-1)*(dlzkaZaznamu+4));
ff.seek((long)(pointer));
ss=ff.readUTF();
System.out.println("Na pozicii "+j+" sa precital udaj: "+ss);
System.out.println("Velkost suboru je: "+ff.length()+" BYTOV");
ff.close();
}
catch(IOException chyba)
{
System.out.println("Chyba pocas citania zo suboru "+chyba);
}
System.out.println();
System.out.println(" ");
System.out.println("KONIEC");
}
}
Vopred ďakujem za pomoc.