📄 dbf.java
字号:
*/
public StringBuffer GetDbfRec(int row)throws java.io.IOException{
StringBuffer record;// = new StringBuffer(rec_size);
if(!isFile){
return record = new StringBuffer(records[row].toString());
}
else{
record = new StringBuffer(rec_size+numfields);
rFile.seek(data_offset+(rec_size*row));
//Modifed to use Hisaji ONO's approach for reading multi byte character sets
byte[] strbuf = new byte[rec_size]; // <---- byte array buffer fo storing string's byte data
for(int i=0;i<rec_size;i++){
strbuf[i] = dFile.readByte(); // <---- read string's byte data
}
record.append(new String(strbuf)); // <- append byte array to String Buffer
return record;
}
}
/**
* fetches the <i>row</i>th row of the file and parses it into an vector
* of objects.
* @param row - the row to fetch
* @exception java.io.IOException on read error.
*/
public Vector ParseDbfRecord(int row)throws java.io.IOException{
return ParseRecord(GetDbfRec(row));
}
/**
* Parses the record stored in the StringBuffer rec into a vector of
* objects
* @param StringBuffer rec - the record to be parsed.
*/
public Vector ParseRecord(StringBuffer rec){
Vector record=new Vector(numfields);
String t;
Integer I=new Integer(0);
Float F=new Float(0.0);
t = rec.toString();
for(int i=0;i<numfields;i++){
if(DEBUG)System.out.println(DBC+"type "+fielddef[i].fieldtype);
if(DEBUG)System.out.println(DBC+"start "+fielddef[i].fieldstart);
if(DEBUG)System.out.println(DBC+"len "+fielddef[i].fieldlen);
if(DEBUG)System.out.println(DBC+""+t.substring(fielddef[i].fieldstart,
fielddef[i].fieldstart+fielddef[i].fieldlen));
switch(fielddef[i].fieldtype){
case 'C':
record.addElement(t.substring(fielddef[i].fieldstart,
fielddef[i].fieldstart+fielddef[i].fieldlen));
break;
case 'N':
case 'F':
if(fielddef[i].fieldnumdec==0)
try{
record.addElement(I.decode(t.substring(
fielddef[i].fieldstart,fielddef[i].fieldstart+fielddef[i].fieldlen)));
}catch(java.lang.NumberFormatException e){
record.addElement(new Integer(0));
}
else
try{
record.addElement(F.valueOf(t.substring(
fielddef[i].fieldstart,fielddef[i].fieldstart+fielddef[i].fieldlen)));
}catch(java.lang.NumberFormatException e){
record.addElement(new Float(0.0));
}
break;
default:
if(DEBUG)System.out.println(DBC+"Oh - don't know how to parse "
+fielddef[i].fieldtype);
}
}
return record;
}
/**
* Fetches a column of Integers from the database file.
* @param int col - the column to fetch
* @exception java.io.IOException - on read error
* @exception DbfFileException - column is not an Integer.
*/
public Integer[] getIntegerCol(int col )
throws java.io.IOException,DbfFileException
{
return getIntegerCol(col,0,last_rec);
}
/**
* Fetches a part column of Integers from the database file.
* @param int col - the column to fetch
* @param int start - the row to start fetching from
* @param int end - the row to stop fetching at.
* @exception java.io.IOException - on read error
* @exception DbfFileException - column is not an Integer.
*/
public Integer[] getIntegerCol(int col, int start, int end)
throws java.io.IOException,DbfFileException {
Integer column[]=new Integer[end-start];
String record = new String();
StringBuffer sb = new StringBuffer(numfields);
int k=0,i=0;
if(col>=numfields)
throw new DbfFileException(DBC+"No Such Column in file: "+col);
if(fielddef[col].fieldtype!='N')
throw new DbfFileException(DBC+"Column "+col+" is not Integer "+
fielddef[col].fieldtype);
if(start<0)
throw new DbfFileException(DBC+"Start must be >= 0");
if(end>last_rec)
throw new DbfFileException(DBC+"End must be <= "+last_rec);
// move to start of data
try{
for(i=start;i<end;i++){
sb.setLength(0);
sb=GetDbfRec(i);
record=sb.toString();
if(DEBUG)System.out.println(DBC+record.substring(fielddef[col].fieldstart,
fielddef[col].fieldstart+fielddef[col].fieldlen).trim()+"*");
column[i-start]=new Integer(record.substring(fielddef[col].fieldstart,
fielddef[col].fieldstart+fielddef[col].fieldlen).trim());
}
}
catch(java.lang.NumberFormatException nfe){
//throw new DbfFileException(DBC+"Column "+col+" contains an non integer id number "+nfe);
// be nicer
column[i-start]=new Integer(0);
}
catch(java.io.EOFException e){
System.err.println(e);
System.err.println("Dbf->record "+i+" byte "+k+" file pos "
);}
catch(java.io.IOException e){
System.err.println(e);
System.err.println("Dbf->record "+i+" byte "+k+" file pos "
);}
return column;
}
/**
* Fetches a column of Floats from the database file.
* @param int col - the column to fetch
* @exception java.io.IOException - on read error
* @exception DbfFileException - column is not an Integer.
*/
public Float[] getFloatCol(int col) throws DbfFileException,
java.io.IOException{
return getFloatCol(col,0,last_rec);
}
/**
* Fetches a part column of Floats from the database file.
* @param int col - the column to fetch
* @param int start - the row to start fetching from
* @param int end - the row to stop fetching at.
* @exception java.io.IOException - on read error
* @exception DbfFileException - column is not an Integer.
*/
public Float[] getFloatCol(int col,int start,int end)
throws DbfFileException, java.io.IOException{
Float column[]=new Float[end-start];
String record,st;
StringBuffer sb = new StringBuffer(rec_size);
int k=0,i=0;
if(col>=numfields)
throw new DbfFileException("Dbf->No Such Column in file: "+col);
if(fielddef[col].fieldtype!='F'&&fielddef[col].fieldtype!='N')
throw new DbfFileException("Dbf->Column "+col+" is not Float "
+fielddef[col].fieldtype);
if(start<0)
throw new DbfFileException("Dbf->Start must be >= 0");
if(end>last_rec)
throw new DbfFileException("Dbf->End must be <= "+last_rec);
// move to start of data
try{
for(i=start;i<end;i++){
sb.setLength(0);
sb=GetDbfRec(i);
record=sb.toString();
st=new String(record.substring(fielddef[col].fieldstart,
fielddef[col].fieldstart+fielddef[col].fieldlen)).trim();
if(st.indexOf('.')==-1){
st=st+".0";
}
try{
column[i-start]=new Float(st);
}catch(java.lang.NumberFormatException e){
column[i-start]=new Float(0.0);
}
}
}
catch(java.io.EOFException e){
System.err.println("Dbf->"+e);
System.err.println("Dbf->record "+i+" byte "+k+" file pos ");}
catch(java.io.IOException e){
System.err.println("Dbf->"+e);
System.err.println("Dbf->record "+i+" byte "+k+" file pos ");}
return column;
}
/**
* Fetches a column of Strings from the database file.
* @param int col - the column to fetch
* @exception java.io.IOException - on read error
* @exception DbfFileException - column is not an Integer.
*/
public String[] getStringCol(int col) throws DbfFileException,
java.io.IOException{
return getStringCol(col,0,last_rec);
}
/**
* Fetches a part column of Strings from the database file.
* @param int col - the column to fetch
* @param int start - the row to start fetching from
* @param int end - the row to stop fetching at.
* @exception java.io.IOException - on read error
* @exception DbfFileException - column is not an Integer.
*/
public String[] getStringCol(int col,int start,int end)
throws DbfFileException, java.io.IOException{
String column[]=new String[end-start];
String record = new String();
StringBuffer sb = new StringBuffer(numfields);
int k=0,i=0;
if(col>=numfields)
throw new DbfFileException("Dbf->No Such Column in file: "+col);
//if(fielddef[col].fieldtype!='C')
//throw new DbfFileException("Column "+col+" is not a String");
if(start<0)
throw new DbfFileException("Dbf->Start must be >= 0");
if(end>last_rec)
throw new DbfFileException("Dbf->End must be <= "+last_rec);
// move to start of data
try{
for(i=start;i<end;i++){
sb.setLength(0);
sb=GetDbfRec(i);
record=sb.toString();
column[i-start]=new String(record.substring(fielddef[col].fieldstart,
fielddef[col].fieldstart+fielddef[col].fieldlen));
}
}
catch(java.io.EOFException e){
System.err.println("Dbf->"+e);
System.err.println("Dbf->record "+i+" byte "+k+" file pos ");}
catch(java.io.IOException e){
System.err.println("Dbf->"+e);
System.err.println("Dbf->record "+i+" byte "+k+" file pos ");}
return column;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -