📄 dbfnet.java
字号:
//modifed for two byte character sets.
//StringBuffer record = new StringBuffer(rec_size+numfields);
byte[] strbuf = new byte[rec_size+numfields];
for(int i=0;i< rec_size;i++){
// we could do some checking here.
strbuf[i] = dFile.readByte();
}
StringBuffer record = new StringBuffer(new String(strbuf));
//System.out.println(record);
return record;
}
private StringBuffer[] GrabFile() throws java.io.IOException{
StringBuffer records[] = new StringBuffer[last_rec];
for(int i=0;i<last_rec;i++) {
records[i]=GrabNextDbfRec();
}
return records;
}
/**
* fetches the <i>row</i>th row of the file
* @param row - the row to fetch
* @exception java.io.IOException on read error.
*/
public StringBuffer GetDbfRec(int row)throws java.io.IOException{
StringBuffer record;// = new StringBuffer(rec_size);
if(DEBUG)System.out.println("DbN->GetDbfRec("+row+") "+records[row]);
return record = new StringBuffer(records[row].toString());
}
/**
* 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("DbN->type "+fielddef[i].fieldtype);
if(DEBUG)System.out.println("DbN->start "+fielddef[i].fieldstart);
if(DEBUG)System.out.println("DbN->len "+fielddef[i].fieldlen);
if(DEBUG)System.out.println(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':
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));
}
break;
case 'F':
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("DbN->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("DbN->No Such Column in file: "+col);
if(fielddef[col].fieldtype!='N')
throw new DbfFileException("DbN->Column "+col+" is not Integer");
if(start<0)
throw new DbfFileException("DbN->Start must be >= 0");
if(end>last_rec)
throw new DbfFileException("DbN->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();
try{
column[i-start]=new Integer(record.substring(fielddef[col].fieldstart,
fielddef[col].fieldstart+fielddef[col].fieldlen));
}catch(java.lang.NumberFormatException e){
column[i-start]=new Integer(0);
}
}
}
catch(java.io.EOFException e){
System.err.println("DbN->"+e);
System.err.println("DbN->record "+i+" byte "+k+" file pos "
);}
catch(java.io.IOException e){
System.err.println("DbN->"+e);
System.err.println("DbN->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("DbN->No Such Column in file: "+col);
if(fielddef[col].fieldtype!='F')
throw new DbfFileException("DbN->Column "+col+" is not Float or Numeric it is of type "
+fielddef[col].fieldtype);
if(start<0)
throw new DbfFileException("DbN->Start must be >= 0");
if(end>last_rec)
throw new DbfFileException("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));
if(st.indexOf('.')==-1){
st=st+".0";
}
try{
column[i-start]=new Float(st);
}
catch(NumberFormatException nfe){
column[i-start]=new Float(0.0);
}
}
}
catch(java.io.EOFException e){
System.err.println("DbN->"+e);
System.err.println("DbN->record "+i+" byte "+k+" file pos ");}
catch(java.io.IOException e){
System.err.println("DbN->"+e);
System.err.println("DbN->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("DbN->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("DbN->Start must be >= 0");
if(end>last_rec)
throw new DbfFileException("DbN->End must be <= "+last_rec);
// move to start of data
try{
for(i=start;i<end;i++){
//sb.setLength(0);
byte[] strbuf = new byte[rec_size];
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(e);
System.err.println("DbN->record "+i+" byte "+k+" file pos ");}
catch(java.io.IOException e){
System.err.println(e);
System.err.println("DbN->record "+i+" byte "+k+" file pos ");}
return column;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -