Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,265 additions
and
1,115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<project name="jdbf" default="jar" basedir="."> | ||
|
||
<description>jdbf - Java library to write/read DBF files</description> | ||
|
||
<property name="src.dir" location="${basedir}/src" /> | ||
<property name="build.dir" location="${basedir}/build" /> | ||
<property name="src.generated.dir" location="${build.dir}/java_generated_${version.tag}" /> | ||
<property name="classes.dir" location="${build.dir}/classes"/> | ||
<property name="dist.dir" location="${build.dir}/dist"/> | ||
<property name="jar.filename" location="${dist.dir}/jdbf.jar"/> | ||
|
||
<path id="main.classpath"> | ||
<pathelement path="${classes.dir}" /> | ||
</path> | ||
|
||
<target name="mkdirs"> | ||
<mkdir dir="${build.dir}" /> | ||
<mkdir dir="${classes.dir}" /> | ||
<mkdir dir="${dist.dir}" /> | ||
</target> | ||
|
||
<target name="clean"> | ||
<delete dir="${build.dir}" /> | ||
</target> | ||
|
||
<target name="compile" description="compile the source" depends="clean,mkdirs"> | ||
<javac destdir="${classes.dir}" debug="true"> | ||
<src path="${src.dir}"/> | ||
<classpath refid="main.classpath" /> | ||
</javac> | ||
</target> | ||
|
||
<target name="jar" description="Create JAR file" depends="compile"> | ||
<jar destfile="${jar.filename}" basedir="${classes.dir}"/> | ||
</target> | ||
|
||
</project> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,189 +1,139 @@ | ||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import net.iryndin.jdbf.DbfField; | ||
import net.iryndin.jdbf.DbfFieldTypeEnum; | ||
import net.iryndin.jdbf.DbfHeader; | ||
import net.iryndin.jdbf.DbfReader; | ||
import net.iryndin.jdbf.DbfReaderImpl; | ||
import net.iryndin.jdbf.DbfRecord; | ||
import net.iryndin.jdbf.DbfWriter; | ||
import net.iryndin.jdbf.DbfWriterImpl; | ||
|
||
|
||
public class JdbfMain { | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
//method1(args); | ||
method2(args); | ||
} | ||
|
||
public static void method2(String[] args) throws Exception { | ||
DbfReader r = new DbfReaderImpl(); | ||
File file = new File("data/215451/gds_im.dbf"); | ||
//File file = new File("data/215451/tir_im.dbf"); | ||
List<? extends DbfRecord> list = r.read(file, Charset.forName("Cp866")); | ||
DbfRecord record = null; | ||
for (DbfRecord rec : list) { | ||
record = rec; | ||
System.out.println(rec); | ||
} | ||
List<DbfField> fields = record.getFields(); | ||
String s = getStringRep(fields); | ||
System.out.println(s); | ||
|
||
File file2 = new File("1.dbf"); | ||
FileOutputStream out = new FileOutputStream(file2); | ||
DbfWriter writer = new DbfWriterImpl(); | ||
writer.write(out, list, Charset.forName("Cp866")); | ||
out.close(); | ||
} | ||
|
||
private static String getStringRep(List<DbfField> fields) { | ||
StringBuilder sb = new StringBuilder(1024*8); | ||
int i = fields.size(); | ||
for (DbfField f : fields) { | ||
sb.append(f.toStringRep()); | ||
i--; | ||
if (i>0) { | ||
sb.append("|"); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
public static void method1(String[] args) throws Exception { | ||
//Date d = new Date(); | ||
//System.out.println(d.getYear()+1900); | ||
|
||
//File file = new File("data/215451/gds_im.dbf"); | ||
File file = new File("data/215451/tir_im.dbf"); | ||
FileInputStream fin = new FileInputStream(file); | ||
byte[] fileContent = new byte[(int)file.length()]; | ||
fin.read(fileContent); | ||
fin.close(); | ||
|
||
|
||
ByteArrayInputStream in = new ByteArrayInputStream(fileContent); | ||
|
||
byte[] b = new byte[16]; | ||
// read 16 byts | ||
in.read(b); | ||
// create header | ||
DbfHeader header = new DbfHeader(b); | ||
// read next 16 bytes | ||
in.read(b); | ||
// read fields | ||
byte[] fieldBytes = new byte[32]; | ||
List<DbfField> fields = new ArrayList<DbfField>(128); | ||
while(true) { | ||
in.read(fieldBytes); | ||
in.mark(1); | ||
int terminator = in.read(); | ||
if (terminator == 0x0D) { | ||
break; | ||
} else { | ||
in.reset(); | ||
} | ||
DbfField field = new DbfField(fieldBytes); | ||
fields.add(field); | ||
} | ||
|
||
System.out.println("Total fields: " + fields.size()); | ||
|
||
|
||
Set<DbfFieldTypeEnum> fieldTypes = new HashSet<DbfFieldTypeEnum>(); | ||
int bytes = 0; | ||
for (DbfField fld : fields) { | ||
System.out.println(fld); | ||
bytes += fld.getLength(); | ||
|
||
fieldTypes.add(fld.getType()); | ||
} | ||
|
||
System.out.println("Record lengthL: " + bytes); | ||
System.out.println(header); | ||
System.out.println(fieldTypes); | ||
|
||
for (int i=0; i<header.getRecordsQty(); i++) { | ||
int recordMark = in.read(); | ||
readOneRecord(in,fields); | ||
//break; | ||
} | ||
|
||
} | ||
|
||
private static void readOneRecord(ByteArrayInputStream in, | ||
List<DbfField> fields) throws Exception { | ||
for (DbfField f : fields) { | ||
readFieldValue(f,in); | ||
} | ||
|
||
} | ||
|
||
private static void readFieldValue(DbfField f, ByteArrayInputStream in) throws Exception { | ||
switch(f.getType()) { | ||
case Character: | ||
readCharactes(f.getLength(), in); | ||
break; | ||
case Numeric: | ||
readNumeric(f.getLength(), in); | ||
break; | ||
case Date: | ||
readDate(f.getLength(), in); | ||
break; | ||
} | ||
|
||
} | ||
|
||
private static void readDate(int length, ByteArrayInputStream in) throws IOException { | ||
byte[] b = new byte[length]; | ||
in.read(b); | ||
String s = new String(b); | ||
s = s.trim(); | ||
if (s.length()==0) { | ||
s=null; | ||
} | ||
System.out.println(s); | ||
} | ||
|
||
private static void readNumeric(int length, ByteArrayInputStream in) throws IOException { | ||
byte[] b = new byte[length]; | ||
in.read(b); | ||
//int i=0; | ||
//for (i=0; i<length && i != 0x20; i++); | ||
String s = new String(b); | ||
s = s.trim(); | ||
if (s.length()==0) { | ||
s=null; | ||
} | ||
System.out.println(s); | ||
} | ||
|
||
private static void readCharactes(int length, ByteArrayInputStream in) throws IOException { | ||
byte[] b = new byte[length]; | ||
in.read(b); | ||
int i=0; | ||
//for (i=0; i<length && i != 0x20; i++); | ||
//String s = new String(b, 0, i, Charset.forName("Cp866")); | ||
String s = new String(b, Charset.forName("Cp866")); | ||
s = s.trim(); | ||
if (s.length() == 0) { | ||
s = null; | ||
} | ||
|
||
System.out.println(s); | ||
} | ||
|
||
} | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import net.iryndin.jdbf.core.DbfMetadata; | ||
import net.iryndin.jdbf.core.DbfRecord; | ||
import net.iryndin.jdbf.reader.DbfReader; | ||
import net.iryndin.jdbf.util.DbfMetadataUtils; | ||
import net.iryndin.jdbf.util.JdbfUtils; | ||
import net.iryndin.jdbf.writer.DbfWriter; | ||
|
||
|
||
public class JdbfMain { | ||
|
||
static String gdsString = "KONTR,C,1,0|N_MDP,C,8,0|W_LIST_NO,N,2,0|G32,N,3,0|N_RECEIVER,N,1,0|G33,C,10,0|G312,C,250,0|G35,N,13,2|G311,C,9,0|G318,C,14,0|G315,N,11,2|G317C,C,3,0|G221,C,3,0|G221_BUK,C,3,0|G42,N,15,2|KODS_PT,C,3,0|KODS_ABC2,C,2,0|N_TTH,C,30,0|G442REGNU,C,28,0|DELIV_PPP,C,6,0|G40T,C,2,0|G40,C,35,0|G405,N,2,0|TOV_SIGN2,C,1,0|CREATEDATE,D,8,0|MODIFIED_D,D,8,0|ARM_ID,N,3,0|VERSION,C,4,0"; | ||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
//method1(args); | ||
//method2(args); | ||
//method3(args); | ||
//method4(args); | ||
//method5(args); | ||
method6(args); | ||
} | ||
|
||
public static void method5(String[] args) throws Exception { | ||
Charset stringCharset = Charset.forName("Cp866"); | ||
//File file = new File("data/215451/gds_im.dbf"); | ||
File file = new File("data/215451/tir_im.dbf"); | ||
net.iryndin.jdbf.reader.DbfReader reader = new net.iryndin.jdbf.reader.DbfReader(file); | ||
DbfMetadata meta = reader.getMetadata(); | ||
System.out.println(meta); | ||
net.iryndin.jdbf.core.DbfRecord rec = null; | ||
List<net.iryndin.jdbf.core.DbfRecord> recs = new ArrayList<net.iryndin.jdbf.core.DbfRecord>(10); | ||
List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>(); | ||
while ((rec = reader.read()) != null) { | ||
rec.setStringCharset(stringCharset); | ||
String s = rec.getString("G022"); | ||
System.out.println(rec.getStringRepresentation()); | ||
|
||
Map<String,Object> map = rec.toMap(); | ||
maps.add(map); | ||
System.out.println(map); | ||
recs.add(rec); | ||
} | ||
reader.close(); | ||
|
||
String fieldsInfo = "KONTR,C,1,0|N_MDP,C,8,0|MDPLETTERS,C,2,0|W_LIST_NO,N,2,0|CUST_REG_N,C,6,0|CUSTOMPOST,C,8,0|DESIGN_PPP,C,6,0|G542,D,8,0|REG_TIME,C,5,0|SPECIF_GDS,N,3,0|G05_NTF,N,3,0|G222,N,16,2|G35_NTF,N,17,2|G531,C,8,0|GA3,C,8,0|GA1,C,100,0|A_NLIC,C,12,0|A_DLIC,D,8,0|G022,C,76,0|G023,C,150,0|G15C,C,3,0|G15C_ABC2,C,2,0|G501,C,52,0|G502,C,150,0|CNTRYDRIVE,C,3,0|DRIVE_ABC2,C,2,0|DRIVERTYPE,N,1,0|DRIVEROGRN,C,15,0|INN_DRIVER,C,20,0|DRIVERITN,C,13,0|DRIVER_FIO,C,60,0|DRIVERPASS,C,30,0|HOLDERIDN,C,18,0|FAGENTNAME,C,76,0|FAGENTADDR,C,76,0|ITN_RECEIV,C,15,0|G082,C,76,0|G083,C,150,0|OGRNR,C,15,0|INNR,C,12,0|KPP_RECEIV,C,9,0|ITNR,C,13,0|ONLYONEGDS,L,1,0|G05_NTFR,N,3,0|G222_R,N,16,2|G35_NTFR,N,17,2|CUSTCODES,C,8,0|G082S,C,76,0|G083S,C,150,0|GINNS,C,12,0|GKPPS,C,9,0|ONLYONEGD2,L,1,0|G05_NTFS,N,3,0|G222_S,N,16,2|G35_NTFS,N,17,2|CUSTCODET,C,8,0|G082T,C,76,0|G083T,C,150,0|GINNT,C,12,0|GKPPT,C,9,0|ONLYONEGD3,L,1,0|G05_NTFT,N,3,0|G222_T,N,16,2|G35_NTFT,N,17,2|G26,C,2,0|N_TD,C,250,0|N_TR,C,12,0|VIN,C,20,0|TC_COUNTRY,C,3,0|TC_ABC2,C,2,0|N_TRAILER,C,17,0|N_TRAILER2,C,17,0|TC_VOLUME,N,3,0|G3161,C,75,0|G52CODE,C,2,0|G447C,C,26,0|G52DATE,D,8,0|G504,D,8,0|SMELLED,C,23,0|SMELLEDATE,D,8,0|CODELNP_O,C,8,0|NUM_LNP_O,C,4,0|FOREIGN_PL,L,1,0|NUM_PLOM,C,15,0|ONCE_ADM,N,1,0|CMP_PGTD,C,1,0|NLIST,N,3,0|N_DOK_OUT,C,20,0|N_DOK_IN,C,20,0|D_IN_OKDT,D,8,0|PRIM,C,100,0|DAT_PD1,D,8,0|TIME_OUT1,C,5,0|PIGEONFIL1,C,12,0|TOTAL_NUV,N,2,0|DAT_PD,D,8,0|TIME_OUT,C,5,0|PIGEONFILE,C,13,0|GOTIT,C,1,0|GTK_FNAME,C,12,0|ISREAD,C,1,0|RTUN_FNAME,C,12,0|RTUN_READ,C,1,0|DATE_RCBD,D,8,0|SIGN2,C,1,0|RCP_CHKSUM,C,30,0|RTUO_FNAME,C,12,0|RTUO_READ,C,1,0|CREATEDATE,D,8,0|MODIFIED_D,D,8,0|ONTHEPAPER,D,8,0|STATUS_DOC,C,1,0|G05,N,3,0|AVTS_DOCNN,C,32,0|AVTS_RES_N,C,1,0|AVTS_DOCNC,C,32,0|AVTS_RES_C,C,1,0"; | ||
DbfMetadata meta1 = DbfMetadataUtils.fromFieldsString(fieldsInfo); | ||
meta1.setRecordsQty(maps.size()); | ||
//meta1.setRecordsQty(1); | ||
FileOutputStream out = new FileOutputStream("2.dbf"); | ||
net.iryndin.jdbf.writer.DbfWriter writer = new net.iryndin.jdbf.writer.DbfWriter(meta1,out); | ||
writer.setStringCharset("Cp866"); | ||
// for (net.iryndin.jdbf.core.DbfRecord r : recs) { | ||
// writer.write(rec); | ||
// } | ||
|
||
for (Map<String,Object> map : maps) { | ||
writer.write(map); | ||
} | ||
|
||
writer.close(); | ||
|
||
// String fieldsInfo = "KONTR,C,1,0|N_MDP,C,8,0|MDPLETTERS,C,2,0|W_LIST_NO,N,2,0|CUST_REG_N,C,6,0|CUSTOMPOST,C,8,0|DESIGN_PPP,C,6,0|G542,D,8,0|REG_TIME,C,5,0|SPECIF_GDS,N,3,0|G05_NTF,N,3,0|G222,N,16,2|G35_NTF,N,17,2|G531,C,8,0|GA3,C,8,0|GA1,C,100,0|A_NLIC,C,12,0|A_DLIC,D,8,0|G022,C,76,0|G023,C,150,0|G15C,C,3,0|G15C_ABC2,C,2,0|G501,C,52,0|G502,C,150,0|CNTRYDRIVE,C,3,0|DRIVE_ABC2,C,2,0|DRIVERTYPE,N,1,0|DRIVEROGRN,C,15,0|INN_DRIVER,C,20,0|DRIVERITN,C,13,0|DRIVER_FIO,C,60,0|DRIVERPASS,C,30,0|HOLDERIDN,C,18,0|FAGENTNAME,C,76,0|FAGENTADDR,C,76,0|ITN_RECEIV,C,15,0|G082,C,76,0|G083,C,150,0|OGRNR,C,15,0|INNR,C,12,0|KPP_RECEIV,C,9,0|ITNR,C,13,0|ONLYONEGDS,L,1,0|G05_NTFR,N,3,0|G222_R,N,16,2|G35_NTFR,N,17,2|CUSTCODES,C,8,0|G082S,C,76,0|G083S,C,150,0|GINNS,C,12,0|GKPPS,C,9,0|ONLYONEGD2,L,1,0|G05_NTFS,N,3,0|G222_S,N,16,2|G35_NTFS,N,17,2|CUSTCODET,C,8,0|G082T,C,76,0|G083T,C,150,0|GINNT,C,12,0|GKPPT,C,9,0|ONLYONEGD3,L,1,0|G05_NTFT,N,3,0|G222_T,N,16,2|G35_NTFT,N,17,2|G26,C,2,0|N_TD,C,250,0|N_TR,C,12,0|VIN,C,20,0|TC_COUNTRY,C,3,0|TC_ABC2,C,2,0|N_TRAILER,C,17,0|N_TRAILER2,C,17,0|TC_VOLUME,N,3,0|G3161,C,75,0|G52CODE,C,2,0|G447C,C,26,0|G52DATE,D,8,0|G504,D,8,0|SMELLED,C,23,0|SMELLEDATE,D,8,0|CODELNP_O,C,8,0|NUM_LNP_O,C,4,0|FOREIGN_PL,L,1,0|NUM_PLOM,C,15,0|ONCE_ADM,N,1,0|CMP_PGTD,C,1,0|NLIST,N,3,0|N_DOK_OUT,C,20,0|N_DOK_IN,C,20,0|D_IN_OKDT,D,8,0|PRIM,C,100,0|DAT_PD1,D,8,0|TIME_OUT1,C,5,0|PIGEONFIL1,C,12,0|TOTAL_NUV,N,2,0|DAT_PD,D,8,0|TIME_OUT,C,5,0|PIGEONFILE,C,13,0|GOTIT,C,1,0|GTK_FNAME,C,12,0|ISREAD,C,1,0|RTUN_FNAME,C,12,0|RTUN_READ,C,1,0|DATE_RCBD,D,8,0|SIGN2,C,1,0|RCP_CHKSUM,C,30,0|RTUO_FNAME,C,12,0|RTUO_READ,C,1,0|CREATEDATE,D,8,0|MODIFIED_D,D,8,0|ONTHEPAPER,D,8,0|STATUS_DOC,C,1,0|G05,N,3,0|AVTS_DOCNN,C,32,0|AVTS_RES_N,C,1,0|AVTS_DOCNC,C,32,0|AVTS_RES_C,C,1,0"; | ||
// | ||
// DbfMetadata meta = DbfMetadataUtils.fromFieldsString(fieldsInfo); | ||
// System.out.println(meta); | ||
|
||
|
||
} | ||
|
||
public static void method6(String[] args) throws Exception { | ||
//File file = new File("data/215451/tir_im.dbf"); | ||
//File file = new File("data/215451/gds_im.dbf"); | ||
|
||
File file = new File("data/218864/tir_im.dbf"); | ||
//File file = new File("data/218864/gds_im.dbf"); | ||
testReadWriteReadFile(file); | ||
} | ||
|
||
static void testReadWriteReadFile(File file) throws Exception { | ||
Charset stringCharset = Charset.forName("Cp866"); | ||
|
||
// 1. Read file | ||
|
||
DbfReader reader = new DbfReader(file); | ||
DbfMetadata meta = reader.getMetadata(); | ||
DbfRecord rec = null; | ||
List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>(); | ||
while ((rec = reader.read()) != null) { | ||
rec.setStringCharset(stringCharset); | ||
Map<String,Object> map = rec.toMap(); | ||
maps.add(map); | ||
} | ||
reader.close(); | ||
|
||
// 2. Write file | ||
String fieldsInfo = meta.getFieldsStringRepresentation(); | ||
System.out.println(fieldsInfo); | ||
DbfMetadata meta1 = DbfMetadataUtils.fromFieldsString(fieldsInfo); | ||
meta1.setRecordsQty(maps.size()); | ||
FileOutputStream out = new FileOutputStream("1.dbf"); | ||
DbfWriter writer = new DbfWriter(meta1,out); | ||
writer.setStringCharset(stringCharset); | ||
for (Map<String,Object> map : maps) { | ||
writer.write(map); | ||
} | ||
writer.close(); | ||
|
||
// 3. Read written file and compare data with initially read data | ||
{ | ||
DbfReader reader1 = new DbfReader(new File("1.dbf")); | ||
DbfRecord rec1 = null; | ||
List<Map<String,Object>> maps2 = new ArrayList<Map<String,Object>>(); | ||
while ((rec1 = reader1.read()) != null) { | ||
rec1.setStringCharset(stringCharset); | ||
Map<String,Object> map = rec1.toMap(); | ||
maps2.add(map); | ||
} | ||
reader.close(); | ||
|
||
// compare | ||
int i=0; | ||
boolean allEqual = true; | ||
for (Map<String,Object> map : maps2) { | ||
boolean mapEqual = JdbfUtils.compareMaps(map, maps.get(i)); | ||
System.out.println(mapEqual); | ||
allEqual = allEqual && mapEqual; | ||
i++; | ||
} | ||
System.out.println("allEqual="+allEqual); | ||
} | ||
} | ||
} |
Oops, something went wrong.
3be5dbb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please help me i got error unfortunatly stop i need urgent this code as i an new in android please sir help me
and send me code i possible on my email sharmakunal312@gmail.com
thanks sir